please dont rip this site

Language Java Nosugar Graphics ME Code Mecanvas.java

//	MeCanvas clas
//	part of the set of documents known as Java no sugar.
//	Copyright (c) 1996,1997 Sunil Gupta, sunil@magnetic.demon.co.uk
//	placed into the public domain by the author
//
import java.awt.*;
import java.net.*;
import java.io.*;
import Context;

public class MeCanvas extends ThreadedCanvas
{
	//*******************************************************************
	// globals
	//*******************************************************************
	static final int MAX_DURATION =5;
	
	static final int DOING_NOTHING = -1;
	static final int LOADING_IMAGES = 0;
	static final int IMAGES_OK = 2;	
	
	static final int LOOK_LEFT =0;
	static final int LOOK_RIGHT =1;
	static final int TONGUE =2;
	static final int HIT =3;
	static final int AAGH =4;
	static final int SMUG1 =5;
	static final int SMUG2 =6;
	
	static final int N =  0;
	static final int NE = 1;
	static final int E =  2;
	static final int SE = 3;
	static final int S =  4;
	static final int SW = 5;
	static final int W =  6;
	static final int NW = 7;
	static final int STOP = 8;
	
	static final int IMAGE_WIDTH = 118;
	static final int IMAGE_HEIGHT = 186;
	
	static boolean images_loaded = false;
	static String status = "";
	static int doing_what = DOING_NOTHING;
	
	static Image img_look_left, img_look_right, img_tongue;
	static Image img_hit, img_aagh, img_smug1, img_smug2;
	static Image img_ne_aagh, img_nw_aagh, img_sw_aagh, img_se_aagh;
	static Image img_eyebrowse_aagh, img_hair_aagh, img_specs_aagh, img_mouth_aagh;
	
	static Image normal_images[] = null;
	static int current_id = 1;
	
	//*******************************************************************
	// instance variables
	//*******************************************************************
	int which_image = LOOK_LEFT;
	int x,y, direction, duration, time_spent;
	int explode_x, explode_y;
	boolean exploding = false, imploding = false;
	int explode_counter = 0;
		
	//###################################################################
	// Paint stuff
	//###################################################################
	public void addNotify()
	{
		Thread image_loader;
		
		super.addNotify();

		// -------load images asynchronously-----------
		if (doing_what == DOING_NOTHING)
		{
			image_loader = new Thread(this);
			image_loader.start();
		}
	}
			
	//*******************************************************************
	public Dimension minimumSize() 	
	{
		return ( new Dimension(200,200));
	}
	
	//*******************************************************************
	public boolean mouseDown( Event ev, int mouse_x, int mouse_y)
	{
		Rectangle rect;
		
		if (exploding)
			set_status ("Can't you see I'm breaking apart?");
		else
		{
			rect = new Rectangle(x,y,IMAGE_WIDTH, IMAGE_HEIGHT);
			if ( rect.inside(mouse_x, mouse_y))
			{
				set_status ("Hit");
				exploding = true;
				imploding = false;
				explode_counter = 0;
				explode_x = x + IMAGE_WIDTH/2;
				explode_y = y + IMAGE_HEIGHT/2;
			}
			else
				set_status ("You missed");
		}
		return true;
	}

	//###################################################################
	// THREADS 
	//###################################################################
	public void onRun( Graphics g)
	{
		x = (int) (Math.random() * size().width);
		y = (int) (Math.random() * size().height);
		choose_direction();
	}
			
	//*******************************************************************
	public void run()
	{
		if (doing_what == DOING_NOTHING)
		{
			doing_what = LOADING_IMAGES;
			try
			{
				load_images();
			}
			catch (Exception e) {}
		}
		else
			super.run();
	}
	
