Java animation programming foundation fourth part

zhaozj2021-02-08  472

Using image: Now, we will overwrite PaintFrame () to move the image. This also brings some problems, the image is often quite large, and it is tuned by a little bit, which draws all the images will spend a lot of time, especially through a slower connection, this is why DrawImage takes four parameters The reason, where the fourth parameter is an ImageOBserver object. Get image by calling getImage (). Moving an image on the screen: World.gif As the background, Car.gif acts as a moving object, and is drawn twice, causing a two cars game. Image world; image car; public void init () {string str = getParameter ("fps"); int FPS = (STR! = Null)? Integer.Parseint (STR): 10; DELAY = (FPS> 0)? 1000 / fps): 100; World = GetImage (getCodeBase (), "world.gif"); car = getimage (getCodeBase (), "car.gif");} public void Paint (Graphics g) {Update (g) } public void PainTframe (graphics g) {Dimension D = size (); int w = world.getwidth (this); int h = world.getHeight (this); if ((w> 0) && (h> 0) ) {g.drawImage (world, (D.Width - W) / 2, (D.HEIGHT - H) / 2, this);} w = car.getwidth (this); h = car.getheight (THIS); IF ((w> 0) && (h> 0)) {w = d.width; g.drawImage (car, d.width - (Frame * 5)% w), (D.Height - h) / 3, this); g.drawImage (CAR, D. Width - ((Frame * 7)% W), (D.Height - h) / 2, this);}} Display a series of images: through each frame Display an image to create an animation. We still use a double buffer method to reduce flicker. The reason is that some of the images we have displayed are transparent, so it is necessary to remove the current erase the current, if the technique that does not use double buffer will cause flashing. Image frame []; public void init () {string str = getParameter ("fps"); int FPS = (STR! = Null) Integer.Parseint (STR): 10; DELAY = (fps> 0)? (1000 / fps): 100; frames = new image [10]; for (int i = 0; i <10; i ) {frames [i] = getImage (), "duke / t" i ". GIF ");}} public void Paint (Graphics g) {Update (g);} public void PaintFrame (graphics g) {g.drawimage (frames [frame% 10], 0, 0, null);}

转载请注明原文地址:https://www.9cbs.com/read-878.html

New Post(0)