Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Making Movable Boxes (Java)

Mon Nov 22, 2010 6:29 pm

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:
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
 
mortifiedPenguin
Gerbil Elite
Posts: 812
Joined: Mon Oct 08, 2007 7:46 pm

Re: Making Movable Boxes (Java)

Mon Nov 22, 2010 9:16 pm

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.
2600K @ 4.8GHz; XSPC Rasa/RX240/RX120 Phobya Xtreme 200; Asus P8Z68-V Pro; 16GB Corsair Vengeance 1333 C9; 2x7970 OC w/ Razor 7970; Force GT 120GB; 3x F3 1TB; Corsair HX750; X-Fi Titanium; Corsair Obsidian 650D; Dell 2408WFP Rev. A01; 2x Dell U2412m
 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Re: Making Movable Boxes (Java)

Mon Nov 22, 2010 9:58 pm

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?
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
 
way2strong
Gerbil
Posts: 51
Joined: Fri Jun 27, 2008 7:07 am

Re: Making Movable Boxes (Java)

Mon Nov 22, 2010 11:34 pm

No, they both start from the upper left corner
 
mortifiedPenguin
Gerbil Elite
Posts: 812
Joined: Mon Oct 08, 2007 7:46 pm

Re: Making Movable Boxes (Java)

Tue Nov 23, 2010 12:36 am

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.
2600K @ 4.8GHz; XSPC Rasa/RX240/RX120 Phobya Xtreme 200; Asus P8Z68-V Pro; 16GB Corsair Vengeance 1333 C9; 2x7970 OC w/ Razor 7970; Force GT 120GB; 3x F3 1TB; Corsair HX750; X-Fi Titanium; Corsair Obsidian 650D; Dell 2408WFP Rev. A01; 2x Dell U2412m

Who is online

Users browsing this forum: No registered users and 12 guests
GZIP: On