|
|
The Off Screen Graphics ContextSome students ask me about an example on Off Screen GC and Scrolling String without blinking. The main idea about using an Off Screen GC is as follow: /* you create an image of the size you want inside applet. */ Image img = createImage(w, h); /* Create an off screen GC associate with your image */ Graphics offscr = img.getGraphics(); /* using the off screen GC and drawing something on it */ /* when you draw on GC, you actually draw on your image */ offscr.setColor(Color.black); offscr.fillRect(0, 0, w, h); .... /* make sure your dispose the GC after you finish using it */ offscr.dispose(); /* after your image is prepared and ready, you draw your image on screen in paint or update method */ Example on Off Screen GCThe following is an example that use the off screen GC to show a scrolling sting upon stars background.
Source code : Scroll.java |