Hi,
I am using repaint(); to show animation on a JPanel. The problem is, when I add a background to my JPanel, and then initiate the repaint(); method, it repaints my entire JPanel which is really bad for performance. My question to you is: Is there any way to specify which object I would like to repaint? Heres my example code:
[CODE]public class DrawFrame{
public int speed = 10;
public int x = 480;
public int y = 640;
public int sx = 0;
public int sy = 0;
public int ex = 480;
public int ey = 60;
public String background = “background.jpg”;
public File background2 = new File(background);
public BufferedImage image2;
public String path = “plane.gif”;
public File file = new File(path);
public BufferedImage image;
public DrawFrame() {
Container cp = this.getContentPane();
DrawingPanel dp = new DrawingPanel();
cp.add(dp);
dp.addKeyListener(this);
dp.setFocusable(true);
}
public static void main(String[] args) {
DrawFrame f = new DrawFrame();
f.setExtendedState(f.getExtendedState()|JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void refresh(){
repaint();
}
class DrawingPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
try {
image = ImageIO.read(file);
g.drawImage(image,x,y, this);
g.setColor(Color.red);
g.fillRect(sx, sy, 5, 15);
g.setColor(Color.green);
g.fillRect(ex, ey, 50, 50);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
this is just a snipit of code in which i removed the part you dont need to see in order to make it easier to read.
So I’m hoping theres some way to say something like: “fillRect.repaint();” or something along those lines.
Thanks for reading,
Nate