Import java.awt.*; Import Java.util.*; Import Javax.swing.*; Public Class SleepMethodTest Extends JFRAME { / **** / Private Static Final Long Serings IALVERSIONUID = 1L; Private Thread T; // Define the color array Private Static Color [] color = {color.black, color.blue, color.cyan, color.green, color.orange, color.yllow, color.red, color.light_gray}; Vate Static Final Random Rand = New Random (); // Create a random object Private Static Color Getc () {// method of getting the random color value. hream ( new runnable () {// Create anonymous thread object int x = 30; // Define the initial coordinates int y = 50; public void run () {// cover thread interface method whole (true) {// unlimited cycle try {thread .sleep (100); // thread sleeps 0.1 seconds} Catch (InterruptedException E) {e.printstacktrace ();} // Get component graphics context object graphics graphics = getGraphics (); GRAP. hics.setcolor (getc ());/ / Set drawing color // Draw straight lines and increase vertical coordinates graphics.Drawline (x, y, 100, y ++); if (y> = 80) {y = 50;}}});/);/ / Start thread} Public Static Void Main (String [] ARGS) {Init (New SleepMethodtest (), 100, 100);} // The method of initialization program interface Public Static Void (JFREAME FRAME, INT WID TH, int Height) {{ Frame.setDefaultCloseoperation (jframe.exit_on_close); frame.setsize (width, height); frame.setvisible (true); The dormant in Java is the SLEEP () method. This example defines the Getc () method. This method is used to randomly generate the object of the color type, and use the getGraphics () method to obtain the Graphics object in the anonymous internal class of the thread. This object calls the setcolor () method to set the color to the graphics; call the drawline () method to draw a line segment, and the line segment will automatically adjust according to the change of the vertical coordinate.