jpanel

Java basic 2d game animation stutter

你说的曾经没有我的故事 提交于 2019-11-28 14:22:14
So, I have been working on a 2d rpg for some time now and I can't seem to fix this one problem. The graphics seem to "jump" or stutter every few seconds for an unknown reason. This is getting quite annoying because I don't know what is causing it. Here is a very basic program I wrote that just has a red square that moves from one side of the screen to the other side. Even in this very basic program the square still stutters every few updates and I really can't figure it out for the life of me. public class Main extends JPanel { int x=0, y=0; public JFrame window = new JFrame("Window"); public

JLabel won't show with JPanel.setLayout(null). Why?

我的梦境 提交于 2019-11-28 14:21:08
I want to show many different labels over a map, so I'm using null layout in my panel, and calling setLocation for each label. For some reason, though, the labels don't show. If I remove the pan.setLayout(null), then the label appears in the top-center of the panel. Why isn't null layout working with setPosition? package mapa; import java.awt.*; import javax.swing.*; public class Mapa extends JFrame { private static JPanel pan; private static JLabel lab; public Mapa() { } private static void createAndShowGUI() { Mapa frame = new Mapa(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lab

Prevent Java from repainting the content of a JPanel while updating

一个人想着一个人 提交于 2019-11-28 14:20:26
I have a JPanel which contains a lot of child components. While updating\adding new components to the parent JPanel I'd like to prevent it from repainting, how can this achieved? Try RepaintManager.currentManager(component).markCompletelyClean(component) . It will prevent the component from repainting. You might need to do this after each time you add new components. setVisible(false) update setVisible(true) you could try by using setIgnoreRepaint(boolean value) but it's a typical swing feature that can or cannot work (mainly because it depends from AWT so you never know). Otherwise you could

Visual Artifacts appearing on JPanel

泪湿孤枕 提交于 2019-11-28 14:15:51
I am trying to create a program with 2 JPanel using BorderLayout . The center panel is for random drawing of rectangle while the south panel is for the buttons. I am getting a weird image of the button on the top left corner of the JFrame whenever I hover the mouse cursor over the North or South button. I did some research and found out that this could be the reason for having a transparent background. I tried using super.paintComponent(g) for the panel but the rest of the rectangles drawn earlier disappear. I need the rectangles to stay in the JPanel but not the weird image on the top left. I

How does one properly handle keypresses and repainting of a JComponent in the context of moving a ball around a screen?

…衆ロ難τιáo~ 提交于 2019-11-28 14:03:29
I thought I would try and write a program that would paint a ball, and when the arrow keys are pressed, would move the ball around the screen in the direction pressed. First I started off to try and make a program that would just do "UP" arrow key motion. I've looked around for a solution, and just can't figure out what's wrong with this code. I don't know if it's a problem with my input and action maps (i.e., a problem with the program recognizing key presses) or if it's a problem with how the JComponent and JFrame classes work in swing. I thought maybe the problem might also be focus; I don

Specifying the location of Canvas in Swing

只愿长相守 提交于 2019-11-28 13:46:30
I'm learning Swing GUI design. One thing I haven't quite sorted out is how to add an add a Canvas to a specific location in a container. More specifically: I create a Canvas class that uses Paint method. Object of this class is added to a Panel. What I do not quite understand, is how and where it is added to a Panel. In Tkinter Canvas is a widget that contains only an image, but in Swing there is no similar widget (probably not the best word) added to the Frame that contains only Canvas object and nothing else. Sorry if it is too vague, I'm adding a self-contained code. Please ignore text

Why gif animation doesn't animate when using it in paintComponent()?

纵然是瞬间 提交于 2019-11-28 13:32:23
I'm using paintComponent() to paint a gif animated image at the backgound of JPanel . It shows up the gif but doesn't animate. I use java 1.5 and i know that i can use label with icon. Does any body know why and how to fix it? private static class CirclePanel extends JPanel { ImageIcon imageIcon = new ImageIcon(BarcodeModel.class.getResource("verify.gif")); Point point = f.getLocation(); protected void paintComponent(Graphics g) { Graphics gc = g.create(); Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d

Adding background image to JPanel on button action

安稳与你 提交于 2019-11-28 13:12:52
What is the best way to add a background image to a JPanel/JLabel when a JButton is called? I know how to get the JButton action and such. I just can't figure out or find a way to get the background image to change when that button is pressed. Here is an example: import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import java.util.concurrent.ExecutionException; import javax.imageio.ImageIO; import javax.swing.ImageIcon;

JPanel positioned incorrectly

夙愿已清 提交于 2019-11-28 13:06:27
问题 I've made a game in Java, where you're supposed to guess a random-generated int, between 1-100. If you guess too low, a textfield will be filled with a text that says, "too low" but in Swedish. Same if you guess too high. The problem occurs when the answer is right, I then call a method to make an Invisible JPanel, visible. That works just fine, but the Jpanel itself won't move to the top where its supposed to be. And it needs to fit perfectly since it's just an photoshopped picture of the

.drawLine() issues and buffered image

半腔热情 提交于 2019-11-28 12:57:45
I have a paint programme and i have all the buttons and sliders done however i am having a problem with the actual painting itself. When I drag the cursor across the screen instead of an unbroken line I am getting almost a dotted line which i dont want. Here's the code for the MouseListener in the JPanel and BufferedImage : public void mouseDragged(MouseEvent e) { Graphics g=buffered.getGraphics(); g.setColor(mycol); Graphics2D graph=(Graphics2D)g; BasicStroke stroke=new BasicStroke(30); graph.setStroke(stroke); // g.fillRect(xcor, ycor, 20, 20); / /varx=e.getX(); ycor=e.getY(); xcor=e.getX();