Making Movable Boxes (Java)
Posted: 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)
Here is the colorpanel (note it chooses which box is selected by if the containsPoint method returns a true value)(mousePressed method)
And here is the final GUI Window
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.
EDIT: Please, I need a response by tonight!
Here is the Rectangle class (ignore the Circle class name)
Code: Select all
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)
Code: Select all
//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
Code: Select all
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.
EDIT: Please, I need a response by tonight!