As an appletProgramAs shown in.
Key design ideas:
We need to set the coordinates of the circle center of a ball, preferably in the center of the window, and then increase the radius of the ball and re-draw it to achieve the animation of increasing the ball.
Design Scheme:
To draw a ball, we use a graphics class. The graphics class has the function of drawing.
Then we design a thread to control the playing of the animation. we reload the run () method to increase the ball radius and call the redrawing function of the form, redraw the form at a specified time.
Next we will implement the graphics G function to draw a color sphere, which is easy to do. We will use a specific color to cover a circular area of a certain size.
Below isSource code
Import Java. AWT. *; <br/> Import Java. applet. applet; <br/> Import javax. swing. japplet; </P> <p> public class circle extends japplet implements runnable {<br/> int X; // The Center X coordinate is X-r/2 <br/> int y; // The Center y coordinate is Y-r/2 <br/> int R; // radius </P> <p> private image IMG; <br/> private graphics graph; <br/> Public void Init () {<br/> dimension de = This. getsize (); <br/> X = de. height/2; <br/> Y = de. width/2; <br/> r = 1; <br/>}; <br/> Public void start () {<br/> // define a thread <br/> dimension de = This. getsize (); <br/> X = de. height/2; <br/> Y = de. width/2; <br/> r = 1; <br/> thread animation = new thread (this); <br/> animation. start (); <br/>}< br/> Public void stop () {</P> <p >}< br/> Public void destroy () {</P> <p >}< br/> Public void run () {<br/> dimension de1 = This. getsize (); <br/> while (true) {<br/> r + = 1; <br/> If (r> (de1.height/2) | r> (de1.width/2) {<br/> r = 1 ;;< br/>}< br/> repaint (); <br/> try <br/> {<br/> // 30 ms for thread suspension <br/> thread. sleep (30); <br/>}< br/> catch (interruptedexception ex) <br/>{< br/>}< br/> Public void paint (Graphics g) {<br/> // set the color of the ball <br/> G. setcolor (color. pink); <br/> // draw a solid circle from X-r/2, Y-r/2 <br/> G. filloval (X-r/2, Y-r/2, R, R ); <br/>}< br/> // double buffer mechanism <br/> Public void Update (Graphics g) <br/>{< br/> // initialize buffer <br/> If (IMG = NULL) <br/>{< br/> IMG = createimage (this. getsize (). width, this. getsize (). height); <br/> graph = IMG. getgraphics (); <br/>}< br/> // background clear screen, that is, set the same color and size of the ball components and the background <br/> graph. setcolor (getbackground (); <br/> graph. fillrect (0, 0, this. getsize (). width, this. getsize (). height); <br/> // draw the corresponding element component <br/> graph. setcolor (getforeground (); <br/> paint (graph); <br/> // re-draw the drawn circle on the screen <br/> G. drawimage (IMG, 0, 0, this); <br/>}< br/>}
I used the update (Graphics g) function of this program for reference.CodeFor smooth and non-flashing screen during repainting, I do not understand it very deeply.
In the run () method, I tried to use R to control the ball size. When the radius reaches the maximum range of the screen, I returned to the original size and started again, the running result is not as good as expected,
I would like to ask you to give me some advice.