Results 1 to 3 of 3
- 05-20-2009, 06:27 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
Clear Graphics Objects from Jpanel
Hey guys,
I was just wondering how to clear all Shape components that have been added to a Jpanel from the Jpanel itself?
i.e. I have a Jpanel that has multiple Rectangles, Circles etc etc, and I was hoping there would be a simple way of removing all of these components from the Jpanel in a simple fashion.
Any guidance would be greatly appreciated,
David
- 05-20-2009, 09:07 PM #2
I was hoping there would be a simple way
Everything in graphics work is low-level. So you have to build these things yourself.
There are generally two ways to do this:
Store the shapes in an array or java.util.List ( = new ArrayList()) and access them from the (chosen) data structure for manipulation/rendering. When you want to "remove" them reallocate/instantiate the array to a zero-length array or call the clear method on the list. then call repaint from your event code.
The other way is to use an offscreen image, aka double buffer. Draw each shape to the image in your event code and draw the image in your components paintComponent method. This is usually/more for specialty uses. To "remove" the shapes you clear the image by refilling its background. In this approach you don't have to store the shapes as you do in the first/easier approach.
Java Code:public class Pseudo extends JPanel { List<Shape> shapes = new ArrayList<Shape>(); protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; for(Shape s : shapes) { g2.draw(s); } } /** In some event code. */ public void eventMethod(SomeEvent e) { shapes.clear(); repaint(); } }
- 05-20-2009, 09:34 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
[SOLVED]
hey mate,
thanks a lot for your input (on both of my questions), I understand that this may be a less admirable way of achieving the desired goal, but after playing around I found that an easy way to clear all the components on the Jpanel lets call it mypanel is to use
mypanel.removeAll();
As I said before I'm sure it isn't the best way of going about it, however its a quick and easy solution and my assignment is due in three days so I'm gonna live with it for the time being!
Again thanks for your help,
David
Similar Threads
-
Need For Clear Screen
By adithya4u4ever in forum New To JavaReplies: 8Last Post: 03-08-2009, 08:41 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
Newbie need help on JPanel graphics
By junpogi in forum AWT / SwingReplies: 7Last Post: 10-21-2008, 07:44 AM -
clear cache
By Jadellll in forum New To JavaReplies: 0Last Post: 03-20-2008, 09:27 AM -
Help, someone clear up Interfaces for me
By mathias in forum New To JavaReplies: 1Last Post: 08-06-2007, 02:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks