Home > Back-end >  Java multi-thread and graphic problem, ask for help!
Java multi-thread and graphic problem, ask for help!

Time:11-15

The import tool. *;
Import the Java. The awt. *;
The import javax.mail. Swing. *;
Public class start {
Public static void main (String [] args) {
The test p1=new test (0, 50);



Test the p2=new test (0100);


The test p3=new test (0150);
The test p4=new test (0200);



Thread pt1=new Thread (p1);
Thread pt2=new Thread (p2);
Thread the pt3=new Thread (p3);
Thread pt4=new Thread (p4);
JFrame frame=new JFrame ();



Frame. The setSize (400300);
Frame. SetLocationRelativeTo (null);
Frame. SetVisible (true);
Frame. SetDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);

Frame. The add (p1);
Frame. The add (p2);
Frame. The add (p3);
Frame. The add (p4);
Pt1. Start ();
Pt2. Start ();
The pt3. Start ();
Pt4. Start ();
}
///////////////////////////////////////////////////////////////////////////////
The package tool;
The import javax.mail. Swing. *;
Import the Java. The awt. *;

Public class test extends JPanel implements Runnable {
int x,y;
The public test (int x, int y) {
This.=x x;
This. Y=y;
}
Public void paint (Graphics g) {
Super. Paint (g);
G.d rawOval (x, y, 20, 20);
}

Public void the run () {
While (x<300) {





X=x + (int) ((Math) random () * 10) + 1);

Enclosing repaint ();
Try {
Thread.sleep (150);
}
Catch InterruptedException (e) {
e.printStackTrace(); }
}

}
}


Multiple small ball movement, why only the last thread running, other all don't show

CodePudding user response:

Panel use has a problem, you are inside the Panel drawing, the final Panel the front Panel of the blocked, you can either draw five goals in a Panel, or dynamically change the position of the Panel instead of a Panel,
  • Related