drawpanel.java

来自「包含多个java程序」· Java 代码 · 共 156 行

JAVA
156
字号
//DrawPanel.java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;

/**
 * DrawPanel
 * @author  
 * @version 1.0 05/24/03
 *
 * DrawPanel is a state pattern,its behavior will change with the tool it owns
 *
 * It uses the double-buffer technique to accelerate painting
 */

class DrawPanel extends JPanel implements MouseListener,MouseMotionListener
{
	private	Picture		picture = new Picture();
	private	Tool		tool = null;
	private	BufferedImage	orgimg, bufimg, offimg;
	private	Graphics	gbufimg, goffimg;
	private int			width = 800, height = 600, sx = 5, sy=5;
	private int			type = BufferedImage.TYPE_3BYTE_BGR;
	private	static boolean init = true;

	public DrawPanel(){
		addMouseListener(this);
		addMouseMotionListener(this);
		setBackground(Color.gray);
		setDoubleBuffered(true);
		setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
	}

	public void paint(Graphics g){
		super.paint(g);
		// First run
		if(init == true) {
			newPicture();
			init = false;
		}
		goffimg.drawImage(bufimg,0,0,width,height,0,0,width,height,null);
		if(tool != null)
			tool.draw(goffimg);
		g.drawImage(offimg,0,0,width,height,0,0,width,height,null);
	}

	public void newPicture() {
		width = 800; height = 600;
		type = BufferedImage.TYPE_3BYTE_BGR;
		orgimg = new BufferedImage(width, height, type);
		initBufimg();
		Graphics g = orgimg.getGraphics();
		g.setColor(Color.pink);
		g.fillRect(0, 0, width, height);
		g.setColor(Color.WHITE);
		g.fillRect(sx, sy, width-2*sx, height-2*sy);
		clear();
	}

	private void initBufimg() {
		width = orgimg.getWidth();
		height = orgimg.getHeight();
 		type = orgimg.getType();
		bufimg = new BufferedImage(width, height, type);
		offimg = new BufferedImage(width, height, type);
		gbufimg = bufimg.getGraphics();
		goffimg = offimg.getGraphics();
	}

	private	void draw(Graphics g){
		g.drawImage(orgimg,0,0,width,height,0,0,width,height,null);
		picture.draw(g);
	}

	public	void	unDo(){
		picture.remove();
		draw(gbufimg);
		repaint();
	}
	public	void	reDo(){	
		picture.add();
		if(picture.getLast() != null)
			picture.getLast().draw(gbufimg);
		repaint();
	}
	public	void	clear(){	
		picture.clear();
		if(tool != null)
			tool.reset();
		draw(gbufimg);
		repaint();
	}
	
	public	boolean save(String name){
		if(!name.endsWith(".jpg") && !name.endsWith(".JPG"))
			name += ".jpg";
		boolean	r = false;
		File f = new File(name);
		if(f == null)
			return false;
		try{
			r = ImageIO.write(bufimg, "JPEG", f);
		} catch(Exception e) {}
		return r;
	}
	public	boolean open(String name){
		BufferedImage img = null;
		try{
			img = ImageIO.read(new File(name));
		} catch(Exception e) {}
		if(img == null)
			return false;
		orgimg = img;
		initBufimg();
		clear();
		return true;
	}
	
	public void	setColor(Color c){
		if(tool != null && c!=null)
			tool.setColor(c);
	}
	public void	setTool(Tool t){
		if(t != null)
			tool = t;
	}

	public	void mousePressed(MouseEvent e){
		int x = e.getX(), y = e.getY();	
		if(tool!=null && x<=width && y<=height && tool.mouseDown(x, y))
			repaint();
	}
	public	void mouseReleased(MouseEvent e){
		int x = e.getX(), y = e.getY();	
		if(tool!=null && x<=width && y<=height){
		 	tool.mouseUp(x, y);
		 	// Add shape to picture
			picture.add(tool.getProduct());
		}
		if(picture.getLast() != null)
			picture.getLast().draw(gbufimg);
	}
	public	void mouseDragged(MouseEvent e){
		int x = e.getX(), y = e.getY();	
		if(tool!=null && x<=width && y<=height && tool.mouseDrage(x, y))
			repaint();
	}

	public	void mouseClicked(MouseEvent e){}
	public	void mouseEntered(MouseEvent e){}	
	public	void mouseExited(MouseEvent e){}
	public	void mouseMoved(MouseEvent e){}
}

⌨️ 快捷键说明

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