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

📄 paintpane.java

📁 一个简易的java画图软件
💻 JAVA
字号:
package app.pane;

import java.awt.Color;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImageProducer;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.net.URL;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

import app.Application;
import draw.Draw;
import draw.figuare.AbstractFiguare;

import skin.Display;

/**
 * 绘图工具。 有 选择,拖动,圆形,矩形等。 选择颜色。 选择是否填充。
 * 
 * @author Thihy
 * 
 */
public class PaintPane extends JToolBar {
	protected Display display;
	protected Application app;
	protected Draw draw;

	/**
	 * 默认的线形的颜色:黑色
	 */
	public final static Color DEFAULT_STROKE_COLOR = Color.black;
	/**
	 * 默认的填充的颜色:白色
	 */
	public final static Color DEFAULT_FILL_COLOR = Color.white;

	protected Color strokeColor = DEFAULT_STROKE_COLOR;
	protected Color fillColor = DEFAULT_FILL_COLOR;
	protected JToggleButton strokeButton, fillButton;

	public final String ID = "PaintPane";// 必须与类的名字相同

	/**
	 * 初始化
	 * 
	 * @param parent
	 *            工具条所在的主窗口
	 */
	public PaintPane(Application parent) {
		this(VERTICAL, parent);
	}

	/**
	 * 初始化,并且指定工具条所控制的画板
	 * 
	 * @param draw
	 *            控制的画板
	 * @param parent
	 *            工具条所在的主窗口
	 */
	public PaintPane(Draw draw, Application parent) {
		this(VERTICAL, parent);
		this.draw = draw;
	}

	/**
	 * 初始化,并且指定工具条的名称
	 * 
	 * @param name
	 *            工具条的名称
	 * @param parent
	 *            工具条所在的主窗口
	 */
	public PaintPane(String name, Application parent) {
		this(name, VERTICAL, parent);
	}

	/**
	 * 初始化,并且指定工具条的样式
	 * 
	 * @param orientation
	 *            工具条的样式
	 * @param parent
	 *            工具条所在的主窗口
	 */
	public PaintPane(int orientation, Application parent) {
		this(null, orientation, parent);
		// 没有指定名称,则调用配置属性里的名称。
		setName(getLabel("tool"));
	}

	/**
	 * 初始化,并且指定工具条的名称和工具条的样式
	 * 
	 * @param name
	 *            工具条的名称
	 * @param orientation
	 *            工具条的样式
	 * @param parent
	 *            工具条所在的主窗口
	 */
	public PaintPane(String name, int orientation, Application parent) {
		super(name, orientation);
		this.app = parent;
		display = new Display();

		initGui();
	}

	/**
	 * 指定工具条所控制的画板
	 * 
	 * @param draw
	 *            控制的画板
	 */
	public void setDraw(Draw draw) {
		if (draw != this.draw && draw != null) {// draw不能为空
			this.draw = draw;

			arrowButton.setSelected(true);
			draw.setPaintTool(AbstractFiguare.ARROW);
		}
	}

	JToggleButton arrowButton;