	//*******************************************************************
	public void onLoop( Graphics g)
	{
		Image img;
		int image_num, random;
		int font_width, font_height, font_x, font_y;
		FontMetrics metrics;

		if (doing_what != IMAGES_OK)
		{
			metrics = g.getFontMetrics();
			font_width = metrics.stringWidth(status);
			font_height = metrics.getHeight();
			
			random = (int) Math.round(Math.random() * 4.0);
			switch (random)
			{
				case 1:	
					g.setColor(Color.red);
					break;
				case 2:
					g.setColor(Color.blue);
					break;
				case 3:
					g.setColor(Color.green);
					break;
				default:
					g.setColor(Color.black);
			}
			
			font_x = size().width - font_width;
			font_y = (size().height - font_height)/2;
			g.drawString(status, font_x, font_y);
		}
		else if (exploding || imploding)
		{
			onExplodeLoop(g);
		}
		else
		{
			//----------------choose the image-------------
			image_num = choose_image(direction);
		
			//------------draw it-------------------------
			img = normal_images[image_num];
			g.drawImage( img,x,y,null);
		
			//---------get ready for next image-----------
			move_image();
		
			time_spent ++;
			duration --;
			if (duration <= 0)	choose_direction();
		}
		
		//----------------sleep-------------------------------
		try
		{
			Thread.sleep(50);
		}
		catch (Exception e){}
	}
	
	//*******************************************************************
	public void onExplodeLoop( Graphics g)
	{
		int delta;
		
		//-------------------draw the peices in place------------------
		if (explode_counter < 2)
		{
			g.drawImage( img_hit,x,y,null);
		}
		else if (explode_counter < 6)
		{
			g.drawImage( img_aagh,x,y,null);
		}
		else
		{
			delta = (explode_counter-5) * (explode_counter-5) * 2;
			g.drawImage(img_ne_aagh, explode_x-delta, explode_y+delta, null);
			g.drawImage(img_nw_aagh, explode_x+delta, explode_y+delta, null);
			g.drawImage(img_sw_aagh, explode_x-delta, explode_y+delta, null);				
			g.drawImage(img_se_aagh, explode_x-delta, explode_y-delta, null);
			g.drawImage(img_eyebrowse_aagh, explode_x+delta, explode_y, null);
			g.drawImage(img_hair_aagh, explode_x, explode_y-delta, null);
			g.drawImage(img_specs_aagh, explode_x-delta, explode_y, null);
			g.drawImage(img_mouth_aagh, explode_x, explode_y+delta, null);
		}
		
		//-------------------get ready for next step-------------------
		if (exploding)
		{
			explode_counter ++;
			if (explode_counter >= 15)
			{
				exploding = false;
				imploding = true;
				x = (int) (Math.random() * size().width);
				y = (int) (Math.random() * size().height);
				explode_x = x;
				explode_y = y;
			}
		}
		else
		{
			explode_counter --;
			if (explode_counter <= 0)
			{
				imploding = false;
				choose_direction();
			}
		}
	}
	
	//###################################################################
	// stuff
	//###################################################################
	void load_images() throws Exception
	{
		URL dir_url, img_url;
		MediaTracker tracker = new MediaTracker(this);
		
		System.out.println("Called load_images");
		
		//------------load main pictures---------------
		if (!images_loaded)
		{
			dir_url = new URL(Context.code_base, "pics/" );
		
			img_url = new URL(dir_url, "looklef.gif");
			img_look_left = getImage(img_url, tracker);
			img_url = new URL(dir_url, "lookrit.gif");
			img_look_right = getImage(img_url, tracker);
			img_url = new URL(dir_url, "tongue.gif");
			img_tongue = getImage(img_url, tracker);
			img_url = new URL(dir_url, "hit.gif");
			img_hit = getImage(img_url, tracker);
			img_url = new URL(dir_url, "aagh.gif");
			img_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "smug.gif");
			img_smug1 = getImage(img_url, tracker);
			img_url = new URL(dir_url, "smug2.gif");
			img_smug2 = getImage(img_url, tracker);
		
			//------------------populate array--------------------	
			normal_images = new Image[7];
			normal_images[0] = img_look_left;
			normal_images[1] = img_look_right;
			normal_images[2] = img_tongue;
			normal_images[3] = img_hit;
			normal_images[4] = img_aagh;
			normal_images[5] = img_smug1;
			normal_images[6] = img_smug2;
	
			//------------load aargh pictures---------------
			dir_url = new URL(Context.code_base,"aaghpart/" );
			
			img_url = new URL(dir_url, "aagh_ne.gif");
			img_ne_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "aagh_nw.gif");
			img_nw_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "aagh_sw.gif");
			img_sw_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "aagh_se.gif");
			img_se_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "eyebrows.gif");
			img_eyebrowse_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "hair.gif");
			img_hair_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "specs.gif");
			img_specs_aagh = getImage(img_url, tracker);
			img_url = new URL(dir_url, "mouth.gif");
			img_mouth_aagh = getImage(img_url, tracker);
		
			images_loaded = true;
			doing_what = IMAGES_OK;
			set_status("Images were fine");
		}
	}
	
	//*******************************************************************
	synchronized int get_id()
	{
		int old_id;
		
		old_id = current_id;
		current_id ++;
		return old_id;
		
	}

	//*******************************************************************
	synchronized Image getImage( URL url, MediaTracker tracker) throws Exception
	{
		int ID;
		Image image;
		

		Thread.yield();
		
		// -----------------------------------------------
		// tell the toolkit to load an image, no point checking for
		// null as the images are loaded asynchronously
		image = getToolkit().getImage(url);
		
		// -----------------------------------------------
		// you have to use a media tracker to find out whether
		// the image was loaded
		
		ID = get_id();
		tracker.addImage(image, ID);
		tracker.waitForID(ID);
		if ( tracker.isErrorID(ID) )
		{	
			set_status("Unable to or couldnt find image:\n '" + url +"'");
			throw new IOException("Unable to or couldnt find image: '" + url +"'") ;
		}
		
		// -----------------------------------------------
		set_status( url);
		return image;
	}
	
	//*******************************************************************
	int choose_image( int the_direction)
	{
		int image_num, mutate_image;
		double normality_level;
		
		//------------------current image--------------------
		switch (the_direction)
		{
			case N:
			case S:
				image_num = SMUG1;
				break;
				
			case NE:
			case SE:
			case E:
				image_num = LOOK_LEFT;
				break;
				
			case NW:
			case W:
			case SW:
				image_num = LOOK_RIGHT;
				break;
				
			default:
				image_num = SMUG2;
				break;
		}
		
		//----------------random one -------------
		normality_level = Math.random() * 5.0;
		if (normality_level <= 1.0)
		{
			mutate_image = (int) Math.round(Math.random() * 2.0);
			switch (mutate_image)
			{
				case 0:
					image_num = LOOK_LEFT;
					break;
				case 1:
					image_num = LOOK_RIGHT;
					break;
				case 2:
					image_num = TONGUE;
					break;
			}
			
		}
		
		//----------------and the jury says--------
		return image_num;
	}
	
	//*******************************************************************
	void move_image()
	{
		int delta;
		delta = 4 * time_spent;
		switch (direction)
		{
			case N:	 			y+=delta; 	break;
			case NE: x+=delta;	y+=delta; 	break;
			case E:	 x+=delta; 				break;
			case SE: x+=delta;	y-=delta; 	break;
			case S:	 			y-=delta; 	break;
			case SW: x-=delta;	y-=delta;	break;
			case W:  x-=delta; 				break;
			case NW: x-=delta;	y+=delta; 	break;
		}
		
		if (	(x <= 0) || ( (x+IMAGE_WIDTH) >= size().width))
			x = (int) Math.round(Math.random() * (size().width - IMAGE_WIDTH) );
		
		if (	(y <= 0) || ( (y+IMAGE_HEIGHT) >= size().height))
			y = (int) Math.round(Math.random() * (size().height - IMAGE_HEIGHT));
	}
	
	//*******************************************************************
	void choose_direction()
	{
		direction = (int) Math.round((Math.random() * STOP));
		duration = (int) (Math.random() * MAX_DURATION);
		time_spent = 1;
	}
	
	//*******************************************************************
	void set_status(String message)
	{
		status = message;
		System.out.println(message);
	}
}


file: /Techref/language/java/nosugar/graphics/me/code/MeCanvas.java, 12KB, , updated: 1997/8/25 12:29, local time: 2024/11/4 13:41,
TOP NEW HELP FIND: 
3.149.23.247:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©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://techref.massmind.org/techref/language/java/nosugar/graphics/me/code/MeCanvas.java"> language java nosugar graphics me code MeCanvas</A>

Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .