Results 1 to 9 of 9

Thread: Dragging Boxes

  1. #1
    anilanar is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default Dragging Boxes

    I can't drag my boxes using MouseMotionListener, helps are appreciated.

    Java Code:
    class DragingBoxesPanel extends JPanel implements MouseListener, MouseMotionListener{
    	
    	private boolean dragging;
    	private int whichBox;			// 0: RED, 1: BLUE 2: NONE
    	private int lastBox;
    	private int redX;
    	private int redY;
    	private int blueX;
    	private int blueY;
    //...
    Java Code:
    public void paintComponent(Graphics g) {
    // ...
                    g.setColor(Color.RED);
    		g.fillRect(redX,redY,50,50);
    		g.setColor(Color.BLUE);
    		g.fillRect(blueX,blueY,50,50);
    Java Code:
    	public void mousePressed(MouseEvent evt) {
    		
    		int x = evt.getX();
    		int y = evt.getY();
    		
    		if(evt.isShiftDown()) {
    			redX = 5;
    			redY = 5;
    			blueX = 75;
    			blueY = 5;
    			repaint();
    		}
    		
    		else {
    			
    			if((x>=redX && x<=redX+50 && y>=redY && y<=redY+50) && ((x<blueX || x>blueX+50) && (y<blueY || y>blueY+50))) { 
    				whichBox = 0;
    				dragging = true;
    			}
    			
    			else if((x>=blueX && x<=blueX+50 && y>=blueY && y<=blueY+50) && ((x<redX || x>redX+50) && (y<redY || y>redY+50))) {
    				whichBox = 1;
    				dragging = true;
    			}
    			
    			else if(x>=redX && x<=redX+50 && y>=redY && y<=redY+50 && x>=blueX && x<=blueX+50 && y>=blueY && y<=blueY+50) {
    				whichBox = 0;
    				dragging = true;
    			}
    			
    			else {
    				whichBox = 2;
    			}
    		}
    	}
    Java Code:
    public void mouseReleased(MouseEvent evt) {
    		dragging = false;
    	}
    Java Code:
    public void mouseDragged(MouseEvent evt) {
    		
    		if(!dragging) return;
    		
    		int x = evt.getX();
    		int y = evt.getY();
    		
    		if(whichBox == 0) {
    			redX = x;
    			redY = y;
    			repaint();
    		}
    		else if(whichBox == 1) {
    			blueX = x;
    			blueY = y;
    			repaint();
    		}
    		else { };
    		
    
    		
    	}
    Java Code:
    public DragingBoxesPanel() {
    		setBackground(Color.WHITE);
    		addMouseListener(this);
    		addMouseMotionListener(this);
    		redX = 5;
    		redY = 5;
    		blueX = 75;
    		blueY = 5;
    		whichBox = 0;
    		dragging = false;
    	}

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

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

    Default

    If I were to do this, I'd create a class called Box or SimpleBox that encapsulates all the characteristics of a box. Something like:
    Java Code:
    public class SimpleBox {
       private int x;
       private int y;
       private int width;
       private int height;
       private Color color;
       
       // constructors setters and getters go here
    
       // the box can paint itself
       public void paint(Graphics g) {
          g.setColor(color);
          g.fillRect(x, y, width, height);
       }
    
       // can check if it contains x & y.  Can also overload this and check
       // if it contains a Point
       public boolean contains(int x2, int y2) {
          // code to check if contains
       }
       
       public void move(int x, int y) {
          this.x = x;
          this.y = y;
       }
    
    }

    Then the DragBoxPanel can contain a collection of SimpleBoxes, and its paintcomponent method
    can simply ask the boxes to draw themselves:
    Java Code:
    public class DragBoxPanel extends JPanel{
       private List<SimpleBox> boxes = new ArrayList<SimpleBox>();
    
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (SimpleBox box : boxes) {
             box.paint(g);
          }
       }

  4. #4
    anilanar is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    Actually I'm learning java from web, and I don't know arrays yet.

    Here's DragingBoxesPanel (yea, it shoud've been Dragging-)

    rapidshare . com/files/276083966/DragingBoxesPanel.java

    There's no error actually, just the boxes don't move.

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

    Default

    Quote Originally Posted by anilanar View Post
    Actually I'm learning java from web, and I don't know arrays yet.
    Don't you think that you may be getting the cart before the horse if you're trying to learn complex GUI programming before learning basic Java? You may be setting yourself up for much disappointment by doing this. Just a thought.

  6. #6
    anilanar is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    I've been reading and doing exercises from the start of this page: math.hws.edu/javanotes
    You can check there and share your thoughts if you want :)

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

    Default

    I'll be danged -- they list GUI programming before they talk about arrays! Well go for it, but I have to admit that it seems a bit backwards to me.

  8. #8
    anilanar is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    Anyway, I just found what's wrong:

    Java Code:
    if((x>=redX && x<=redX+50 && y>=redY && y<=redY+50) && ((x<blueX || x>blueX+50) && (y<blueY || y>blueY+50))) { 
    				whichBox = 0;
    				dragging = true;
    			}
    that was wrong I should've done :

    Java Code:
    if((x>=redX && x<=redX+50 && y>=redY && y<=redY+50) && [B]((x<blueX || x>blueX+50) || (y<blueY || y>blueY+50))[/B]) { 
    				whichBox = 0;
    				dragging = true;
    			}
    I'm just a 2-3 sections away from arrays, so I'll learn them soon. Thank you :)

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

    Default

    You're welcome. Just for grins, here's an ArrayList way to do it:

    SimpleBox.java
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class SimpleBox {
       private int x;
       private int y;
       private int width;
       private int height;
       private Color color;
       
       public SimpleBox(int x, int y, int width, int height, Color color) {
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
          this.color = color;
       }
       public int getX() {
          return x;
       }
       public void setX(int x) {
          this.x = x;
       }
       public int getY() {
          return y;
       }
       public void setY(int y) {
          this.y = y;
       }
       public int getWidth() {
          return width;
       }
       public void setWidth(int width) {
          this.width = width;
       }
       public int getHeight() {
          return height;
       }
       public void setHeight(int height) {
          this.height = height;
       }
       public Color getColor() {
          return color;
       }
       public void setColor(Color color) {
          this.color = color;
       }
       
       public void paint(Graphics g) {
          g.setColor(color);
          g.fillRect(x, y, width, height);
       }
       
       public boolean contains(int x2, int y2) {
          if (x2 >= x && x2 <= x + width && y2 >= y && y2 <= y + height) {
             return true;
          } else {
             return false;
          }
       }
       
       public void deltaMove(int deltaX, int deltaY) {
          x += deltaX;
          y += deltaY;
       }
       
       public void move(int x, int y) {
          this.x = x;
          this.y = y;
       }
    
    }
    DragBoxPanel.java
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class DragBoxPanel extends JPanel {
       private static final Color[] COLORS = {
          Color.black, Color.white, Color.red, Color.orange, Color.yellow,
          Color.green, Color.blue, Color.magenta
       };
       private static final int SIDE = 50;
       private static final int MAIN_SIDE = 600;
       private static final Dimension MAIN_SIZE = new Dimension(MAIN_SIDE, MAIN_SIDE);
       private List<SimpleBox> boxes = new ArrayList<SimpleBox>();
       private Random random = new Random();
       
       public DragBoxPanel() {
          setPreferredSize(MAIN_SIZE);
          setBackground(Color.darkGray);
          for (Color color : COLORS) {
             int x = random.nextInt(MAIN_SIDE - SIDE);
             int y = random.nextInt(MAIN_SIDE - SIDE);
             SimpleBox box = new SimpleBox(x, y, SIDE, SIDE, color);
             boxes.add(box);
          }
          
          MyMouseListener mymListener = new MyMouseListener();
          addMouseListener(mymListener);
          addMouseMotionListener(mymListener);
       }
       
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (SimpleBox box : boxes) {
             box.paint(g);
          }
       }
       
       private class MyMouseListener extends MouseAdapter {
          private SimpleBox selectedBox = null;
          private Point delta = null;
          
          @Override
          public void mousePressed(MouseEvent e) {
             selectedBox = null;
             delta = null;
             for (int i = boxes.size() - 1; i >= 0; i--) {
                SimpleBox box = boxes.get(i);
                if (box.contains(e.getX(), e.getY())) {
                   selectedBox = box;
                   delta = new Point(e.getX() - box.getX(), e.getY() - box.getY());
                   return;
                }
             }
          }
          
          @Override
          public void mouseReleased(MouseEvent e) {
             selectedBox = null;
             delta = null;
          }
          
          @Override
          public void mouseDragged(MouseEvent e) {
             if (selectedBox != null) {
                selectedBox.move(e.getX() - delta.x, e.getY() - delta.y);
                DragBoxPanel.this.repaint();
             }
          }
       }
       
       private static void createAndShowUI() {
          JFrame frame = new JFrame("DragBoxPanel");
          frame.getContentPane().add(new DragBoxPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

Similar Threads

  1. Replies: 3
    Last Post: 08-20-2009, 03:04 AM
  2. Replies: 0
    Last Post: 04-05-2009, 02:22 PM
  3. Help! Need to randomly assign boxes
    By newb101 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 11:57 PM
  4. Limiting the Dragging area
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-30-2008, 04:51 PM
  5. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 01:17 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
  •