⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tool.java

📁 包含多个java程序
💻 JAVA
字号:
//Tool.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * Tools
 * @author  
 * @version 1.0 05/24/03
 *
 * Tool is a builder pattern,and the product is a Graphic.
 */

abstract class	Tool
{
	protected	AShape	sp;
	protected 	boolean	flage;
	protected	Graphics	g = null;

	public	Tool(AShape s) {sp = s;}

	public	void	reset() {flage=false;}
	public	void	setColor(Color c) {sp.setColor(c);}
	public	void	setSize(int size) {}
	public	void	setGraphics(Graphics g) {this.g = g;}

	public	boolean	mouseDown(int x, int y) {
		sp.setFirstPoint(x, y);
		flage = true;
		return false;
	}
	abstract public	boolean	mouseDrage(int x, int y);
	abstract public	boolean	mouseUp(int x, int y);
	abstract public	void	draw(Graphics g);

	abstract public	Graphic	getProduct();
}

/**
 * the product is a AShape
 */
class	NormalTool extends Tool
{
	public	NormalTool(AShape s) {super(s);}

	public	boolean	mouseDrage(int x, int y) {
		sp.setSecondPoint(x, y);		
		return true;	
	}
	public	boolean	mouseUp(int x, int y) {
		return flage = false;		
	}
	public	void	draw(Graphics g) {
		if(flage == true)
			sp.draw(g);
	}
	public	Graphic	getProduct() {
		Graphic g = sp;
		sp = (AShape)sp.clone();
		return g;		
	}
}

/**
 * the product is a composite of AShape: Picture
 */
abstract class	CompositeTool extends Tool {
	protected	int size;
	protected	Picture	picture = new Picture();

	public	CompositeTool(AShape s) {super(s);}
	public	CompositeTool(AShape s,int sz) {super(s); size=sz;}

	public	void	setSize(int size) {this.size=size;}

	public	boolean	mouseUp(int x, int y) {
		return flage = false;
	}
	protected	void	addShape() {
		if(flage == true) {
			picture.add(sp);
			sp.draw(g);
			sp = (AShape)sp.clone();
			flage = false;
		}
	}
	public	void	draw(Graphics g) {
		picture.draw(g);
		if(flage == true)
			sp.draw(g);
	}
	public	Graphic	getProduct() {
		Graphic g = picture;
		picture = new Picture();
		return g;		
	}
}

class	PenTool extends CompositeTool {
	public	PenTool(Color c) {
		super(new Line(c));
	}

	public	boolean	mouseDrage(int x, int y) {
		sp.setSecondPoint(x, y);
		addShape();
		sp.setFirstPoint(x, y);
		flage = true;		
		return	false;
	}
}

class	SprayTool extends CompositeTool implements Runnable
{
	private	int x, y;
	Random	rand = new Random();

	public	SprayTool(Color c,int s) {
		super(new Line(c), s);
		new Thread(this).start();
	}

	public void	run() {
		while(true) {
			if(flage == true) {
				addShape();
			}
			try {Thread.sleep(10);}
			catch(InterruptedException e) {}
		}
	}

	public	boolean	mouseDown(int x, int y) {
		flage = true;
		this.x = x;
		this.y = y;
		addShape();
		return	false;
	}
	public	boolean	mouseDrage(int x, int y) {
		mouseDown(x, y);
		return false;				
	}

	protected	synchronized void	addShape() {
		double	a;
		int	x1, y1;
		for(int i=0; i<size; ++i) {
			a = Math.PI*(rand.nextInt(360)-180)/180;
			x1 = x + (int)(i*Math.sin(a));
			y1 = y + (int)(i*Math.cos(a));
			sp.setFirstPoint(x1, y1);
			sp.setSecondPoint(x1, y1);
			picture.add(sp);
			sp.draw(g);
			sp = (AShape)sp.clone();
		}
	}
}

class	BrushTool extends CompositeTool
{
	private	int x1, x2 ,y1, y2;

	public	BrushTool(Color c,int s){super(new FillOval(c),s);}
	
	public	boolean	mouseDrage(int x, int y) {
		int dx = (x2=x) - x1, dy = (y2=y) - y1, len =size/2;
		double dist = Math.sqrt(dx*dx + dy*dy);

        // There may be a big distance between the two continous points,
        // so we have to insert some shapes between them
		if(dist>len) {
			double incx=len*((double)dx/dist),incy=len*((double)dy/dist);

			for(int i=0,j=(int)(dist+len-1)/len; i<j; ++i)
				addShape( x1 + (int)(i * incx), y1 + (int)(i * incy) );
		 }
		addShape(x1=x2, y1=y2);
		return	false;
	}

	public	boolean	mouseDown(int x, int y) {
		super.mouseDown(x ,y);
		addShape(x1=x, y1=y);
		return	false;
	}

	protected	void	addShape(int x,int y) {
		sp.setFirstPoint(x-size, y-size);
		sp.setSecondPoint(x+size, y+size);
		picture.add(sp);
		sp.draw(g);
		sp = (AShape)sp.clone();
	}
}

class	RubberTool extends BrushTool
{
	public	RubberTool(int s) {
		super(null, s);
		// Just change the shape class
		sp = new FillRect(Color.white);
	}
    // Override the setColor method
	public	void	setColor(Color c) {sp.setColor(Color.white);}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -