Results 1 to 14 of 14
  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Adding an ActionListener to a drawn Circle

    Hello all, I am trying to write a program that creates a random color circle in a random location and when the user clicks the circle with the mouse the circle disappears and reappear in another random location and another random color. My question is how do I add the NewCircleListener to the circle? Every time I try adding it it acts like the NewCircleListener does not exsit.Saying "cannot resolve NewCircleListener symbol", I am not looking for anyone to write my code for me just some pointers would be greatly appreciated.Thanks alot.


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/3/12
     * Time: 7:27 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise16_27 extends JFrame {
        NewPanel panel = new NewPanel();
        Exercise16_27()
        {
           add(panel);
        }
        public static void main(String[] args)
        {
            Exercise16_27 frame = new Exercise16_27();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setSize(600, 600);
            frame.setTitle("Circle Game");
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        }
        class NewPanel extends JPanel {
            int _xPoint = ((int)(Math.random()* 500));
            int _yPoint = ((int)(Math.random()* 500));
            NewPanel()
            {
    
            }
            protected void paintComponent(Graphics g)
            {
                int _radius = 10;
                super.paintComponents(g);
                g.fillOval(_xPoint, _yPoint, _radius, _radius);
                g.setColor(getRandomColor());
    
            }
    
            public Color getRandomColor()
            {
                Random numGen = new Random();
                return new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
            }
            class NewCircleListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    _xPoint = ((int)(Math.random()* 500));
                    _yPoint = ((int)(Math.random()* 500));
                    repaint();
    
                }
            }
    
        }
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Adding an ActionListener to a drawn Circle

    You can't just put an ActionListener on a circle, that doesn't make a ton of sense. Use a MouseListener on the JPanel and check the location of the click against any circles you want to monitor.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding an ActionListener to a drawn Circle

    Okay, I added the mouse Listener instaed of the NewCircleListener but it now draws the random circle when I click anywhare on the panel but doesnot remove the old circle
    and the color does not change.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/3/12
     * Time: 7:27 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise16_27 extends JFrame {
        NewPanel panel = new NewPanel();
        Exercise16_27()
        {
           add(panel);
        }
        public static void main(String[] args)
        {
            Exercise16_27 frame = new Exercise16_27();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setSize(600, 600);
            frame.setTitle("Circle Game");
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        }
        class NewPanel extends JPanel {
            int _xPoint = ((int)(Math.random()* 500));
            int _yPoint = ((int)(Math.random()* 500));
            NewPanel()
            {
              addMouseListener(new MouseListener() {
                  @Override
                  public void mouseClicked(MouseEvent mouseEvent)
                  {
                      _xPoint = ((int)(Math.random()* 500));
                      _yPoint = ((int)(Math.random()* 500));
                      repaint();
    
                  }
    
                  @Override
                  public void mousePressed(MouseEvent mouseEvent)
                  {
    
                  }
                  @Override
                  public void mouseReleased(MouseEvent mouseEvent)
                  {
                      repaint();
    
                  }
    
                  @Override
                  public void mouseEntered(MouseEvent mouseEvent)
                  {
    
                  }
    
                  @Override
                  public void mouseExited(MouseEvent mouseEvent) {
                      //To change body of implemented methods use File | Settings | File Templates.
                  }
              });
            }
            protected void paintComponent(Graphics g)
            {
                int _radius = 10;
                super.paintComponents(g);
                g.fillOval(_xPoint, _yPoint, _radius, _radius);
                g.setColor(getRandomColor());
    
            }
    
            public Color getRandomColor()
            {
                Random numGen = new Random();
                return new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
            }
            class NewCircleListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
    
    
                }
            }
    
        }
    
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Adding an ActionListener to a drawn Circle

    You need to call super.paintComponent() from your paintComponent() method, not super.paintComponents() - notice the extra 's'.

    You also aren't checking the mouse position against any circle positions, so that's going to trigger no matter where you click.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Adding an ActionListener to a drawn Circle

    You can't add an ActionListener to some random drawing; you should add a MouseListener to that JPanel and when its appropriate method is called, it should check where the mouse was pressed and take its action(s).

    kind regards,

    Jos

    edit: way too slow as always ...
    Last edited by JosAH; 06-04-2012 at 05:30 PM.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding an ActionListener to a drawn Circle

    Thanks guys.

  7. #7
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding an ActionListener to a drawn Circle

    I got it just one more question if you don't mind is my getRandomColor() not correct because its not changing colors.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/3/12
     * Time: 7:27 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise16_27 extends JFrame {
        NewPanel panel = new NewPanel();
        int _radius = 10;
        Exercise16_27()
        {
           add(panel);
        }
        public static void main(String[] args)
        {
            Exercise16_27 frame = new Exercise16_27();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setSize(600, 600);
            frame.setTitle("Circle Game");
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        }
        class NewPanel extends JPanel {
            int _xPoint = ((int)(Math.random()* 500));
            int _yPoint = ((int)(Math.random()* 500));
            NewPanel()
            {
              addMouseListener(new MouseListener() {
                  @Override
                  public void mouseClicked(MouseEvent mouseEvent)
                  {
                      if(insideCircle(mouseEvent.getX(), mouseEvent.getY()))
                      {
    
                        _xPoint = ((int)(Math.random()* 500));
                        _yPoint = ((int)(Math.random()* 500));
                        repaint();
                      }
                  }
    
                  @Override
                  public void mousePressed(MouseEvent mouseEvent)
                  {
    
                  }
                  @Override
                  public void mouseReleased(MouseEvent mouseEvent)
                  {
                      repaint();
    
                  }
    
                  @Override
                  public void mouseEntered(MouseEvent mouseEvent)
                  {
    
                  }
    
                  @Override
                  public void mouseExited(MouseEvent mouseEvent) {
                      //To change body of implemented methods use File | Settings | File Templates.
                  }
              });
            }
            protected void paintComponent(Graphics g)
            {
    
                super.paintComponent(g);
                g.fillOval(_xPoint, _yPoint, _radius, _radius);
                g.setColor(getRandomColor());
    
            }
            public boolean insideCircle(int px,int py)
            {
               return distance(px, py, _xPoint, _yPoint) < _radius;
            }
            public double distance(double x1, double y1, double x2, double y2)
            {
                return Math.sqrt((x2 -x1)* (x2 -x1) + (y2 - y1) * (y2 - y1));
            }
            public Color getRandomColor()
            {
                Random numGen = new Random();
                return new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
            }
    
        }
    
    }
    Last edited by aortell24; 06-04-2012 at 05:46 PM. Reason: fixed it

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

    Default Re: Adding an ActionListener to a drawn Circle

    Quote Originally Posted by aortell24 View Post
    I got it just one more question if you don't mind is my getRandomColor() not correct because its not changing colors.
    Oh, it is changing colors just fine -- but when do you call it in on the Graphics object in relation to when you use the Graphics object to draw something?
    Last edited by Fubarable; 06-04-2012 at 05:52 PM.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Adding an ActionListener to a drawn Circle

    You're setting the color after you draw the circle. That's a bit like dipping your brush in paint only after painting a picture.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding an ActionListener to a drawn Circle

    Duh, Thanks alot guys. I really appreciate all the help.You guys always answer so quick every other forum I belong to usually takes days to get any answers.

  11. #11
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding an ActionListener to a drawn Circle

    Sorry to bother you guys again but I am trinyg to add another circle for fun I got it to show up and change colors but it will not move do I need to add another mouse Listener?

    Java Code:
     import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Random;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/4/12
     * Time: 1:21 PM
     * To change this template use File | Settings | File Templates.
     */
    class NewPanel extends JPanel {
        int _xPoint = ((int)(Math.random()* 500));
        int _yPoint = ((int)(Math.random()* 500));
        int _xPoint1 = ((int)(Math.random()* 500));
        int _yPoint1 = ((int)(Math.random()* 500));
        int _radius = 30;
        NewPanel()
        {
            setBackground(Color.BLUE);
            addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent mouseEvent)
                {
                    if(insideCircle(mouseEvent.getX(), mouseEvent.getY()))
                    {
                        _xPoint = ((int)(Math.random()* 500));
                        _yPoint = ((int)(Math.random()* 500));
                        repaint();
                    }
    
    
    
                    if(insideCircle1(mouseEvent.getX(), mouseEvent.getY()))
                    {
                        _xPoint1 = ((int)(Math.random()* 500));
                        _yPoint1 = ((int)(Math.random()* 500));
                        repaint();
                    }
                }
                @Override
                public void mousePressed(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
                @Override
                public void mouseReleased(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
                @Override
                public void mouseEntered(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
                @Override
                public void mouseExited(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });
        }
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(getRandomColor());
            g.fillOval(_xPoint, _yPoint, _radius, _radius);
    
            g.setColor(getRandomColor());
            g.fillOval(_xPoint1, _yPoint1, _radius, _radius);
    
        }
        public boolean insideCircle(int px,int py)
        {
            return distance(px, py, _xPoint, _yPoint) < _radius;
        }
        public double distance(double x1, double y1, double x2, double y2)
        {
            return Math.sqrt((x2 -x1)* (x2 -x1) + (y2 - y1) * (y2 - y1));
        }
        public boolean insideCircle1(int px1,int py1)
        {
            return distance1(px1, py1, _xPoint, _yPoint) < _radius;
        }
        public double distance1(double x1, double y1, double x2, double y2)
        {
            return Math.sqrt((x2 -x1)* (x2 -x1) + (y2 - y1) * (y2 - y1));
        }
        public Color getRandomColor()
        {
            Random numGen = new Random();
            return new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
        }
    }

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Adding an ActionListener to a drawn Circle

    I think you should test your assumptions. Does your code really determine whether the mouse is inside a circle? Hint: How are the x and y values passed into fillOval() used? What about "radius"?

    You might also want to check out the Math class.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding an ActionListener to a drawn Circle

    Is this what you mean ?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Random;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/4/12
     * Time: 1:21 PM
     * To change this template use File | Settings | File Templates.
     */
    class NewPanel extends JPanel {
        int _xPoint = ((int)(Math.random()* 500));
        int _yPoint = ((int)(Math.random()* 500));
        int _xPoint1 = ((int)(Math.random()* 500));
        int _yPoint1 = ((int)(Math.random()* 500));
        int _radius = 30;
        NewPanel()
        {
            //setBackground(Color.BLUE);
            addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent mouseEvent)
                {
                    if(insideCircle(mouseEvent.getX(), mouseEvent.getY()))
                    {
                        _xPoint = ((int)(Math.random()* 500));
                        _yPoint = ((int)(Math.random()* 500));
                        repaint();
                    }
                    if(insideCircle1(mouseEvent.getX(), mouseEvent.getY()))
                    {
                        _xPoint1 = ((int)(Math.random()* 500));
                        _yPoint1 = ((int)(Math.random()* 500));
                        repaint();
                    }
                }
                @Override
                public void mousePressed(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
                @Override
                public void mouseReleased(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
                @Override
                public void mouseEntered(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
                @Override
                public void mouseExited(MouseEvent mouseEvent)
                {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });
        }
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(getRandomColor());
            g.fillOval(_xPoint - _radius, _yPoint - _radius,  _radius,  _radius);
    
            g.setColor(getRandomColor());
            g.fillOval(_xPoint1 - _radius, _yPoint1 - _radius, _radius, _radius);
    
        }
        public boolean insideCircle(int px,int py)
        {
            return distance(px, py, _xPoint, _yPoint) < _radius;
        }
        public double distance(double x1, double y1, double x2, double y2)
        {
            return Math.sqrt((x2 - x1) * (x2 -x1) + (y2 - y1) * (y2 - y1));
        }
        public boolean insideCircle1(int px,int py)
        {
            return distance1(px, py, _xPoint, _yPoint) < _radius;
        }
        public double distance1(double x1, double y1, double x2, double y2)
        {
            return Math.sqrt((x2 -x1) * (x2 -x1) + (y2 - y1) * (y2 - y1));
        }
        public Color getRandomColor()
        {
            Random numGen = new Random();
            return new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
        }
    }

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Adding an ActionListener to a drawn Circle

    Your insideCircle( ... ) and insideCircle1( ... ) methods aren't correct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. error when adding actionlistener
    By nananya in forum New To Java
    Replies: 19
    Last Post: 12-14-2011, 05:44 AM
  2. Need help adding an actionListener
    By HbJgd in forum New To Java
    Replies: 1
    Last Post: 06-12-2011, 05:33 PM
  3. Replies: 3
    Last Post: 10-20-2010, 06:24 AM
  4. adding a actionListener but not using inner class
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 10-08-2010, 07:24 AM
  5. problem with adding circle and rectangle
    By nicnicnic in forum Java 2D
    Replies: 3
    Last Post: 11-16-2009, 06:13 PM

Posting Permissions

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