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.
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();
}
}
}
}
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.
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.
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)
{
}
}
}
}
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.
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 ...
Re: Adding an ActionListener to a drawn Circle
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.
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));
}
}
}
Re: Adding an ActionListener to a drawn Circle
Quote:
Originally Posted by
aortell24
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?
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.
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.
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?
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));
}
}
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.
Re: Adding an ActionListener to a drawn Circle
Is this what you mean ?
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));
}
}
Re: Adding an ActionListener to a drawn Circle
Your insideCircle( ... ) and insideCircle1( ... ) methods aren't correct.
kind regards,
Jos