jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Is this possible to do? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is this possible to do?" Watch "Is this possible to do?" New topic
Author

Is this possible to do?

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, is it possible to draw an image inside a JPanel or something similar, then draw over it with say a circle, and then get a circle to move over the image without repainting the image.

Thanks for any help
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Yes. One easy way is to use a non–opaque cover component in an OverlayLayout.

You can draw on the overlay component, move the graphics with mouse code or animation code.
Another way, with j2se 1.5+, is to use a Container and set the z-order on the components.
You would use a null layout and position the components with setBounds.
Another is to use a JLayeredPane which supports z–order.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Note that Swing actually *will* need to repaint (parts of) the image with that solution.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Excellent, I tried it and it works.

I'm not sure I can apply double buffering though using this method, since for double buffering I need to draw a picture into an Image/BufferedImage's getGrapics() object which will be rectangular.

Then inside the JPanel which needs to be drawn above the background, I need to paint this image to screen through its paintComponent(Graphics g) method.

Then for the background, I have a separate class extending JPanel,
where the paintComponent method uses drawImage to draw the background image.

So basically, when I set up the double buffering:

The offscreen BufferedImage is initially a black square, so using bufferGraphics.drawImage(...) will draw a black square(+whats inside square), which won't work the same as just drawing a circle on screen on top of the old background.

Any ideas?
Thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I feel like I don't have a clear understanding of your question so I'll try restating.
You are considering using a BufferedImage to draw in the paintComponent method of the top/overlay component. In this BufferedImage you want to draw a circle.
If this is correct and all you want to do is to move the circle around in the component you could eliminate the BufferedImage and draw the circle directly in the paintComponent method. Swing uses double buffering by default. So using a single component, drawing the background image and drawing the (moving) circle above it will be double buffered. Only the parts of the background image that had parts of the circle drawn above it in the last animation frame will need to be redrawn. The same applies to the overlay scheme, bits of the background that were covered/re-exposed will be redrawn.
Using double buffering may not be necessary.
Back to the question of how to make a BufferedImage that will only show a circle you can try something like this (pseudo)

If you want to use BufferedImage.TYPE_INT_ARGB you can try

BufferedImage graphics has a default color of black so you have to fill in the background with the color you want.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, I'll try and explain my problem...

I have an applet
foto cube
It is signed, but thats just so you can save and load cubes.

Anyway the cube, uses double-buffering and is on a blue background...
(Just using 1 JPanel, no OverlayLayout here)


This works well... I noticed problems when I didn't use double buffering, there was some flickering when the cube was moving.

I noticed you can pass 'true' into the constructor to get double-buffering, but I don't think it worked when I tried is last.

My problem is:
Where I have the blue background(500 by 500), I would like to use an image/bufferedimage of the same size.

Clearly I can just put the line...

in above, but I tried this before, and it seemed to be a bit of a drag on the cpu which is already busy twisting and rotating the cube.

This is the reason I wanted to try and just paint the background image once(eg with OverlayLayout), then just repaint the cube on top.

The thing with a circle was just a test to see if I could do it, but its now got a bit confusing with all this doublebuffering stuff.

I'm not sure what the ideal solution to this is.
Hope this makes it clearer.

Thanks for any help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this possible to do?