0

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);
}
}
  • 2
    “i can only change the Graphics2D object color and if I did this the whole graph will be changed to the new color” …You can call g.setColor or g.setPaint multiple times in a single painting method. You can change the color before you draw each shape. I suspect you are currently storing only a list of shapes, without color information, but since you haven’t shown us any code, I can only guess about that. – VGR May 19 '23 at 18:08
  • I know that I can use g.setcolor before drawing any shape but how to change that color later for only one shape not all of them , I have the reference for all the shapes but they all share the same color variable , for example I want to change the color of the second shape only not all the shapes – Mowafk Mha May 19 '23 at 19:09
  • Edit your question and show the code for your painting method. We cannot answer without seeing how you store your shapes. – VGR May 19 '23 at 19:44
  • I added an example of what I'm trying to do – Mowafk Mha May 19 '23 at 21:15
  • `Ellipse2D.Double oval_1;` cannot by itself record the color. Why not add a `Color oval_color_1;` field, and use that in your paintComponent method? (By the way, your paintComponent methods needs to call super.paintComponent(g); before doing anything else. See https://docs.oracle.com/javase/tutorial/uiswing/painting/ for an explanation.) – VGR May 19 '23 at 21:18
  • 1
    Keep an ArrayList of objects to be painted. This way you can keep the properties such as the shape and color of the object. See: [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/) for more information and working examples. – camickr May 19 '23 at 21:26
  • For [example](https://stackoverflow.com/questions/34168628/gui-application-that-allows-the-user-to-choose-the-shape-and-color-of-a-drawing/34168841#34168841) – MadProgrammer May 19 '23 at 22:40

0 Answers0