Multi-thread exercises for beginners 3-mobile text, multi-thread for beginners 3-Text

Source: Internet
Author: User

Multi-thread exercises for beginners 3-mobile text, multi-thread for beginners 3-Text

The frame-by-frame animation in Flash can achieve the text moving display effect, and the java program can also achieve similar results.

1. The Ticker class inherits Canvas and implements the Runnable interface. Use the paint method in the Canvas to control the position of the string to move.

2. The TickerFrame class inherits the Frame class, implements the ActionListener window, and adds two buttons to control the movement and stop of text.

Core part:

The string is about to exit the display area. You need to reset the reduced x coordinate value. Since the x coordinate value at that time is equal to the text width, this means that the width of the string needs to be determined. The string width depends on the selected font, font size, and appearance. The FontMetrics class is used to obtain information about a specific font and measure the size of a string in a specific font. However, this class is an abstract class and cannot be instantiated. Therefore, you should also use the getFontMetrics method to obtain references to the FontMetrics object.

The Code is as follows:

1 import java. awt. *; 2 import java. awt. event. *; 3 class Ticker extends Canvas implements Runnable 4 {5 // define the string position coordinates 6 private int x, y; 7 // define Integer Variables to store the string height, width 8 private int height, width; 9 private Thread runner = null; 10 // defines the String 11 private String text = null; 12 // The FontMetrics class can measure the character size of a specific font, but it is an abstract class and cannot be directly instantiated. 13 14 private FontMetrics metrics = null; 15 private Font font = new Font ("Monospaced", Font. BOLD, 50); 16 17 public Ticker (String _ text) 18 {19 text = _ text; 20 // use the getFontMetrics method, returns the reference of a FontMetrics object 21 metrics = getFontMetrics (font); 22 // The height is equal to the height of the font 23 height = metrics. getHeight (); 24 // The width is equal to the width of the string 25 width = metrics. stringWidth (text); 26 27 x = getSize (). width; 28 y = height; 29 30 // the size of Ticker should be at least 31 larger than the size of the text (width, height + 2 ); 32 33} 34 // custom Thread startup Method 35 public void start () 36 {37 if (runner = null) 38 {39 runner = new Thread (this ); 40 runner. start (); 41} 42} 43 // custom method for stopping a thread 44 public void stop () 45 {runner = null ;} 46 47 // run method 48 public void run () 49 {50 Thread currentThread = Thread. currentThread (); 51 while (runner = currentThread) 52 {53 // custom method, so that the string moves to the left coordinate to change 54 computeCoordinates (); 55 // re-paint 56 repaint (); 57 try 58 {59 runner. sleep (30); 60} 61 catch (InterruptedException ie) 62 {63 System. err. println ("Error:" + ie); 64} 65} 66} 67 // override the painting () method of the parent class 68 public void paint (Graphics g) 69 {70g. setColor (Color. blue); 71g. setFont (font); 72g. drawString (text, x, y); 73} 74 75 private void computeCoordinates () 76 {77 // reset 78 if (x <-width) When all text is moved out of the window) 79 x = getSize (). width; 80 else 81 // coordinate reduction, which has reached the result of moving the string to the left 82 x-= 2; 83 84} 85 86} 87 public class TickerFrame extends Frame implements ActionListener 88 {
// Define two buttons 89 private Button start = new Button ("Start"); 90 private Button stop = new Button ("Stop ");
// Create a Ticker Class Object 91 Ticker ticker = new Ticker ("learn in practice! ");
// In the constructor, initialize the window 92 public TickerFrame () 93 {94 super ("moving text"); 95 setLayout (new BorderLayout ()); 96 add ("Center", ticker );
// Create a new container 97 Panel p = new Panel ();
// Set to stream layout 98 p. setLayout (new FlowLayout ());
// Add the button to the container and register the listener 99 p for the button. add (start); start. addActionListener (this); 100 p. add (stop); stop. addActionListener (this); 101 add ("South", p );
// Use an anonymous internal class to register the listener for the window. 102 addWindowListener (new WindowAdapter () 103 {104 public void windowClosing (invalid wevent we) 105 {106 System. exit (0); 107} 108}); 109 pack ();
// Make the window visible 110 setVisible (true); 111}
// Implement the abstract method of the interface and process the event 112 public void actionreceivmed (ActionEvent AE) 113 {114 if (AE. getSource () = start) 115 ticker. start (); 116 else117 if (AE. getSource () = stop) 118 ticker. stop (); 119 120} 121 public static void main (String [] args) 122 {new TickerFrame ();} 123}

Run:

Click Start to move the text to the left. Click Stop to Stop the text. When all the text is moved outside the window, the text is reset and moved to the left again.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.