im trying to make a gui visualizer for a certine graph (only one graph ) and I want to visulize the travelling sales man problem on it , so first i will draw the graph and then I will solve it using some algorithims, im using swing and im drawing all the shapes (circles and lines) on a panel by overriding the paint method , what im trying to do is to change the color of any shape I want in the middle of my program
the problem im struggling with is how to change the color of any shape that I draw
,all my vertices will hava a shape object that i will add to the Graphics2D object of the panel that I will draw on, but how to change the color of only one shape from the many shapes that I draw on the same panel
shape dosn't have setcolor() methode and i can only change the
Graphics2D object color and if I did this the whole graph will be changed to the new color
I want to draw many shapes inside the JPanel and I want to be able to change the color of any shape I want during my program
public class Graphtesting {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(3);
My_Panel panel=new My_Panel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
int var=sc.nextInt();
if(var==0)
{
// i want to change the color of oval 2 only and not both
//panel.oval_1.setcolor() i want to do something like this but shape dosn't have a function to set color
// its like i want a seprate graphics2d object for each one of my shapes because i want to be able to change the color of each one individualy
}
else
{
// i want to change the color of oval 1 only and not both
}
}
}
class My_Panel extends JPanel
{
Ellipse2D.Double oval_1;
Ellipse2D.Double oval_2;
My_Panel()
{
setPreferredSize(new Dimension(500,500));
}
@Override
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g;
oval_1=new Ellipse2D.Double(10,10,50,50);
oval_2=new Ellipse2D.Double(60,60,50,50);
g2d.setColor(Color.orange);
g2d.fill(oval_2);
g2d.setColor(Color.red);
g2d.fill(oval_1);
}
}