	/**
	 * 初始化工具条,创建各个按钮
	 */
	private void initGui() {
		setFloatable(false);

		ButtonGroup bg = new ButtonGroup();

		arrowButton = (JToggleButton) createShapeTool("arrow",
				AbstractFiguare.ARROW, bg);
		add(arrowButton);

		// add(createShapeTool("select",AbstractFiguare.SELECT,bg));
		add(createShapeTool("rect", AbstractFiguare.RECTANGEL, bg));
		add(createShapeTool("roundedrect", AbstractFiguare.ROUND_RECTANGEL, bg));
		add(createShapeTool("ellipse", AbstractFiguare.ELLIPSE, bg));
		add(createShapeTool("line", AbstractFiguare.STRAIGHT_LINE, bg));
		add(createShapeTool("text", AbstractFiguare.TEXT, bg));
		// add(createShapeTool("hand",AbstractFiguare.HAND,bg));
		// add(createShapeTool("magnifier",AbstractFiguare.MAGNIFIER,bg));

		bg.setSelected(arrowButton.getModel(), true);

		addSeparator();
		ButtonGroup colorBG = new ButtonGroup();

		strokeButton = (JToggleButton) createColorTool("stroke", colorBG);
		fillButton = (JToggleButton) createColorTool("fill", colorBG);

		strokeButton.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				int px = e.getX(), py = e.getY();
				((JToggleButton) e.getSource()).getLocation();

				if (12 <= px && 12 <= py) {
					Color color = JColorChooser.showDialog(null, "选择线条颜色",
							strokeColor);
					if (color != null) {
						strokeColor = color;
						draw.setStrokeColor(strokeColor);
						JToggleButton b = (JToggleButton) e.getSource();
						updateButtonColor(b, strokeColor);
					}
				}
			}
		});

		fillButton.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				int px = e.getX(), py = e.getY();
				if (12 <= px && 12 <= py) {
					Color color = JColorChooser.showDialog(null, "选择填充颜色",
							fillColor);
					if (color != null) {
						fillColor = color;
						draw.setFillColor(fillColor);
						JToggleButton b = (JToggleButton) e.getSource();
						updateButtonColor(b, fillColor);
					}
				}

			}
		});
		add(strokeButton);
		add(fillButton);

		colorBG.setSelected(strokeButton.getModel(), true);
		updateButtonColor(strokeButton, strokeColor);
		updateButtonColor(fillButton, fillColor);

		add(createLittleColorTool("blackwhite", 0));
		add(createLittleColorTool("colorsswap", 1));
		add(createLittleColorTool("colorsnone", 2));

	}

	/**
	 * 创建小型颜色按钮,主要提供了一些方便的操作
	 * 
	 * @param toolName
	 * @param id
	 * @return
	 */
	private JComponent createLittleColorTool(String toolName, int id) {
		JButton b = new JButton();
		ImageIcon image = getImage(toolName);
		b.setIcon(image);
		b.setMargin(new Insets(0, 0, 0, 0));

		switch (id) {
		case 0: // 设置为默认颜色
			b.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					strokeColor = DEFAULT_STROKE_COLOR;
					fillColor = DEFAULT_FILL_COLOR;
					draw.setStrokeColor(strokeColor);
					draw.setFillColor(fillColor);
					updateButtonColor(strokeButton, strokeColor);
					updateButtonColor(fillButton, fillColor);
				}
			});
			break;
		case 1: // 交换颜色
			b.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					Color color;
					color = strokeColor;
					strokeColor = fillColor;
					fillColor = color;
					draw.setStrokeColor(strokeColor);
					draw.setFillColor(fillColor);
					updateButtonColor(strokeButton, strokeColor);
					updateButtonColor(fillButton, fillColor);
				}
			});
			break;
		case 2: // 设置为无色
			b.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if (strokeButton.isSelected()) {
						strokeColor = null;
						draw.setStrokeColor(strokeColor);
						updateButtonColor(strokeButton, strokeColor);
					} else if (fillButton.isSelected()) {
						fillColor = null;
						draw.setFillColor(fillColor);
						updateButtonColor(fillButton, fillColor);
					}
				}
			});
			break;
		default:

		}

		return b;
	}

	/**
	 * 创建形状工具,是一个单选按钮。
	 * 
	 * @param toolName
	 *            形状工具的名称
	 * @param figuareID
	 *            图形工具对应的ID,这个会告诉画布当前是什么工具
	 * @param bg
	 *            所在的按钮组
	 * @return
	 */
	private JComponent createShapeTool(String toolName, final int figuareID,
			ButtonGroup bg) {
		final JToggleButton tb = new JToggleButton();
		tb.setToolTipText(getTipText(toolName));
		ImageIcon image = getImage(toolName);
		tb.setIcon(image);
		tb.setMargin(new Insets(1, 1, 1, 1));
		if (figuareID >= 0) {
			tb.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if (tb.isSelected()) {
						draw.setPaintTool(figuareID);
					}
				}
			});
		}
		bg.add(tb);
		return tb;
	}

	/**
	 * 创建颜色工具,是一个单选按钮。
	 * 
	 * @param toolName
	 *            形状工具的名称
	 * @param bg
	 *            所在的按钮组
	 * @return
	 */
	private JComponent createColorTool(String toolName, ButtonGroup bg) {
		final JToggleButton tb = new JToggleButton();
		tb.setToolTipText(getTipText(toolName));
		ImageIcon image = getImage(toolName);
		tb.setIcon(image);
		tb.setMargin(new Insets(1, 1, 1, 1));

		bg.add(tb);
		return tb;
	}

	/**
	 * 在颜色选择按钮上绘制指定颜色<br/> 它会显示在按钮的右下角
	 * 
	 * @param b
	 *            按钮
	 * @param color
	 *            颜色
	 */
	private void updateButtonColor(JToggleButton b, Color color) {
		ImageIcon image = (ImageIcon) b.getIcon();
		Image im = image.getImage();
		int pixels[] = new int[22 * 22];
		PixelGrabber pg = new PixelGrabber(im, 0, 0, 22, 22, pixels, 0, 22);

		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
		}

		// 在右下角画出一个方块,采用指定的颜色
		for (int i = 12; i < 21; ++i) {
			for (int j = 12; j < 21; ++j) {
				int k = i + 22 * j;
				if (color != null) {
					pixels[k] = color.getRGB();
				} else {
					if (i + j < 34 && i + j > 30) {
						pixels[k] = Color.red.getRGB();
					} else {
						pixels[k] = Color.white.getRGB();
					}
				}
			}
		}

		// 绘出这个图形
		ImageProducer ip = new MemoryImageSource(22, 22, pixels, 0, 22);
		Image newIm = createImage(ip);
		image.setImage(newIm);
		b.setIcon(image);
		b.repaint();
	}

	/**
	 * 从配置文件中根据指定的key返回它的名称
	 * 
	 * @param key
	 *            指定的key
	 * @return 名称
	 */
	protected String getLabel(String key) {
		return getDisplayValue(key, "Label");
	}

	/**
	 * 从配置文件中根据指定的key返回图形地址
	 * 
	 * @param key
	 *            指定的key
	 * @return 图形地址
	 */
	protected ImageIcon getImage(String key) {
		URL url;
		if (PaintPane.class.getResource("images/" + key + ".png") != null) {
			url = PaintPane.class.getResource("images/" + key + ".png");
		} else if (PaintPane.class.getResource("images/" + key + ".gif") != null) {
			url = PaintPane.class.getResource("images/" + key + ".gif");
		} else {
			return null;
		}
		return new ImageIcon(url);
	}

	/**
	 * 从配置文件中根据指定的key返回提示消息
	 * 
	 * @param key
	 *            指定的key
	 * @return 提示消息
	 */
	protected String getTipText(String key) {
		return getDisplayValue(key, "Tip");
	}

	/**
	 * 从配置文件中获取指定key的值
	 * 
	 * @param key
	 *            指定的key
	 * @param kind
	 *            key所在的分组
	 * @return 值
	 */
	protected String getDisplayValue(String key, String kind) {
		return display.getToolValue(ID + "." + key + kind);
	}

}

⌨️ 快捷键说明

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