Results 1 to 3 of 3
  1. #1
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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();
        }
    }

  3. #3
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    [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

  1. Need For Clear Screen
    By adithya4u4ever in forum New To Java
    Replies: 8
    Last Post: 03-08-2009, 08:41 AM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. Newbie need help on JPanel graphics
    By junpogi in forum AWT / Swing
    Replies: 7
    Last Post: 10-21-2008, 07:44 AM
  4. clear cache
    By Jadellll in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 09:27 AM
  5. Help, someone clear up Interfaces for me
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •