When writing a window program with a consistent change in Java, the usual approach is to overwrite the parent class's Paint (Graphics) method in the subclass and use the GUI function in the method to redraw the window. The window of a coherent transformation constantly calls the update (graphics) function, which automatically calls the paint (graphics) function. This will cause a flashing situation.
In order to solve this problem, double buffering technology can be applied. You can override the above procedure by overwriting the update (graphics) function, creating a graphic in memory that is the same size as the window, obtaining the graphics context (graphics) of the graphic, and then invoking the graphics context of the picture as an argument to paint (graphics) function (the GUI function in the paint (graphics) is drawn on the picture), and then the update (graphics) function calls the DrawImage function to draw the created graphic directly onto the window.
/** * Overwrite the Update method, intercept the default calling procedure */public void Update (Graphics g) {Image buffer = CreateImage (This.getwidth (), This.getheight () );//create graphics buffer Graphics Goff = Buffer.getgraphics ();//Gets the graphics context of the graphics buffer paint (goff);//Draw a drawing process with the paint method to plot the drawing buffer goff.dispose ();//Release Graphics context resource G.drawimage (buffer, 0, 0, this);//Draw the graphics buffer to the screen}
Eliminate flicker with dual buffering technology in Java