How to move a second Object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to move a second Object

  1. #1
    Join Date
    Apr 2006
    Posts
    4

    How to move a second Object

    I have a JFrame with a JPanel.

    I have a red square which can be moved around. I now want to add a second square in green which can also be moved around. I have gotten the green square to appear in the position I have set x and y on the panel but it will not allow me to move it.

    Any ideas where i'm going wrong would be a big help as i want to add other movable squares after this one.

    My code is below:

    Code:
        
    package painting3;
     
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;
     
    public class SwingPaintDemo3 {
     
       public static void main(String[] args) {
     
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
     
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
            SwingUtilities.isEventDispatchThread());
            JFrame f = new JFrame("The Rush Hour Game");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.add(new MyPanel());
            f.setSize(300,300);
            f.setVisible(true);
        } 
     
    }
     
    class MyPanel extends JPanel {
     
        RedSquare redSquare = new RedSquare();
        GreenSquare greenSquare = new GreenSquare();
     
        public MyPanel() {
     
            setBorder(BorderFactory.createLineBorder(Color.black));
     
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    moveSquare(e.getX(),e.getY());
                }
            });
     
            addMouseMotionListener(new MouseAdapter(){
                public void mouseDragged(MouseEvent e){
                    moveSquare(e.getX(),e.getY());
                }
            });
     
        }
     
        private void moveSquare(int x, int y){
     
            // Current square state, stored as final variables 
            // to avoid repeat invocations of the same methods.
            final int CURR_X = redSquare.getX();
            final int CURR_Y = redSquare.getY();
            final int CURR_W = redSquare.getWidth();
            final int CURR_H = redSquare.getHeight();
            final int OFFSET = 1;
     
            if ((CURR_X!=x) || (CURR_Y!=y)) {
     
                // The square is moving, repaint background 
                // over the old square location. 
                repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
     
                // Update coordinates.
                redSquare.setX(x);
                redSquare.setY(y);
     
                // Repaint the square at the new location.
                repaint(redSquare.getX(), redSquare.getY(), 
                        redSquare.getWidth()+OFFSET, 
                        redSquare.getHeight()+OFFSET);
            }
        }
                
           private void moveSquare2(int x, int y){
                final int CURR_X = greenSquare.getX();
                final int CURR_Y = greenSquare.getY();
                final int CURR_W = greenSquare.getWidth();
                final int CURR_H = greenSquare.getHeight();
                final int OFFSET = 1;
     
                if ((CURR_X!=x) || (CURR_Y!=y)) {
     
                // The square is moving, repaint background 
                // over the old square location. 
                repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
     
                // Update coordinates.
                greenSquare.setX(x);
                greenSquare.setY(y);
     
                // Repaint the square at the new location.
                repaint(greenSquare.getX(), greenSquare.getY(), 
                        greenSquare.getWidth()+OFFSET, 
                        greenSquare.getHeight()+OFFSET);
            }
        }
     
        public Dimension getPreferredSize() {
            return new Dimension(300,300);
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
            redSquare.paintSquare(g);
            greenSquare.paintSquare2(g);
        }  
    }
     
    class RedSquare{
     
        private int xPos = 50;
        private int yPos = 50;
        private int width = 20;
        private int height = 20;
     
        public void setX(int xPos){ 
            this.xPos = xPos;
        }
     
        public int getX(){
            return xPos;
        }
     
        public void setY(int yPos){
            this.yPos = yPos;
        }
     
        public int getY(){
            return yPos;
        }
     
        public int getWidth(){
            return width;
        } 
     
        public int getHeight(){
            return height;
        }
     
        public void paintSquare(Graphics g){
            g.setColor(Color.RED);
            g.fillRect(xPos,yPos,width,height);
            g.setColor(Color.BLACK);
            g.drawRect(xPos,yPos,width,height);  
        }
    }
     
    class GreenSquare{
     
        private int xPos = 200;
        private int yPos = 200;
        private int width = 20;
        private int height = 20;
     
        public void setX(int xPos){ 
            this.xPos = xPos;
        }
     
        public int getX(){
            return xPos;
        }
     
        public void setY(int yPos){
            this.yPos = yPos;
        }
     
        public int getY(){
            return yPos;
        }
     
        public int getWidth(){
            return width;
        } 
     
        public int getHeight(){
            return height;
        }
     
        public void paintSquare2(Graphics g){
            g.setColor(Color.GREEN);
            g.fillRect(xPos,yPos,width,height);
            g.setColor(Color.BLACK);
            g.drawRect(xPos,yPos,width,height);  
        }
    }

  2. #2
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    The quickest way to get things working, would be to make modifications to addMouseListener and addMouseMotionListener, so that they look like:

    Code:
           addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    moveSquare(e.getX(),e.getY());
                    moveSquare2(e.getX(), e.getY());
                }
            });
     
            addMouseMotionListener(new MouseAdapter(){
                public void mouseDragged(MouseEvent e){
                    moveSquare(e.getX(),e.getY());
                    moveSquare2(e.getX(), e.getY());
                }
    ie the addition of moveSquare2(). (Maybe you forgot that).

    However, please note that this is not making use of the object oriented concepts such as inheritance, which would help you reduce the size of the code.

Similar Threads

  1. Now "they" says Stateful Object Powerful ??
    By Hendry in forum Enterprise
    Replies: 3
    Last Post: 11-18-2003, 01:20 PM
  2. Replies: 7
    Last Post: 10-24-2002, 06:33 AM
  3. Replies: 0
    Last Post: 08-08-2002, 05:56 AM
  4. Even C++ had this much right...
    By Derek Mooney in forum .NET
    Replies: 94
    Last Post: 10-29-2001, 08:44 PM
  5. Object Modeling for the WEB
    By Greg Dirst in forum authorevents.appleman
    Replies: 1
    Last Post: 04-10-2000, 02:56 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center