I'm writing this Java code from a video tutorial in youtube on making Java game applets. However, the instructor didn't really explain how an applet's update method does double-buffering as he calls it.
import java.awt.*; import java.applet.Applet; public class Game extends Applet implements Runnable { private Image i; private Graphics g2; private int x, y, dx = 1, dy = 1, radius = 10; ... public void update(Graphics g) { if ( i == null ) { i = createImage(getHeight(), getHeight()); } g2 = i.getGraphics(); g2.setColor(getBackground()); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setColor(getForeground()); paint(g2); g.drawImage(i, 0, 0, null); } public void paint(Graphics g) { x+= dx; y+= dy; g.setColor(Color.RED); g.fillOval( x, getHeight() - y, radius * 4, radius * 4); g.setColor(Color.BLUE); g.fillOval( getWidth() - x - radius, y, radius * 4, radius * 4); g.setColor(Color.GREEN); g.fillOval( x, y, radius * 4, radius * 4); g.setColor(Color.YELLOW); g.fillOval( getWidth() - x , getHeight() - y , radius * 4, radius * 4); }
How is flickering being removed here? What is the use of having an Image object? What is the use of having another Graphics object, why not use the parameter Graphics?