flamethrower205

Illustrious
Jun 26, 2001
13,105
0
40,780
I need some programming help in Java. Right now I have a particle simulation (that I optimized the [-peep-] out of....except the actually displaying). My issue is in the displaying. Currently it extends JFrame and repaints every 1/60 of a second by a thread.delay method in the main. What it basically does each repaint is call methods like g.setColor and g.drawLine (and other shapes) to draw gridlines, background, and the particles (particles are circles of radius 5 pixels with random colors, default being blue). What I want to do is somehow set as background the grid cause that never changes. Part of the problem is it flickers, so I want to eliminate this- would using a FrameBuffer alleviate this? If so, how do I go about doing it? It should really only have to repaint the particles as they are what moves.
Any help/ code snipits would be greatly appreciated. I'll admit I have very little experience with graphics in java since I've never had to make visuals (generally it just outputs to a text file..)


The one and only "Monstrous BULLgarian!"
 

Schmide

Distinguished
Aug 2, 2001
1,442
0
19,280
You need to do double buffering. Actually back buffering, but for the most part usually referred as so. Double buffering is an actual hardware method for flipping surfaces. For the most part

1 Create an Image equal to your window size.

Image image1;
Graphics graphics;

public void init()
{
image1 = createImage(x, y);
graphics = image1.getGraphics();
}

2 In your paint loop

public void paint (Graphics g)
{
/* draw into the background graphics */
graphics.setColor(new Color(r, g, b));
graphics.(drawsomething)


/* blt image to actual graphics */
g.drawImage(image1,0,0,this)
}

I left out a bit of setup but I’m sure you can add that.

Dichromatic for your viewing plesure...