Java thread control image segmentation and synthesis
Sohong Light Stone Ling Shu Wei Gao Zhenong Abstract: Split the web still image using Java, then synthesize the display according to the user's interactive operation, and use the thread to control the concurrency of the program to achieve the dynamic and interactive effect of the image display. . Keywords: Java Language Class Class Thread Java is an object-oriented programming language. It has the characteristics of platform-independent, object-oriented, dynamic, security, etc., allowing multiple threading methods to be programmed directly to concurrent control. Java also supports distributed network operations, which can easily access the access of the network file object. With the drawing feature provided by the Java language itself, some simple graphics can be drawn. For complex graphics, the image is generally made by drawing software, and then the image is downloaded and processed and controlled by the method provided by Java, thereby realizing the dynamic display of still images. 1 Java Image Processing and Thread Connection Control 1.1 Java Image Processing Java Language provides a rich class, interface, and corresponding call methods (METHOD). Use these classes or interfaces, you can define your own class or subclass to make full use of the Java object-oriented features to program. In the Java.awt package, special Image classes are provided, which is an abstract class that provides abstract ways to draw some common features of the image. Two GetImage () methods are available in the Applet class and Tookit class, and the images you want to download are queried according to the absolute address and relative address. The download method of relative addresses is usually used. Its syntax definition and functions are as follows: 1.Public Image GetImage (String Name) Function: Download images according to the relative address. Parameters: URL - URL (Uniform Resource Location) Base Address, Name - Image File Name. After getting image, you can call the DrawImage () method provided by the Graphics class to display the image. 2. Public DrawImage (Image IMG, INT X, INT Y, OBSERVER) Function: Display images at the specified location. Parameters: IMG - A image, X-horizontal coordinate, Y-intertwind, OBSERVER - image monitor, is used to monitor the download of the image, accept image loading information (return True when the image is fully loaded, Otherwise returning false). When displays an image, it is often desirable to achieve the control of the image to achieve media playback in various ways you like. To this end, Java provides an ImageOBServer class and a MediaTracker class for tracking multimedia objects including images and sounds, which are mainly used in this program that tracking the MediaTracker class that tracks multiple image status. 1.2 Java multi-threaded computing features Currently, threads have been used for many operating systems and application development systems. The thread is a single control flow of the program, with the characteristics of the sequential program, but the thread is not 1 program, which is just a program's execution sequence. The thread has a strong concurrent function, and there may be multiple threads at the same time at the same time. Threads are dynamic, have a certain life cycle, and experience the process of being created, executed, blocked until dieting. A special THREAD class is provided in the Java language to support direct multi-threaded programming. The Thread class provides a control method for threads, such as start (), STOP (), Run (), Suspend (), resume (), SLEEP (), and Run () methods, etc., which can control the state of threads. . And use the setPriority () method to set the running priority order of the thread.
The definition method of the Thread class is as follows: Thread (ThreadGroup Group, Runable Target, String Name) setPriority () is used to set the priority of the thread. The thread priority is an integer between MinPriority (defined in the class) and the MaxPriority (defined in the class). Thread different priorities determine switching between different threads. 2 Java image segmentation and synthetic algorithm and implementation Java programs first download a complete image, then divide them into 20 unit puzzles, ie, divided into 5 lines 4 columns. In this case, the 20th image unit is specifically set to 1 blank image to use the user interactive operation when puzzle. These parameters are defined in the corresponding variables, respectively. Final int xcells = 5; // Each line of puzzle Final Int Ycells = 4; // Each column puzzle number final int allcells = 20; // Split Empty = 19; // Put 20 unit Cells [19] is set to // blank image then stores these image segmentation units in 1 Cell class cells [], where the Cell class contains images and its starting position and current location, which specifically defines As follows: Class Cell {Int SX, Sy; // Start Position INT CX, CY; // Current Position Image IMG; // Unit Image Public Cell (Image IMG, INT X, INT Y) // Cell Class Construction Function { THIS.IMG = IMG; SX = X; Sy = Y;} // Ges to the start position to X, Y} In order to track each image division unit, it is also necessary to establish an entity of 1 MediaTracker class (Instance) Then, then call the AddImage () method to specify one unique identifier for each image to be tracked. The identifier determines the priority order of the image acquisition and causes the image independently. Mediatracker Tracker = NEW Mediatracker (this) // establishes 1 Mediatracker entity for the current use class, used to track the image Cells [EMPTY] = New Cell (Createempty (), Topoint (Empty) .x, Topoint (EMPTY) .X, TOPOINT Empty) .y); tracker.addimage (cells [Empty] .img, 0); // Call the createempty () method generates a blank image and adds 20 unit Void SetPosition in the tracking // Cells array (INT X, INT Y) // Setting the unit image Current position {CX = x; cy = y;}
The position of each image unit is stored in the position array Position: int position [] [] = new int [xcells] [Ycells] can be used to operate the various units of the image using the cells of the image, thereby images of each unit image Synthesize display and change the position of each unit via the POSITION [] [] array. In order to perform concurrent operations for each unit, the thread is required to be controlled, and the start, sleep, and other state changes by the mouse event and the button event control process, the method is as follows (only Run () method as an example): Thread ImageThread = NULL; // Define thread ImageThread, initial // Value is empty public void Run () {imageThread.SetPriority (thread.minPriority); // Set line // process priority Try {imageThread.sleep (2000); / / Thread Sleep Waiting 2000ms} Catch (InterruptedException E) {} first = CHANGEARRAY (); // Call ChangeArray () method Random change map // Limit cell position while (! Loaded) // Judgment image If it is not tracked, Then call the related / / method to track and load the image {repaint (); try {imagerethread.sleep (100);} catch (interruptedException e) {system.out.println (e);}}} ChangeArray () method is used to random The location of the image unit is changed, and the method is as follows: boolean changearaRay () {int source [] = new int [20]; int full [] = new int [20]; for (int i = 0; i