Results 1 to 3 of 3
  1. #1
    Suurisa is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default 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:

    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 ColorPanel class. (The one with the MouseListener in it. Causing the problem.)

    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();
                }
            }
        }
        
    }
    Here is the Circle class.

    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;
        }
        
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. mouseListener trouble please help!
    By linux1man in forum AWT / Swing
    Replies: 6
    Last Post: 06-03-2009, 01:57 AM
  2. Mouselistener questions
    By jigglywiggly in forum New To Java
    Replies: 0
    Last Post: 05-07-2009, 09:59 PM
  3. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 AM
  4. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 AM
  5. I need help with my MouseMotionAdapter and MouseListener.
    By MurderfaceX4 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 03:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •