Results 1 to 14 of 14
- 06-04-2012, 04:35 PM #1
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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(); } } } }
- 06-04-2012, 04:49 PM #2
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 06-04-2012, 04:51 PM #3
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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) { } } } }
- 06-04-2012, 04:58 PM #4
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 06-04-2012, 05:00 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
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.
Build a wall around Donald Trump; I'll pay for it.
- 06-04-2012, 05:10 PM #6
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
Re: Adding an ActionListener to a drawn Circle
Thanks guys.
- 06-04-2012, 05:39 PM #7
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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
-
Re: Adding an ActionListener to a drawn Circle
Last edited by Fubarable; 06-04-2012 at 05:52 PM.
- 06-04-2012, 05:50 PM #9
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 06-04-2012, 05:55 PM #10
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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.
- 06-04-2012, 08:43 PM #11
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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)); } }
- 06-04-2012, 08:53 PM #12
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 06-04-2012, 09:33 PM #13
Member
- Join Date
- Jun 2012
- Location
- St.louis, Missouri
- Posts
- 49
- Rep Power
- 0
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)); } }
- 06-04-2012, 09:41 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
Similar Threads
-
error when adding actionlistener
By nananya in forum New To JavaReplies: 19Last Post: 12-14-2011, 05:44 AM -
Need help adding an actionListener
By HbJgd in forum New To JavaReplies: 1Last Post: 06-12-2011, 05:33 PM -
how to make the circle visible clearly, which is drawn by using Graphics object
By prasad.vara in forum AWT / SwingReplies: 3Last Post: 10-20-2010, 06:24 AM -
adding a actionListener but not using inner class
By hariza in forum AWT / SwingReplies: 2Last Post: 10-08-2010, 07:24 AM -
problem with adding circle and rectangle
By nicnicnic in forum Java 2DReplies: 3Last Post: 11-16-2009, 06:13 PM
Bookmarks