Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
How to use Multiple Paint Components at the same time in java

I'm making a GUI with java which represents the Longest Path Problem I'm using Graphics 2D to draw the vertices and the links between them. I've made a button which calculates the longest path between two vertices I want to make a link that grows slowly so, I've used Thread.sleep() method to make that but when I use it the whole program becomes very slow and lagging. I've Overriden the paintComponent method to draw and I can't use Multi threading because that makes problems with EDT (Event Dispatch Thread). I've saw other topics for Multiple paint Components but nothing helped me.Any ideas or help??

public void paintComponent(Graphics g)
{   
    super.paintComponent(g);
    if(!dragging&&MainFrame.ActiveAddNode)
    g.drawRect(X,Y, 20, 20);//Virtual rectangle for the vertex
    p=new Polygon();
    for(int i=0;i<Node.size();i++)
    {
        p.addPoint(Node.get(i).X+10, Node.get(i).Y+10);
        g.setColor(Node.get(i).c);
        g.fillRect(Node.get(i).X, Node.get(i).Y, 20, 20);//represents the graph vertex
        g.setColor(Color.CYAN);
        g.drawString(""+i, Node.get(i).X+7,  Node.get(i).Y+15);//the number of the vertex

    }
for(int i=0;i<Node.size();i++)
    for(int j=0;j<Node.get(i).Children.size();j++){
        g.setColor(Color.BLUE);
        for(int k=0;k<link.size()-1;k++)
        {
            if(link.get(k)==i&&link.get(k+1)==Node.get(i).Children.get(j)){
                g.setColor(Color.RED);//Highlight the Longest path link
            }
        }

        g.drawLine(Node.get(i).X+10, Node.get(i).Y+10, Node.get(Node.get(i).Children.get(j)).X+10
                ,  Node.get(Node.get(i).Children.get(j)).Y+10);
        int Xsum=(Node.get(i).X+Node.get(Node.get(i).Children.get(j)).X)/2;
        int Ysum=(Node.get(i).Y+Node.get(Node.get(i).Children.get(j)).Y)/2;
        g.drawString(""+Node.get(i).Edges.get(j),Xsum,Ysum);

    }//end of 2nd loop
}//end of paint component
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I've used Thread.sleep()

Don't use Thread.sleep(...) when code is executing on the Event Dispatch Thread (EDT). This will prevent the GUI from painting itself.

The point of the paintComponent(...) method is to paint the current state of the component.

So you need a method(s) that will allow you to dynamically alter the state of the component. So maybe you have a property like: setPathLength(...). So once you calculate the longest path you know how many iterations are required to display the entire path.

Then you need to use a Timer to schedule your animation. Every time the Timer fires you need to increase the "path length" by 1 and then repaint() your component. So each time one more node is painted.

Read the section from the Swing tutorial on How to Use Swing Timers for more information.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!