// ThreadedCanvas class // part of the set of documents known as Java no sugar. // Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // import java.awt.*; public abstract class ThreadedCanvas extends Canvas implements Runnable { //################################################################### // instance variables //################################################################### Thread painter = null; Rectangle rect; Image mem_image; boolean clear_it = true; //################################################################### // things that must be implemented by subclass //################################################################### public abstract Dimension minimumSize(); public abstract void onRun(Graphics g); public abstract void onLoop( Graphics g); //################################################################### public Dimension preferredSize() { return minimumSize(); } //**************************************************************** public void set_clear_flag( boolean flag) { clear_it = flag; } //**************************************************************** public void reshape(int x, int y, int w, int h) { super.reshape(x,y,w,h); set_mem_image(w,h); } //******************************************************************* public void addNotify() { super.addNotify(); set_mem_image(minimumSize().width, minimumSize().height); } //******************************************************************* void set_mem_image( int w, int h) { //--------------------------------------------------------------- rect = new Rectangle(0,0,w, h); mem_image = createImage(w, h); clearCanvas(); } //******************************************************************* public Graphics getGraphics() { if (mem_image != null) return mem_image.getGraphics(); else return null; } //******************************************************************* public void paint(Graphics g) { g.drawImage(mem_image, 0, 0, this); } //******************************************************************* public void update(Graphics g) { paint(g); } //******************************************************************* public void clearCanvas() { Graphics g = getGraphics(); g.setColor(getBackground()); g.fillRect(0,0,rect.width, rect.height); g.setColor(Color.black); g.drawRect(0,0, rect.width-1, rect.height-1); } //################################################################### // THREADS //################################################################### public void start() { if (painter == null) painter = new Thread(this); if ( ! painter.isAlive() ) painter.start(); } //**************************************************************** public void stop() { if (painter != null) { painter.stop(); painter = null; } } //**************************************************************** public void run() { Graphics mem_g,screen_g; // ******************clear the drawing surface***************** mem_g = getGraphics(); screen_g = super.getGraphics(); onRun(mem_g); // ******************draw the selected shape************************* while (true) { if (clear_it) { clearCanvas(); } onLoop(mem_g); //repaint(); paint(screen_g); //------------now let other things happen--------------- Thread.yield(); } } }
.