Results 1 to 3 of 3
Thread: MouseListener & GUI
- 10-26-2009, 10:42 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
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:
Here is the ColorPanel class. (The one with the MouseListener in it. Causing the problem.)Java 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 Circle class.Java 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(); } } } }
Java 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.
Similar Threads
-
mouseListener trouble please help!
By linux1man in forum AWT / SwingReplies: 6Last Post: 06-03-2009, 01:57 AM -
Mouselistener questions
By jigglywiggly in forum New To JavaReplies: 0Last Post: 05-07-2009, 09:59 PM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 03:30 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 08:10 AM -
I need help with my MouseMotionAdapter and MouseListener.
By MurderfaceX4 in forum New To JavaReplies: 1Last Post: 12-07-2007, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks