1

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.

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • "I call the setDubbelBuffer(true)in the constructor of gameview." Are you sure about that? I see: `drawing.setDoubleBuffered(false);` – gla3dr Oct 01 '15 at 17:25
  • @gla3dr yes i accidently put it on false while debugging but it didnt make a difference when putting it on true again. – Sven van den Boogaart Oct 01 '15 at 17:26
  • 1
    @MadProgrammer went over why you shouldn't use `getGraphics` in a fairly similar type of question: [Java get graphics](http://stackoverflow.com/questions/28015214/java-get-graphics) – Obicere Oct 01 '15 at 17:32
  • @Obicere Thanks for the link i checked both tutorials but i still don't see what Graphic g i could pass to paintComponent() from Render to avoid using getGraphics in paintComponent. – Sven van den Boogaart Oct 01 '15 at 18:11
  • @SvenB the one that Swing gives you! Override `paintComponent(Graphics)` and this will be called automatically when repainting a component with the appropriate graphics object. – Obicere Oct 01 '15 at 18:13
  • @Obicere Im trying to use the mvc pattern, do i need to add a model to my JPanel? Im gettin really confused, most tutorials/answers have a model that extends JPanel. – Sven van den Boogaart Oct 01 '15 at 18:33
  • @SvenB well, your `MyDrawing` classes extend `JPanel`. You could override the `paintComponent` method there. However, draw calls might not be immediate. Swing is not a library for games unfortunately. Alternatively, you can paint to an off-screen render and draw that onto a panel. – Obicere Oct 01 '15 at 18:34

0 Answers0