In a Java application im making i want to update my view and redraw some targets with the positions they have.
My GameView render function and constructor:
//Constructor
public GameView(){
frame = new JFrame("Hunter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawing = new MyDrawing();
drawing.setDoubleBuffered(true);;
drawing.setIgnoreRepaint(true);
frame.getContentPane().add(BorderLayout.CENTER,drawing);
frame.setResizable(false);
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//Render method where i want to draw all the object. (called form another class)
public void Render(ArrayList<Target> targets){
//drawing is a MyDrawing object see next code.
// i clear the jpanel with a white background
drawing.drawBackground();
// Loop al targets and paint them
for (Target target : targets) {
drawing.paintComponent(target.getX(), target.getY(), target.getSize());
}
}
The MyDrawing class.
public class MyDrawing extends JPanel {
//Paint a target
public void paintComponent( int x, int y, int size){
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillOval(x, y, size, size);
}
public void drawBackground(){
Graphics g = getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(),this.getHeight());
}
}
Render in my GameView is called in a loop from my GameController;
public void Run(){
while(notClosed){
Render();
}
try {
Thread.sleep(1000/100);
} catch (Exception e) {
// TODO: handle exception
}
}
}
public void Render(){
myView.Render(myGame.getTargets());
}
Eveything I want is drawn on the screen but i see some flashes. Especialy on the left side of the jpanel the flashes are very visible. Also once in every x? secons the whole screen flashes. I call the setDubbelBuffer(true)in the constructor of gameview.