原始的画板:
(1)DrawUI类:
public class DrawUI extends javax.swing.JFrame{
public static void main(String args[]){
DrawUI ui = new DrawUI();
ui.initDrawUI();
}
/**
* 初始化窗体的方法
*/
public void initDrawUI(){
//指调用房当前initDrawUI()方法的对象
this.setTitle("我的画板v01");
this.setSize(600,500);
//设置布局
java.awt.FlowLayout fl = new java.awt.FlowLayout();
this.setLayout(fl);
//创建一个按钮组
javax.swing.ButtonGroup group = new javax.swing.ButtonGroup();
//单选按钮
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");
//按钮分组
group.add(line);
group.add(rect);
group.add(oval);
this.add(line);
this.add(rect);
this.add(oval);
this.setDefaultCloseOperation(3);
this.setVisible(true);
//从窗体上获取画布
//窗体在屏幕上所占据的区域是允许改变颜色的
java.awt.Graphics g = this.getGraphics();
//创建一个鼠标监听器对象
DrawListener dlis = new DrawListener(g,group);
//给窗体加鼠标监听器
this.addMouseListener(dlis);
}
}
(2)DrawListener类:
import java.awt.event.MouseEvent;
/**
* 画板的监听器,实现鼠标监听器接口
*
* @author Administrator
*
*/
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";
public DrawListener(java.awt.Graphics g, javax.swing.ButtonGroup group) {
this.g = g;
this.group = group;
}
// 按下
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();
if ("line".equals(type)) {
// 当鼠标释放的时候就画直线
g.drawLine(x1, y1, x2, y2);
} else if (type.equals("rect")) {
g.drawRect(x1, y1, x2 - x1, y2 - y1);
} else if (type.equals("oval")) {
g.drawOval(x1, y1, x2 - x1, y2 - y1);
}
}
// 进入
public void mouseEntered(MouseEvent e) {
// System.out.println("mouseEntered");
}
// 离开
public void mouseExited(MouseEvent e) {
// System.out.println("mouseExited");
}
// 点击
public void mouseClicked(MouseEvent e) {
// System.out.println("mouseClicked");
}
}
总结:
1、运用initDraw()设计窗体,而非把所有设计步骤都放在main()方法中
2、设计窗体的框架步骤:a、大小、标题->布局->添加元素组件->关闭操作、可见性
b、为窗体添加监听器
3、只有组件可见后方可创建Graphics对象
4、单选按钮需添加进按钮组方可起作用
5、运用属性、构造函数将两个相关类连接起来(例如:将属性Graphics g,Buttongroup group传入DrawListener类)
6、Graphics类不能使用构造方法来创建对象
7、每个按钮都有其动作命令(和按钮的文本不一样)
8、只要为组件添加监听器,组件就可以自动监听动作事项
扩展后的画板(任意方向画图、存储数据):
(1)接口Shape;实现类Line、Rect、Oval
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
//坐标、颜色
protected int x1,x2,y1,y2;
protected Color color;
//draw()方法
public abstract void draw(Graphics g);
}
import java.awt.Graphics;
import java.awt.Color;
public class Line extends Shape{
//重写构造方法
public Line(int x1,int y1,int x2,int y2,Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}
//重写draw()方法
public void draw(Graphics g){
g.setColor(color);
g.drawLine(x1,y1,x2,y2);
}
}
import java.awt.Graphics;
import java.awt.Color;
public class Rect extends Shape{
public Rect(int x1,int y1,int x2,int y2,Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}
public void draw(Graphics g){
g.setColor(color);
g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
}
import java.awt.Color;
import java.awt.Graphics;
public class Oval extends Shape{
public Oval(int x1,int y1,int x2,int y2,Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}
public void draw(Graphics g){
g.setColor(color);
g.drawOval(Math.min(x1,x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
}
(2)DrawUI、DrawListener类
public class DrawUI extends javax.swing.JFrame{
public static void main(String args[]){
DrawUI ui = new DrawUI();
ui.initDrawUI();
}
/**
* 初始化窗体的方法
*/
public void initDrawUI(){
//指调用房当前initDrawUI()方法的对象
this.setTitle("我的画板v01");
this.setSize(600,500);
//设置布局
java.awt.FlowLayout fl = new java.awt.FlowLayout();
this.setLayout(fl);
//创建一个按钮组
javax.swing.ButtonGroup group = new javax.swing.ButtonGroup();
//单选按钮
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");
//按钮分组
group.add(line);
group.add(rect);
group.add(oval);
this.add(line);
this.add(rect);
this.add(oval);
this.setDefaultCloseOperation(3);
this.setVisible(true);
//从窗体上获取画布
//窗体在屏幕上所占据的区域是允许改变颜色的
java.awt.Graphics g = this.getGraphics();
//创建一个鼠标监听器对象
DrawListener dlis = new DrawListener(g,group);
//给窗体加鼠标监听器
this.addMouseListener(dlis);
}
}
import java.awt.event.MouseEvent;
/**
* 画板的监听器,实现鼠标监听器接口
*
* @author Administrator
*
*/
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";
public DrawListener(java.awt.Graphics g, javax.swing.ButtonGroup group) {
this.g = g;
this.group = group;
}
// 按下
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();
if ("line".equals(type)) {
// 当鼠标释放的时候就画直线
g.drawLine(x1, y1, x2, y2);
} else if (type.equals("rect")) {
g.drawRect(x1, y1, x2 - x1, y2 - y1);
} else if (type.equals("oval")) {
g.drawOval(x1, y1, x2 - x1, y2 - y1);
}
}
// 进入
public void mouseEntered(MouseEvent e) {
// System.out.println("mouseEntered");
}
// 离开
public void mouseExited(MouseEvent e) {
// System.out.println("mouseExited");
}
// 点击
public void mouseClicked(MouseEvent e) {
// System.out.println("mouseClicked");
}
}
对比总结:
1、将Line、Rect、Oval分别创建成类,实现Shape接口,便于统一规范存储
2、因为存储元素(对象)个数不固定,故选择队列存储方式
3、每个对象所要存储的数据变量以属性表示
4、接口以实现程序的目标为准,将各个实现类的共有属性和方法加以归纳提炼
5、运用接口可以灵活使用累的多态性这一特征
6、运用构造函数将一个类中的属性传入另一个类