120 likes | 276 Views
Roger L. Norton. Graphics in Java. Our Final Project. (replace link with final project). Roger L. Norton. Roger L. Norton. Double Buffering. Draw shapes onto a hidden image. Image is copied to the required canvas when needed. Double Buffering.
E N D
Roger L. Norton Graphics in Java Our Final Project (replace link with final project)
Roger L. Norton Roger L. Norton Double Buffering Draw shapes onto a hidden image Image is copied to the required canvas when needed
Double Buffering • An off-screen image (pixmap) is created, and all draw operations are done into this image. • Method update is overridden to avoid clearing the screen before paint is called. • For each step, clear the image and redraw all of the objects in the off-screen image. • In paint, the off-screen image is drawn onto the window.
Double Buffering The following instructions are needed to perform double buffering: (Assume that the image will be drawn on an extension of a canvas) Declarations Dimension offScreenDimension, d; Image offScreenImage; Graphics offScreenGraphics; Constructor public MyCanvas ( ) { d = getSize(); offScreenDimension = d; offScreenImage = createImage(d.width,d.height); offScreenGraphics = offScreenImage.getGraphics(); offScreenGraphics.setColor (getForeground()); ………….. }
Double Buffering method update //override update to avoid clearing the screen before calling paint publicvoid update (Graphics g) { paint (g); } method paint publicvoid paint (Graphics g) { //clear the canvas offScreenGraphics.setColor(getBackground()); offScreenGraphics.fillRect(0,0,d.width, d.height); //draw the shapes offScreenGraphics.setColor(getForeground()); for (int i = 0; i < shapeCount; i++) { s = (Shape)shapeList.elementAt(i); s.draw(offScreenGraphics); } g.drawImage(offScreenImage, 0, 0, this); }
Drawing Shapes DrawRect1 (Object Model) DrawRect2 DrawOval DrawFilledOval
Roger L. Norton Paint Programs DrawShapes1 DrawShapes2 DrawOval DrawFilledOval
Homework #4 Create a Java Applet that implements the above GUI.