0
votes

I am creating a game that involves drawing on the order of 1000 filled circles, of radii between 2 - 10 pixels, onto a large JPanel at a frequency of 50Hz.

How I am currently doing this is by drawing each circle by calling the drawOval method via the JPanel itself. This, however, is very slow and somehow manages to increase the game running time by 50%.

So I thought of making use of a BufferedImage: create the buffered image and draw the filled circles on the buffered image instead. Then draw the entire buffered image onto the JPanel in one go. Will this approach be any faster than my original method? Is the use of drawOval itself for the 1000 or so times slow in both cases?

I suspect a lot of processing is wasted by printing the same pixels again whenever such pixels have not changed colour. Is there a way to avoid repainting such redundant pixels?

1
This depends on a lot of things. How large are the circles for instance. I'd suggest you try it out and perform relevant benchmarks yourself. Any answer would basically be speculative.aioobe
The circles have radii that would range from 2 to 10 pixelsInvolute
Swing already implements double buffering. You can change it though with setBufferStrategy.user1803551

1 Answers

0
votes

If you create a DrawPanel you don't need instance variables. DrawPanel is located in the Javax.swing package.

To Create an oval you

  1. create a DrawPanel
  2. Add the DrawPanel to a JFrame

Example:

private class MyDrawPanel extends JPanel 
{ 
    public void paintComponent(Graphics g) 
    { 
        g.fillRect(0,0,this.getWidth(),this.getHeight());        
        g.setColor(Color.white); 
        g.fillOval(90,180,150,150) 
    } 
}