please dont rip this site

Language Java Nosugar Graphics Common Threadedcanvas.java

//	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();
		}
	}
	
}


file: /Techref/language/java/nosugar/graphics/common/ThreadedCanvas.java, 3KB, , updated: 1997/8/25 12:17, local time: 2024/11/17 18:21,
TOP NEW HELP FIND: 
3.145.88.20:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/language/java/nosugar/graphics/common/ThreadedCanvas.java"> language java nosugar graphics common ThreadedCanvas</A>

Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .