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

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


[size=large][color=olive]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)。
代码如下:[/color][/size]
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) {

}

}

[size=large][color=olive]4.创建一个抽象类Shape[/color][/size]
package 画板_颜色_重绘;

import java.awt.Graphics;

public abstract class Shape {

int x1, y1, x2, y2;

public abstract void draw(Graphics g);

}

[size=large][color=olive]5.创建各种画图类(如:Line【直线】,Rec【矩形】,Oval【椭圆】,roundRec【圆矩】,Arc【弧线】),都继承自Shape类,是Shape类的各种实现。
例如:画Oval的方法[/color][/size]
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));
}
}


[size=large][color=olive]简单的画图板就出来啦[/color][/size]
[img]http://dl.iteye.com/upload/attachment/574764/81f61f19-d062-3b88-9593-1319b4e7a3e9.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/574769/96916a7f-fe3f-3da8-9ce2-31ccf60c6482.jpg[/img]

[size=large][color=olive]
[b]tips:[/b]java自身函数的应用:Math.min(x1, x2), Math.abs(x1 - x2)[/color][/size]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值