Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Dragging and Dropping Rectangle - GUI

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dragging and Dropping Rectangle - GUI

    I'm writing a program that displays a GUI with two rectangles ... upon clicking inside the area of either rectangle, the user should be able to drag the rectangle wherever they like, during which time, the color of the rectangle should change - but it should go back to its original color upon declicking. I can get my rectangle to move, but I cannot get it to change color. What should I do?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class ColorPanel extends JPanel{
     
    private Rectangle r1, r2, r3;
    private Rectangle selectedRectangle; // Used to track selected shape
    private int x, y ; // Used to track mouse coordinates 
     
    public ColorPanel(Color backColor){
    setBackground(backColor);
    r1 = new Rectangle(5, 5, 25, 25, Color.red);
    r2 = new Rectangle(50, 50, 25, 25, Color.blue);
    selectedRectangle = null; 
    addMouseListener(new PanelListener());
    addMouseMotionListener(new PanelMotionListener());
    }
     
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    if (selectedRectangle == null)
    r1.fill(g);
    r2.fill(g); 
     
    }
     
    private class PanelListener extends MouseAdapter{
     
    public void mousePressed(MouseEvent e){
    x = e.getX();
    y = e.getY();
    if (r1.containsPoint(x, y)){
    selectedRectangle = r1;
     
    }
    else if (r2.containsPoint(x, y)){
    selectedRectangle = r2; 
    }
     
    }
     
    public void mouseReleased(MouseEvent e){
    x = e.getX();
    y = e.getY();
    selectedRectangle = null;
     
    } 
    }
     
    private class PanelMotionListener extends MouseMotionAdapter{
     
    public void mouseDragged(MouseEvent e){
    int newX = e.getX();
    int newY = e.getY();
    int dx = newX - x;
    int dy = newY - y;
     
    if (selectedRectangle == r1){
    selectedRectangle.move(dx, dy);
    x = newX;
    y = newY; 
    repaint();
     
    }
    }
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Dragging and Dropping Rectangle - GUI

    I assume Rectangle is a custom class. If so does it have a setColor method? If so how about setting its colour inside your Listener classes.

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Dragging and Dropping Rectangle - GUI

    Have you considered the Swing Drag & Drop framework which provides facilities for this kind of thing?

Similar Threads

  1. grow a rectangle in java
    By Khoatic in forum Java Theory & Questions
    Replies: 2
    Last Post: September 8th, 2010, 06:13 PM
  2. Fill in rectangle partially (clip?¿)
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 5
    Last Post: March 27th, 2010, 01:38 PM
  3. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM
  4. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 08:22 AM