`
云初静
  • 浏览: 28385 次
  • 性别: Icon_minigender_2
  • 来自: 湖南
社区版块
存档分类
最新评论

画板_初始_监听_颜色选择

阅读更多
初始画图板的创建:
1.创建一个队列接口ListInterface 和一个队列应用类ListImp
2.创建一个画图板界面类DrawUI,在其中用一个队列ListImp<Shape>来保存形状对象。创建面板对象UI,使用UI调用初始化函数init()。在init()方法中设置界面的各种属性,以及添加组件。添加“选择颜色”按钮,设置按钮的事件监听器,调用弹出颜色选择器的方法。重写绘制窗体的方法。
代码如下:

package 画板_颜色_重绘;

import java.awt.Graphics;
import java.awt.event.ActionEvent;

//画图板
public class DrawUI extends javax.swing.JFrame {
	private java.awt.Color color = java.awt.Color.black;
	// 定义一个队列用来保存形状对象
	private ListImp<Shape> shapes = new ListImp<Shape>();

	public static void main(String[] args) {
		// 创建面板
		DrawUI ui = new DrawUI();
		// 调用初始化函数或者showUI函数
		ui.init();
	}

	// 初始化函数
	public void init() {
		// 设置属性
		this.setTitle("画图板");
		this.setSize(800, 500);
		// 设置点击关闭退出窗口
		this.setDefaultCloseOperation(3);
		// 设置布局
		java.awt.FlowLayout fl = new java.awt.FlowLayout();
		this.setLayout(fl);

		// 创建组件单选按钮
		javax.swing.JRadioButton line = new javax.swing.JRadioButton("直线");
		// 设置组件动作命令
		line.setActionCommand("line");
		// 默认选中直线单选按钮
		line.setSelected(true);
		// 继续创建别的组件单选按钮,设置组件动作命令
		javax.swing.JRadioButton rect = new javax.swing.JRadioButton("矩形");
		rect.setActionCommand("rect");
		javax.swing.JRadioButton oval = new javax.swing.JRadioButton("椭圆");
		oval.setActionCommand("oval");
		javax.swing.JRadioButton roundRect = new javax.swing.JRadioButton(
				"圆角矩形");
		roundRect.setActionCommand(" roundRect");
		javax.swing.JRadioButton Arc = new javax.swing.JRadioButton("弧线");
		Arc.setActionCommand(" Arc");
		// 创建分组 group
		javax.swing.ButtonGroup group = new javax.swing.ButtonGroup();
		// 实现分组
		group.add(line);
		group.add(rect);
		group.add(oval);
		group.add(roundRect);
		group.add(Arc);
		// 将组件添加到面板上
		this.add(line);
		this.add(rect);
		this.add(oval);
		this.add(roundRect);
		this.add(Arc);
		// 添加"选颜色"按钮,点击后,弹出颜色选择器
		javax.swing.JButton bu_color = new javax.swing.JButton("选颜色");
		// 将按钮添加在窗体上
		this.add(bu_color);
		// 设置按钮的事件监听器
		bu_color.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 调用弹出颜色选择器的方法
				showColorSelecer();
			}
		});
		// 设置窗体可见
		this.setVisible(true);
		// 添加画布,要在可视化之后
		java.awt.Graphics grap = this.getGraphics();
		// 窗口添加监听器
		DrawListener dl = new DrawListener(grap, group, shapes);
		this.addMouseListener(dl);
	}

	// 当点击选择颜色按钮时,显示颜色选择器,并得到用户默认选中的颜色
	private void showColorSelecer() {
		// 弹出颜色选择器:弹出在哪个组件(null)上,标题(请选择颜色),初始化颜色(红色)三个参数要指定
		// 这是JColorChooser中的一个public static 方法,可以直接调用
		this.color = javax.swing.JColorChooser.showDialog(null, "颜色选择器",
				java.awt.Color.red);
	}

	// 重写绘制窗体的方法
	public void paint(Graphics g) {
		// 调用父类的方法来正确的绘制窗体
		super.paint(g);

		// 遍历队列,
		for (int i = 0; i < shapes.size(); i++) {
			// 取出对象,
			Shape sh = shapes.get(i);
			// 绘制形状对象
			sh.draw(g);
		}
	}
}


3.创建一个DrawListener类,重写java.awt.event.MouseListener类中的方法。主要是mousePressed和mouseReleased方法的重写。在mouseReleased方法中创建指向shape的对象,因为Shape是抽象类,所以,采用自动转型,创建子类对象指向父类:Shape sh=new Line(x1, y1, x2, y2)。用sh调用画图的方法sh.draw(g),并将绘制的形状对象保存到队列中shapes.add(sh)。
代码如下:

package 画板_颜色_重绘;

import java.awt.event.MouseEvent;

//画板的监听器,实现鼠标监听器接口
public class DrawListener implements java.awt.event.MouseListener {
	private int x1, y1, x2, y2;
	private java.awt.Graphics g;
	private javax.swing.ButtonGroup group;
	private String type = "line";
	private ListImp<Shape> shapes;

	public DrawListener(java.awt.Graphics g, javax.swing.ButtonGroup group,ListImp<Shape> shapes) {
		this.g = g;
		this.group = group;
		this.shapes = shapes;
	}

	// 按下
	public void mousePressed(MouseEvent e) {
		// 先判断要绘制的形状
		// 得到选中选项的动作命令
		type = group.getSelection().getActionCommand();

		System.out.println("要绘制的形状:" + type);
		// 得到鼠标按下时候光标的坐标
		x1 = e.getX();
		y1 = e.getY();
	}

	// 释放
	public void mouseReleased(MouseEvent e) {
		// 得到鼠标释放时候光标的坐标
		x2 = e.getX();
		y2 = e.getY();
		Shape sh=new Line(x1, y1, x2, y2);
		if ("line".equals(type)) {
			sh = new Line(x1, y1, x2, y2);

		} else if ("rect".equals(type)) {
			sh = new Rect(x1, y1, x2, y2);

		} else if (type.equals("oval")) {
			sh = new Oval(x1, y1, x2, y2);
			g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));

		} else if (type.equals(" roundRect")) {
			sh = new RoundRect(x1, y1, x2, y2, Math.abs(x1 - x2) / 5,
					Math.abs(y1 - y2) / 5);

		} else if (type.equals(" Arc")) {
			sh = new Arc (Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2), 30, 80);
		}
		// 绘制形状
		sh.draw(g);
		//将绘制的形状对象保存到队列中
		shapes.add(sh);
	}

	// 进入
	public void mouseEntered(MouseEvent e) {

	}

	// 退出
	public void mouseExited(MouseEvent e) {

	}

	// 点击
	public void mouseClicked(MouseEvent e) {

	}

}

4.创建一个抽象类Shape
package 画板_颜色_重绘;

import java.awt.Graphics;

public abstract class Shape {

	 int x1, y1, x2, y2;

	public abstract void draw(Graphics g);

}

5.创建各种画图类(如:Line【直线】,Rec【矩形】,Oval【椭圆】,roundRec【圆矩】,Arc【弧线】),都继承自Shape类,是Shape类的各种实现。
例如:画Oval的方法

package 画板_颜色_重绘;

import java.awt.Graphics;

public class Oval extends Shape{
			// 重载构造方法
			public Oval (int x1, int y1, int x2, int y2) {
				this.x1 = x1;
				this.y1 = y1;
				this.x2 = x2;
				this.y2 = y2;
	}
			public void draw(Graphics g) {
				g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
						Math.abs(y1 - y2));
			}
}


简单的画图板就出来啦





tips:java自身函数的应用:Math.min(x1, x2), Math.abs(x1 - x2)
  • 大小: 31.2 KB
  • 大小: 56.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics