Importjava.awt.*;Importjavax.swing.*; Public classSkyplane { Public Static voidMain (string[] args) {JFrame W=NewJFrame (); W.setsize (824, 446); Mypanel MP=NewMypanel (); W.add (MP); Thread T=NewThread (MP); T.start (); W.show (); }}classMypanelextendsJPanelImplementsRunnable {Graphics G1; Image buffer, plane, sky; intx = 10; inty = 400; Public voidPaint (Graphics g) {Super. Paint (g); Buffer= CreateImage (GetWidth (), getheight ());//Creating an image bufferG1 = Buffer.getgraphics ();//Create plot AreaPlane =NewImageIcon ("Image/plane.png"). GetImage ();//Get ImagesSky =NewImageIcon ("Image/sky.png"). GetImage ();//Get Imagesg1.drawimage (Sky,0, 0, This);//draw an image to a buffer firstG1.drawimage (plane, x, Y, This);//draw an image to a buffer firstG.drawimage (buffer, 0, 0, This);//draw the image in the buffer } Public voidrun () { while(true) { if(x = = 800) x= 10; Elsex+ = 2; if(y = = 0) y= 400; Elsey--; Try{Thread.Sleep (50); } Catch(Exception e) {} repaint (); } }}
Java double-buffered drawing