-
MouseListener & GUI
Hey all, this is probably going to be the last question i'll have here for a while.
But I was wondering what's wrong with this code?
I need it for my programming class, and even my teacher doesn't know why it doesn't work.
So i'm relying on you guys to help.
here is the GUI class:
Code:
/**
* The GUI Window class.
*
* @Sebastian Wietecha
* @Version 1
*/
import javax.swing.*;
import java.awt.*;
public class GUIWindow
{
public static void main(String[] args){
JFrame theGUI = new JFrame();
theGUI.setTitle("GUI Program");
theGUI.setSize(300, 200);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorPanel panel = new ColorPanel(Color.white);
Container pane = theGUI.getContentPane();
pane.add(panel);
theGUI.setVisible(true);
}
}
Here is the ColorPanel class. (The one with the MouseListener in it. Causing the problem.)
Code:
/**
* The ColorPanel class for the GUIWindow class.
*
* @Sebastian Wietecha
* @Version 1.0
*/
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(200, 100, 25, Color.red);
c2 = new Circle(100, 100, 50, Color.blue);
selectedCircle = null;
addMouseListener(new PanelListener());
addMouseMotionListener(new PanelMotionListener()); [B]THIS IS THE PROBLEM![/B]
}
public void paintComponent(Graphics g){
super.paintComponent(g);
c1.fill(g);
c2.fill(g);
}
private class PanelListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
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){
x = e.getX();
y = e.getY();
selectedCircle = null;
}
private class PanelMotionListener extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
int newX = e.getX();
int newY = e.getY();
int dx = newX - x;
int dy = newY - y;
if (selectedCircle != null)
selectedCircle.move(dx, dy);
x = newX;
y = newY;
repaint();
}
}
}
}
Here is the Circle class.
Code:
/**
* The circle class.
*
* @Sebastian Wietecha
* @Version 1.0
*/
import java.awt.*;
public class Circle
{
private int centerX, centerY, radius;
private Color color;
public Circle(int x, int y, int r, Color c){
centerX = x;
centerY = y;
radius = r;
color = c;
}
public void draw(Graphics g){
Color oldColor = g.getColor();
g.setColor(color);
g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
g.setColor(oldColor);
}
public void fill(Graphics g){
Color oldColor = g.getColor();
g.setColor(color);
g.fillOval(centerX - radius, centerY - radius, radius*2, radius*2);
g.setColor(oldColor);
}
public boolean containsPoint(int x, int y){
int xSquared = (x - centerX) * (x - centerX);
int ySquared = (y - centerY) * (y - centerY);
int radiusSquared = radius * radius;
return xSquared + ySquared - radiusSquared <= 0;
}
public void move(int xAmount, int yAmount){
centerX = centerX + xAmount;
centerY = centerY + yAmount;
}
}
-
You say it doesn't work but never tell us how it doesn't work. Doing so would greatly help others help you. Much luck.
-
You have a scope issue: You have your PanelMotionListener class as a private inner class of your PanelListener class, and so it will be invisible outside of PanelListener. Don't do this. Make it an inner class of the ColorPanel class, fine, but it needs to be outside of PanelListener.