Java applet: is there a simple way to draw and erase in a two-layered 2D scene?

This seems fairly quick, so long as the rendered image area remains around 640x480, the code can achieve from 125-165 FPS. The code tracks 2000 semi-transparent lines of width 4px, and moves them around in an area 8 times the size of the rendered image.

This seems fairly quick, so long as the rendered image area remains around 640x480, the code can achieve from 125-165 FPS. The code tracks 2000 semi-transparent lines of width 4px, and moves them around in an area 8 times the size of the rendered image. Import java.awt.image.

BufferedImage; import java.awt.event. *; import java.awt.geom. *; import java.awt.

*; import javax.swing. *; import java.util. Random; class LineAnimator { public static void main(String args) { final int w = 640; final int h = 480; final Renderingnts hints = new RenderingKEY_ANTIALIASING, RenderingVALUE_ANTIALIAS_ON ); hints.

Put( RenderingKEY_ALPHA_INTERPOLATION, RenderingVALUE_ALPHA_INTERPOLATION_QUALITY ); final BufferedImage bi = new BufferedImage(w,h, BufferedImage. TYPE_INT_RGB); final JLabel l = new JLabel(new ImageIcon(bi)); final BouncingLine lines = new BouncingLine20000; for (int ii=0; ii1000 ) { lastTime = System. CurrentTimeMillis(); fps = count + " FPS"; count = 0; } g.

SetColor(Color. YELLOW); g. SetFont(font); g.

DrawString(fps,10,30); l.repaint(); g.dispose(); } }; Timer timer = new Timer(1,al); timer.start(); JOptionPane. ShowMessageDialog(null, l); System. Exit(0); } } class BouncingLine { private final Color color; private static final BasicStroke stroke = new BasicStroke(4); private static final Random random = new Random(); Line2D line; int w; int h; int x1; int y1; int x2; int y2; BouncingLine(int w, int h) { line = new Line2D.

Double(random. NextInt(w),random. NextInt(h),random.

NextInt(w),random. NextInt(h)); this. W = w; this.

H = h; this. Color = new Color( 128+random. NextInt(127), 128+random.

NextInt(127), 128+random. NextInt(127), 85 ); x1 = (random.nextBoolean()? 1 : -1); y1 = (random.nextBoolean()?1 : -1); x2 = -x1; y2 = -y1; } public void move() { int tx1 = 0; if (line.

GetX1()+x1>0 && line. GetX1()+x10 && line. GetY1()+y10 && line.

GetX2()+x20 && line. GetY2()+y2GetY2()+y2; } line. SetLine(tx1,ty1,tx2,ty2); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.

SetColor(color); g2. SetStroke(stroke); //line. Set g2.

Draw(line); } } Update 1 When I posted that code, I thought you said 100s to 1000s, rather than 1000s to 100,000s! At 20,000 lines the rate drops to around 16-18 FPS. Update 2 ..is this optimized approach, using layers, possible in Java?Sure.

I use that technique in DukeBox - which shows a funky plot of the sound it is playing.It keeps a number of buffered images.Background. A solid color in a non-transparent image. Old Traces.

The older sound traces as stretched or faded from the original positions. Has transparency, to allow the BG to show. Latest Trace.

Drawn on top of the other two. Has transparency.

Nice code! But it uses the OpenGL/game approach of redrawing absolutely everything on every frame. (Your example requires this anyway because your lines are large and all moving all the time.) The point of my question is that sometimes it is possible to hugely optimize this, in situations where drawing and erasing locally is an option.

There are cases where drawing and erasing locally only works if you can do it in separate layers (so that erasing in one layer doesn't disturb the other layers). My question was basically, is this optimized approach, using layers, possible in Java? – Matt Sep 13 at 21:16 "The point of my question is that sometimes it is possible to hugely optimize.." The point of my answer is that sometimes it is not necessary.

You know 'premature optimization' and all that? ;) As to the rest of your comment, see Update 2. – Andrew Thompson Sep 14 at 3:36 @Andrew Thompson please why there paint, instead of paintComponent, hmmm and in most of your answers, are you know something ... – mKorbel Nov 3 at 20:13 If you mean the BouncingLineA) it is not a component (so I might have called it paintLine) B) it is also 'not Swing'.

– Andrew Thompson Nov 6 at 2:24.

After a day of no proposed solutions, I started to think that Java Graphics cannot erase individual items back to a transparent color. But it turns out that the improved Graphics2D, together with BufferedImage and AlphaComposite, provide pretty much exactly the functionality I was looking for, allowing me to both draw shapes and erase shapes (back to full transparency) in various layers. Now I do the following in start(): screenBuffer = new BufferedImage(640, 480, BufferedImage.

TYPE_INT_ARGB); screenBufferGraphics = screenBuffer.createGraphics(); overlayBuffer = new BufferedImage(640, 480, BufferedImage. TYPE_INT_ARGB); overlayBufferGraphics = overlayBuffer.createGraphics(); I have to use new BufferedImage() instead of createImage() because I need to ask for alpha. (Even for screenBuffer, although it is the background -- go figure!) I use createGraphics() instead of getGraphics() just because my variable screenBufferGraphics is now a Graphics2D object instead of just a Graphics object.

(Although casting back and forth works fine too. ) The code in paint() is barely different: g. DrawImage(screenBuffer, 0, 0, null); g.

DrawImage(overlayBuffer, 0, 0, null); And objects are rendered (or erased) like this: // render to background screenBufferGraphics. SetColor(Color. Red); screenBufferGraphics.

FillOval(80,80, 40,40); // render to overlay overlayBufferGraphics. SetComposite(AlphaComposite. SrcOver); overlayBufferGraphics.

SetColor(Color. Green); overlayBufferGraphics. FillOval(90,70, 20,60); // render invisibility onto overlay overlayBufferGraphics.

SetComposite(AlphaComposite. DstOut); overlayBufferGraphics. SetColor(Color.

Blue); overlayBufferGraphics. FillOval(70,90, 30,20); // and flush just this locally changed region repaint(60,60, 80,80); The final Color. Blue yields transparency, not blueness -- it can be any color that has no transparency.

As a final note, if you are rendering in a different thread from the AWT-EventQueue thread (which you probably are if you spend a lot of time rendering but also need to have a responsive interface), then you will want to synchronize the above code in paint() with your rendering routine; otherwise the display can wind up in a half-drawn state. If you are rendering in more than one thread, you will need to synchronize the rendering routine anyway so that the Graphics2D state changes do not interfere with each other.(Or maybe each thread could have its own Graphics2D object drawing onto the same BufferedImage -- I didn't try that.) It looks so simple, it's hard to believe how long it took to figure out how to do this!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions