please dont rip this site

Language Java Nosugar Graphics Draw Code Drawselectorcanvas.java

// 
//	DrawSelectorCanvas 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.*;
import java.applet.*;

public class DrawSelectorCanvas  extends ThreadedCanvas
{
	//*******************************************************************
	// globals
	//*******************************************************************
	static final int draw3DRect =2;
	static final int drawArc = 3;
	static final int drawLine = 4;
	static final int drawOval =5;
	static final int drawPolygon = 6;
	static final int drawRect =7;
	static final int drawRoundRect = 8;
	static final int fill3DRect = 9;
	static final int fillArc = 10;
	static final int fillOval = 11;
	static final int fillPolygon = 12;
	static final int fillRect = 13;
	static final int fillRoundRect = 14;
	
	//*******************************************************************
	// instance variables
	//*******************************************************************
	Color black = new Color(0,0,0);
	String draw_mode;
	int drawmode = drawLine;
		

	//*******************************************************************
	// public interface
	//*******************************************************************
	public void draw_shapes( String shapename)
	{
		draw_mode = shapename;
		start();
	}
		
	//*******************************************************************
	// Paint stuff
	//*******************************************************************
	public Dimension minimumSize() 	
	{
		return ( new Dimension(200,200));
	}
	
	
	//*******************************************************************
	// THREADS 
	//*******************************************************************
	
	public void onRun( Graphics g)
	{
		if 	( draw_mode.equals("draw3DRect")	)	{ drawmode = draw3DRect; }
		else if ( draw_mode.equals("drawArc")		)	{ drawmode = drawArc; }
		else if ( draw_mode.equals("drawLine")	)	{ drawmode = drawLine; }
		else if ( draw_mode.equals("drawOval")	)	{ drawmode = drawOval; }
		else if ( draw_mode.equals("drawPolygon"))	{ drawmode = drawPolygon; }
		else if ( draw_mode.equals("drawRect")	)	{ drawmode = drawRect; }
		else if ( draw_mode.equals("drawRoundRect"))	{ drawmode = drawRoundRect; }
		else if ( draw_mode.equals("fill3DRect")	)	{ drawmode = fill3DRect; }
		else if ( draw_mode.equals("fillArc")		)	{ drawmode = fillArc; }
		else if ( draw_mode.equals("fillOval")	)	{ drawmode = fillOval; }
		else if ( draw_mode.equals("fillPolygon")	)	{ drawmode = fillPolygon; }
		else if ( draw_mode.equals("fillRect")	)	{ drawmode = fillRect; }
		else if ( draw_mode.equals("fillRoundRect"))	{ drawmode = fillRoundRect; }
		else
		{
			stop();
		}
	}
			
	public void onLoop( Graphics g)
	{
		// -------------use a random belchy colour scheme-------------------
		g.setColor(random_colour() );
			
		// ----------------------draw the selected shape--------------------
		switch(drawmode)
		{
			case draw3DRect:	draw3DRects(g, false);	break;
			case fill3DRect:	draw3DRects(g, true);	break;
			case drawArc:		drawArcs(g,false);		break;	
			case fillArc:		drawArcs(g, true);		break;
			case drawLine:		drawLines(g);			break;
			case drawOval:		drawOvals(g, false);	break;
			case fillOval:	   drawOvals(g, true);		break;
			case drawPolygon:	drawPolygons(g, false);	break;
			case fillPolygon:   drawPolygons(g, true);	break;
			case drawRect:		drawRects(g, false);	break;
			case fillRect:	   drawRects(g, true);		break;
			case drawRoundRect:	drawRoundRects(g, false);	break;
			case fillRoundRect: drawRoundRects(g, true); 	break;
		} //switch
	}
	
	//*******************************************************************
	// RANDOM THINGS
	//*******************************************************************
	private Rectangle random_rectangle()
	{
		Rectangle rect;
		Point a,b;
		int x1,y1,x2,y2;
		
		a = random_point();
		b = random_point();
		
		x1 = Math.min(a.x, b.x);
		x2 = Math.max(a.x, b.x);
		y1 = Math.min(a.y, b.y);
		y2 = Math.max(a.y, b.y);
		
		rect = new Rectangle(x1,y1,x2-x1, y2-y1);
		return(rect);
	}
	
