/* * BufferCanvas * * Version 1.1 - Java 1.1 compatible * * Dec 10 2001 * * Copyright 2001 David Joiner and the Shodor Education Foundation */ import java.awt.*; /** * BufferCanvas is a canvas with double buffering */ public abstract class BufferCanvas extends Canvas { /* * Member variables */ protected Image im; protected Graphics buf=null; protected int imWidth=100; protected int imHeight=20; private boolean clearScreen=true; /* * Constructors */ public BufferCanvas () { super(); checkSize(); } /* * Painting methods */ public void update(Graphics g) { // check to see if the component has been implemented or resized if (checkSize()) { repaintBuffer(); } if (!paintingBuffer) { g.drawImage(im,0,0,this); } else { repaint(2); } } boolean paintingBuffer = false; public abstract void paintBuffer(Graphics buf); public void repaintBuffer() { if (buf != null) { paintingBuffer = true; if(clearScreen) { Color fgColor = buf.getColor(); buf.setColor(getBackground()); buf.fillRect(0,0,imWidth,imHeight); buf.setColor(fgColor); } paintBuffer(buf); paintingBuffer = false; repaint(); } } public void paint(Graphics g) { update(g); } public boolean checkSize() { // check to see if size data has changed and that it is usable Dimension size=getSize(); if (imWidth==size.width && imHeight==size.height) { if (size.height>0 && size.width>0 && buf==null) { imWidth = size.width; imHeight = size.height; readyBuffer(); return true; } else { return false; } } else { if (size.height>0 && size.width>0) { imWidth = size.width; imHeight = size.height; readyBuffer(); return true; } else { return false; } } } public int getWidth() { return getSize().width; } public int getHeight() { return getSize().height; } public void readyBuffer() { if (buf!=null) buf.dispose(); im=createImage(imWidth,imHeight); buf=im.getGraphics(); repaint(); } public void thickLine(Graphics g, int x1,int y1,int x2,int y2) { g.drawLine(x1,y1,x2,y2); g.drawLine(x1+1,y1,x2+1,y2); g.drawLine(x1,y1+1,x2,y2+1); g.drawLine(x1-1,y1,x2-1,y2); g.drawLine(x1,y1-1,x2,y2-1); } }