Draw a line with Java
Wang Zhe, Mudanjiang, Heilongjiang Province
---- Java applets and Applications in Java are two different programming methods with great differences in structure and function. The Applet can be applied to web pages to make colorful page effects, Adding Luster to the website; the Application can compile various applications like other programming languages (such as VB and VC.
---- This article will discuss the first case. Draw a K-line chart using Java Applet on the web page.
---- The "K" line is a parameter indicator in the stock market analysis. It is drawn using the data such as daily stock renewal, maximum price, lowest price, closing price, and transaction volume, in combination with the five-day and ten-day moving averages, the stock's ups and downs can be reflected in a phase.
---- The K-line chart made by Java Applet is mostly applied to securities-type websites to facilitate analysis and operation of online investors. However, due to the speed of China's network, some securities websites have made K-line charts into images to save download time. Of course, the tools (Java Applet) used to draw the keline are the same as the drawing method.
---- There are several key issues to solve when writing a Java Applet for drawing a keline, namely: 1) multithreading; 2) layout manager selection; 3) data input and output stream applications; 4) set and listen to mouse and keyboard events.
---- The following describes the entire programming process and method through an example.
---- I. Multithreading
---- A complete Java Applet contains four methods: Init (), Start (), Stop (), Destroy () (I .e. initialization, Start, Stop, and clear ), it constitutes a complete life cycle, and its running order is also executed from top to bottom.
---- In the process of drawing a K-line chart, in addition to drawing forms and Yin and Yang-line rectangles representing stock movements, you must also listen to mouse events and draw a freely moving "Cross" cursor at the same time, in order to avoid blinking and reduce the waiting time for recalculation, in addition to the "Cross" cursor that follows the mouse movement, the layout of the page, the Yin line, the Yang line rectangle, and so on are not re-painted. This requires the use of multiple threads to complete this task.
---- 1. Some basic concepts about multithreading:
---- Multithreading actually starts another process. The running process is independent of the main program, loaded from the Start () method of the main program, and called and executed by the Run () method. There are two methods to implement multithreading: Create a subclass of the Thread class (class inheritance) and implement the runnable interface.
---- For ease of use, Java encapsulates all functions related to threads into a class in the Java class library: Java. lang. Thread. Through this class, Java can create, control, and terminate threads to complete all operations related to multithreading.
---- In Java, a subclass can only inherit one superclass (parent class). Because the Java Applet we want to write is applied to webpages, we must first inherit the browser class (java. therefore, in this example, we implement multiple threads by implementing the runnable interface. The implementation statement is as follows: