ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm creating a java application with GUI which it draws 2 circles in a panel and when you click on a circle the color of the particular circle should change. I've got it so far to click inside the circle and change color but I call the repaint method to do that and as a result I get both circles changing a color
My code so far:
Code:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyPanel extends JPanel
{
private MyShapes circle1 = new MyShapes();
private MyShapes circle2 = new MyShapes();
private JTextField positions = new JTextField(10);
public MyPanel()
{
this.add(positions);
this.addMouseListener(new MouseListener()
{
@Override
public void mouseClicked(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
positions.setText("X:"+x+" Y:"+y);
//if((x >= circle1.getX() && x<=circle1.getX()+circle1.getWidth()) && (y >= circle1.getY() && y<=circle1.getY()+circle1.getHeight()))
if(Math.pow(x-(circle1.getX()+circle1.getRadius()), 2)+Math.pow(y-(circle1.getY()+circle1.getRadius()),2) <= Math.pow((double)(circle1.getWidth()/2), 2))
repaint();
if(Math.pow(x-(circle2.getX()+circle2.getRadius()), 2)+Math.pow(y-(circle2.getY()+circle2.getRadius()),2) <= Math.pow((double)(circle2.getWidth()/2), 2))
repaint();
}
@Override
public void mouseEntered(MouseEvent e){}
@Override
public void mouseExited(MouseEvent e){}
@Override
public void mousePressed(MouseEvent e){}
@Override
public void mouseReleased(MouseEvent e) {}
});
}
public void paint(Graphics g)
{
circle1.getCircle(50, 50, 40, 40, g);
circle2.getCircle(300, 50, 40, 40, g);
}
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class MyShapes
{
private int x, y, width, height;
private Color [] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};
Graphics g;
public void getCircle(int x, int y, int width, int height, Graphics g)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
setColor(g);
g.fillOval(x, y, width, height);
}
public void setColor(Graphics g)
{
Random generator = new Random();
int randomColor = generator.nextInt(colors.length);
g.setColor(colors[randomColor]);
}
// A method to return the x position of a shape object
public int getX()
{
return x;
}
// A method to return the y position of a shape object
public int getY()
{
return y;
}
// A method to return the width of a shape object
public int getWidth()
{
return width;
}
// A method to return the height of a shape object
public int getHeight()
{
return height;
}
// A method to return the radius of a circle
public double getRadius()
{
return (double)(width/2);
}
}
Code:
import javax.swing.JFrame;
public class showGUI2 extends JFrame
{
private MyPanel panel1 = new MyPanel();
public showGUI2(String title)
{
super(title);
this.setContentPane(panel1);
}
}
Code:
import javax.swing.JFrame;
public class Question2
{
public static void main(String[] args)
{
showGUI2 run = new showGUI2("Random colours");
run.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
run.setSize(400, 400);
run.setVisible(true);
}
}
Any suggestions of what I can do? An alternative way of repaint so I can get only the circle that I'm clicking to change color?
Hi,
You need to move the lines from your paint method to the mouseclicked method (within the corressponding if statement.
At the moment your logic is as follows
if the mouse is clicked within Circle A -> go to repaint
in the mouse is clicked within Circle B -> go to repaint
repaint repaints both circles each time
set you if statement as follows
if the mouse is clicked within Circle A
fill Circle A
repaint
if the mouse is clicked within Circle B
Fill Circle B
repaint
Yes but the problem is that paint is called on initialization and it needs a Graphics parameter. Which I don't have in the class.. Also I can only call such a method inside the listener, I can't implement it inside..
Sorry - forgot about the reference to g.
I guess if you have 2 variables colorA and colorB, set them on initialization and then reset the appropriate one in the mouseClick Event - in other words if the mouse click is within circle A then set colorA to a random value, otherwise leave it as is, same with colorB.
thanks for the reply but I managed with the help of a friend of course to do it in a different way. Actually I implemented my own MouseListener and I'm passing the shapes as a parameter and an integer identification of what type of a shape it is. Because I added 2 squares as well.
Also you need the Graphic reference to set the color again because the method is:
Code:
public void setColor(Graphics g)
{
...
g.setColor[...];
}
Here is the code in case you wonder:
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class MyShapes
{
private int x, y, width, height;
private Color [] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};
public MyShapes(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
/*setColor(g);
g.fillOval(x, y, width, height);*/
}
public void getCircle(Graphics g)
{
setColor(g);
g.fillOval(x, y, width, height);
}
public void getSquare(Graphics g)
{
setColor(g);
g.fillRect(x, y, width, height);
}
public void setColor(Graphics g)
{
Random generator = new Random();
int randomColor = generator.nextInt(colors.length);
g.setColor(colors[randomColor]);
}
// A method to return the x position of a shape object
public int getX()
{
return x;
}
// A method to return the y position of a shape object
public int getY()
{
return y;
}
// A method to return the width of a shape object
public int getWidth()
{
return width;
}
// A method to return the height of a shape object
public int getHeight()
{
return height;
}
// A method to return the radius of a circle
public double getRadius()
{
return (double)(width/2);
}
}
Code:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyPanel extends JPanel
{
private MyShapes circle1 = new MyShapes(50, 50, 40, 40);
private MyShapes circle2 = new MyShapes(300, 50, 40, 40);
private MyShapes square1 = new MyShapes(50, 300, 50, 50);
private MyShapes square2 = new MyShapes(300, 300, 50, 50);
private final int circle = 0;
private final int square = 1;
private JTextField positions = new JTextField(10);
public MyPanel()
{
this.add(positions);
this.addMouseListener(new MyListener(this, circle1, circle));
this.addMouseListener(new MyListener(this, circle2, circle));
this.addMouseListener(new MyListener(this, square1, square));
this.addMouseListener(new MyListener(this, square2, square));
}
public class MyListener implements MouseListener
{
JPanel panel;
MyShapes shape;
int x, y, onShape;
public MyListener(JPanel panel, MyShapes shape, int onShape)
{
this.panel = panel;
this.shape = shape;
this.onShape = onShape;
}
@Override
public void mouseClicked(MouseEvent e)
{
Graphics g = panel.getGraphics();
x = e.getX();
y = e.getY();
positions.setText("X:"+x+" Y:"+y);
if(onShape == 0)
{
if(Math.pow(x-(shape.getX()+shape.getRadius()), 2)+Math.pow(y-(shape.getY()+shape.getRadius()),2) <= Math.pow(shape.getRadius(), 2))
shape.getCircle(g);
} else if (onShape == 1)
{
if((x >= shape.getX() && x<=shape.getX()+shape.getWidth()) && (y >= shape.getY() && y<=shape.getY()+shape.getHeight()))
shape.getSquare(g);
}
}
@Override
public void mouseEntered(MouseEvent e){}
@Override
public void mouseExited(MouseEvent e){}
@Override
public void mousePressed(MouseEvent e){}
@Override
public void mouseReleased(MouseEvent e){}
}
public void paint(Graphics g)
{
circle1.getCircle(g);
circle2.getCircle(g);
square1.getSquare(g);
square2.getSquare(g);
}
}
Code:
import javax.swing.JFrame;
public class showGUI2 extends JFrame
{
private MyPanel panel1 = new MyPanel();
public showGUI2(String title)
{
super(title);
this.setContentPane(panel1);
}
}
Code:
import javax.swing.JFrame;
public class Question2
{
public static void main(String[] args)
{
showGUI2 run = new showGUI2("Random colours");
run.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
run.setSize(400, 400);
run.setVisible(true);
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.