Page 1 of 1

Making Movable Boxes (Java)

Posted: Mon Nov 22, 2010 6:29 pm
by Mondos
So basically I am so close to getting this simple program to work. What is does is create two rectangles and then you can click and drag them around the screen. The only problem is that I can not get the containsPoint(x, y) method to work.

Here is the Rectangle class (ignore the Circle class name)
import java.awt.*;

public class Circle{
   private int cornerX, cornerY, height, width;
   private Color color;
   
   public Circle(int x, int y, int x1, int y1, Color c){
      cornerX = x;
      cornerY = y;
      height = y1;
      width = x1;
      color = c;
   }
   public void draw(Graphics g){
      Color oldColor = g.getColor();
      g.setColor(color);
      g.drawRect(cornerX, cornerY, width, height);
      g.setColor(oldColor);
   }
   
   public void fill(Graphics g){
      Color oldColor = g.getColor();
      g.setColor(color);
      g.fillRect(cornerX, cornerY, width, height);
      g.setColor(oldColor);
   }
   
   public boolean containsPoint(int x, int y){
      int xWidth = x + cornerX;
      int yHeight = y - cornerY;      
      return (x <= xWidth && x >= cornerX && y >= yHeight && y <= cornerY) ;
   }
   
   public void move(int xAmount, int yAmount){
      cornerX =  cornerX + xAmount;
      cornerY =  cornerY + yAmount;
   }
}


Here is the colorpanel (note it chooses which box is selected by if the containsPoint method returns a true value)(mousePressed method)
//Example 6.8

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ColorPanel extends JPanel{
   private Circle c1, c2;
   private Circle selectedCircle;
   private int x, y;
   
   public ColorPanel(Color backColor){
      setBackground(backColor);
      c1 = new Circle(10, 20, 150, 80, Color.red);
      c2 = new Circle(170, 20, 100, 70, Color.blue);
      selectedCircle = null;
      addMouseListener(new PanelListener());
      addMouseMotionListener(new PanelMotionListener());
   }
   
   public void paintComponent(Graphics g){
      super.paintComponent(g);
      c1.fill(g);
      c2.draw(g);
   }
   
   private class PanelListener extends MouseAdapter{
      public void mousePressed(MouseEvent e){
         //Select a cirlce if it ontains the mouse coordinates
         x = e.getX();
         y = e.getY();
         if (c1.containsPoint(x, y))
            selectedCircle = c1;
         else if (c2.containsPoint(x, y))
            selectedCircle = c2;
      }
      
      public void mouseReleased(MouseEvent e){
         //Deselct the selected Circle
         selectedCircle = null;
      }
   }
   
   private class PanelMotionListener extends MouseMotionAdapter{
      
      public void mouseDragged(MouseEvent e){
         if (selectedCircle != null){
            //Computer distance and move the selected cirlce
            int newX = e.getX();
            int newY = e.getY();
            int dx = newX - x;
            int dy = newY - y;
            selectedCircle.move(dx, dy);
            x = newX;
            y = newY;
            repaint();

         }
      }
   }
}



And here is the final GUI Window
import javax.swing.*;      
import java.awt.*;         

public class GUIWindow{
   
   public static void main(String[] args){
      JFrame theGUI = new JFrame();
      theGUI.setTitle("Example 6.8");
      theGUI.setSize(300, 200);
      theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ColorPanel panel = new ColorPanel(Color.white);
      Container pane = theGUI.getContentPane();
      pane.setLayout(new GridLayout(1, 1));
      pane.add(panel);
      theGUI.setVisible(true);
   }
}


I don't know why it doesn't work. What the program does it select the right size box above the actual box (so an invisible box).Please help. :D

EDIT: Please, I need a response by tonight! :oops:

Re: Making Movable Boxes (Java)

Posted: Mon Nov 22, 2010 9:16 pm
by mortifiedPenguin
Before the actual solutions, I think your problem stems from the variable names you're using. The names "xWidth" and "yHeight" are probably poor names to do this calculation; They imply dimension instead of location and are probably the logical stumbling block you ran into. So, renaming them to something like "boundaryMinCoordX" and "boundaryMaxCoordX" (while a mouthfull) may be conducive to thinking in the right direction.

As for the actual code, your issues are:
1) Adding the wrong values to cornerX and cornerY to get xHeight and yHeight
2) The comparison direction for comparing y to yHeight and cornerY is incorrect

The solutions with spoiler tags:

Solution for 1:
When assigning xWidth and yHeight, use width and height instead of x and y. You also need to add to cornerY, not subtract to get the correct value.

Solution for 2:
Swap the comparison directions. y needs less than yHeight and greater than cornerY.

I've done the changes myself and they do work, although there is the slight chance something got written down wrong in the post itself. Let me know if you still have problems.

Re: Making Movable Boxes (Java)

Posted: Mon Nov 22, 2010 9:58 pm
by Mondos
Awesome man! I think my reasoning was off with were the staring coord is for the y value. It is a the lower right hand corner of the rect?

Re: Making Movable Boxes (Java)

Posted: Mon Nov 22, 2010 11:34 pm
by way2strong
No, they both start from the upper left corner

Re: Making Movable Boxes (Java)

Posted: Tue Nov 23, 2010 12:36 am
by mortifiedPenguin
Good to hear. And as way2strong already said, the starting coordinate for both window and rectangle start from the upper left hand corner. A good rule of thumb is that coordinate systems usually (if not always) start at the same point and move outwards from there. In windowing systems, the values are typically positive as they move away from the origin (of which, in my experience was usually in the upper left) regardless of whether it was going "down" or not, unlike the usual coordinate space where the values below the origin are negative.