首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-10-30 
画板_初始_监听_颜色选择初始画图板的创建:1.创建一个队列接口ListInterface 和一个队列应用类ListImp2.创

画板_初始_监听_颜色选择
初始画图板的创建:
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");// 创建分组 groupjavax.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)

热点排行