Results 1 to 5 of 5
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default Right-clicking a rectangle on a JFrame to remove it

    In my program the user clicks a spot on a JFrame and a fixed-size Rectangle appears with its top-left corner at the spot where the user clicked.

    All created Rectangles are stored in an ArrayList.

    I need to get it so the user can right-click a spot on the JFrame, and if there's a rectangle there, the Rectangle is deleted.

    Java Code:
    public GraphElementComponent()
            {
                eList = new ArrayList<EllipseNode>();
                rList = new ArrayList<RectangleNode>();
                
                class MouseActionListener implements MouseListener
                {
                    public void mousePressed(MouseEvent event)
                    {
                        
                        
                        // Left (or right) mouse button is pressed 
                        if (event.getButton() == event.BUTTON1 || event.getButton() == event.BUTTON3)
                        {
                            mouseX = event.getX();
                            mouseY = event.getY();
                        }
                    }
                    public void mouseReleased(MouseEvent event)
                    {
                        if (event.getButton() == event.BUTTON1)
                        {
                            int x = event.getX();
                            int y = event.getY();
                            RectangleNode rn = new RectangleNode(mouseX, mouseY, 50, 150);
                            rList.add(rn);
                            // geList.add(rn);
                            repaint();
                        }
    
    
    
    
    
    else if(event.getButton() == event.BUTTON3)
                        {
                            int x = event.getX();
                            int y = event.getY();
    
    
    // I'm not sure what to put here

    I tried something like



    for (int i = 0; i < rList.size(); i++)
    {
    if (rList(i).contains(x, y))
    {
    rList.remove(r.List(i));
    repaint();

    }




    but it gives some error. Please help.


    the code that I posted is not the entire program obviously, but the only part relevant(I hope) to this problem.
    Last edited by busdude; 02-20-2009 at 01:51 AM.

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Shouldn't you use a mouse clicking event listener which initializes locals x and y first? Or did you already do that but not show it?
    Java Code:
    rList.remove([COLOR="Green"]r[COLOR="Red"].[/COLOR]List(i)[/COLOR]);
    That whole green part looks strange, is the red dot supposed to be there, and shouldn't it just be "i" for the index, not r.List(i) ?. Hope that helped
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    but it gives some error.
    relevant information to post here would include the error message.

  4. #4
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    well I got it to delete things that are right-clicked so I'm on new problems now.


    Is there a way to make a specific action not apply to a specific area of a JFrame?

    I described how my program draws rectangles, and my JFrame is 600 long and 500 tall. Is there a way I can make it so it restricts the drawings to the 500 on the right, ( so if the user clicks a spot somewhere within the first 100 to the left on the JFrame, it does not draw a rectangle)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes. Don't draw directly in the JFrame and don't have the mouselistener attached directly to the JFrame. Better to do this interacting in a JPanel and then layout the JPanel exactly where you want it in the JFrame using an effective mix of layout managers.

Similar Threads

  1. [Applet] Clicking images & moving them.
    By Perfectworld in forum Java Applets
    Replies: 17
    Last Post: 01-16-2010, 08:09 AM
  2. Double clicking executables jar files?
    By cruxblack in forum New To Java
    Replies: 6
    Last Post: 04-23-2009, 02:00 AM
  3. painting lines when clicking a panel
    By yuriythebest in forum New To Java
    Replies: 0
    Last Post: 12-14-2008, 09:56 PM
  4. Replies: 0
    Last Post: 06-25-2008, 10:34 AM
  5. Java3D: Clicking and getting coordinates?
    By seabhcan in forum Advanced Java
    Replies: 0
    Last Post: 01-11-2008, 02:46 PM

Posting Permissions

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