Java-based animation programming foundation

xiaoxiao2021-03-06  62

Source: Java-CN Basic Technique:

There are many ways to achieve animations in Java, but the basic principles they achieve are the same, that is, draw a series of frames to create a feeling of motion on the screen.

Let's construct a program framework, slowly expand, make it more complete.

Use threads:

For multiple update screens per second, you must create a thread to implement a loop of the animation, this loop to track the current frame and respond to periodic screen update requirements. There are two ways to implement threads, you can create a derived class of THREAD, or attached to the interface of Runnable.

An easy-to-criminal error is to loop the animation in Paint (), which occupies the primary AWT thread, and the main thread will be responsible for all drawings and event processing.

A framework applet is as follows:

public class Animator1 extends java.applet.Applet implements Runnable {int frame; int delay; Thread animator; public void init () {String str = getParameter ( "fps"); int fps = (! str = null) Integer.parseInt? (STR): 10; delay = (fps> 0)? (1000 / fps): 100;} public vois start () {Animator = new thread (this); Animator.Start ();} public void run () { while (Thread.currentThread () == animator) {repaint (); try {Thread.sleep (delay);} catch (InterruptedException e) {break;} frame ;}} public void stop () {animator = null;} }

This reference is in your html file:

The above parameter fps represents the number of frames per second

Keep a constant frame speed:

In the above example, Applet is only a fixed time between each two frames, but this has some shortcomings, sometimes you will wait a long time, for ten frames of images per second, should not sleep 100 milliseconds, because in the middle Also spent time.

Here is a simple remedy:

Public void Run () {longTM = system.currenttimemillis (); while (thread.currentthread () == animator) {repaint (); try {tm = delay; thread.sleep (math.max (0, TM - System.currentTimeMillis ()));} catch (interruptedException e) {breaf;} frame ;}}

Draw each frame: The rest is to draw each frame image. The Applet repainT () is called in the above example to draw each frame image.

Public Void Paint (Graphics G) {g.setcolor (color.black); g.drawstring ("frame" frame, 0, 30);} Generate graphics: Now let's draw some slightly difficult things. The next example draws a combination of sinusoidal curves, for each X, painted a short vertical line, all of which makes a graphic, and each frame changes. But unfortunate some flashing, after we will explain why flashes and how to avoid it. Public Void Paint (Graphics G) {Dimension D = Size (); INT H = D.Height / 2; for (int x = 0; x

Public Void Paint (Graphics G) {Update (G);} public void update (graphics g) {color bg = getBackground (); dimension D = size (); int h = d.Height / 2; for (int x = 0; x Y2) {INT T = Y1; Y1 = Y2; Y2 = T;} g.SetColor (BG); g.drawline (x, 0 , x, y1); g.drawline (x, y2, x, d.height); g.setcolor (color.black); g.drawline (x, y1, x, y2);}