	private Point random_point()
	{
		Point point;
		
		point = new Point(0,0);
		point.x = (int) (	Math.random() * rect.width	);
		point.y = (int) (	Math.random() * rect.height  );
		return (point);
	}
	
	private Color random_colour()
	{
		Color color;
		int red, green, blue;
		
		  red = (int) ( 255.0 * Math.random() );
		green = (int) ( 255.0 * Math.random() );
		 blue = (int) ( 255.0 * Math.random() );
		
		color = new Color(red, green, blue);
		return (color);
	}
	
	//*******************************************************************
	// the bits that do the drawing.
	//*******************************************************************
	private void draw3DRects( Graphics g, boolean filled )
	{
		Rectangle rect;
		rect = random_rectangle();
		
		if (filled)
			g.fill3DRect(rect.x, rect.y, rect.width, rect.height,true);
		else
			g.draw3DRect(rect.x, rect.y, rect.width, rect.height,true);
	}
	
	private void drawArcs( Graphics g , boolean filled)
	{
		Rectangle rect;
		int start_angle, arc_angle;
		
		rect = random_rectangle();
		start_angle = (int)(360.0 * Math.random());
		arc_angle = (int)(360.0 * Math.random()) - start_angle;
		
		if (filled)
			g.fillArc(rect.x, rect.y, rect.width, rect.height, start_angle, arc_angle);
		else
			g.drawArc(rect.x, rect.y, rect.width, rect.height, start_angle, arc_angle);
	}
	
	private void drawLines( Graphics g )
	{
		Point a,b;
		
		a = random_point();
		b = random_point();
		g.drawLine(a.x, a.y, b.x, b.y);
	}
	
	private void drawOvals( Graphics g , boolean filled)
	{
		Rectangle rect;
		rect = random_rectangle();
		if (filled)
			g.fillOval(rect.x, rect.y, rect.width, rect.height);
		else
			g.drawOval(rect.x, rect.y, rect.width, rect.height);
	}
	
	private void drawPolygons( Graphics g, boolean filled )
	{
		Polygon poly;
		int n_segments, segment;
		Point origin;
		double offset_angle, segment_angle;
		double this_angle, this_length, dx,dy;
		
		
		n_segments = 10 +  (int)(  10 * Math.random()  );
		poly = new Polygon();
		origin = random_point();
		segment_angle = 2 * Math.PI / n_segments;
		
		offset_angle = 0.0;
		for (  segment=0; segment < n_segments; segment++)
		{
			this_angle = segment_angle * Math.random();
			this_length = 100 * Math.random();
			
			dx = this_length * Math.cos( this_angle + offset_angle);
			dy = this_length * Math.sin( this_angle + offset_angle);
			
			poly.addPoint( origin.x + (int)dx, origin.y + (int)dy );
			
			offset_angle += segment_angle;
		}
		if (filled)
			g.fillPolygon(poly);
		else
			g.drawPolygon(poly);

	}
	
	private void drawRects( Graphics g, boolean filled)
	{
		Rectangle rect;
		rect = random_rectangle();
		
		if (filled)
			g.fillRect(rect.x, rect.y, rect.width, rect.height);
		else
			g.drawRect(rect.x, rect.y, rect.width, rect.height);
	}
	
	private void drawRoundRects( Graphics g, boolean filled )
	{
		Rectangle rect;
		rect = random_rectangle();
		int arc_width, arc_height;
		
		arc_width = (int) ((rect.width/4.0) * Math.random());
		arc_height = (int) ((rect.height/4.0) * Math.random());
		
		if (filled)
			g.fillRoundRect(rect.x, rect.y, rect.width, rect.height, arc_width, arc_height);
		else
			g.drawRoundRect(rect.x, rect.y, rect.width, rect.height, arc_width, arc_height);
	}
	
	
}


file: /Techref/language/JAVA/nosugar/graphics/draw/code/DrawSelectorCanvas.java, 7KB, , updated: 1997/5/31 23:50, local time: 2024/12/29 07:14,
TOP NEW HELP FIND: 
3.128.201.71: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://sxlist.com/TECHREF/language/JAVA/nosugar/graphics/draw/code/DrawSelectorCanvas.java"> language JAVA nosugar graphics draw code DrawSelectorCanvas</A>

Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .