要实现一个简单的画板程序 1.定义一个测试类,用来显示画板窗体。在这个类中,要对窗体属性进行设置,再定义几个按钮,并添加鼠标监听器及给按钮分别添加动作监听器。
2.定义鼠标监听器,在这个监听器中,对鼠标按下与释放时的坐标进行记录,并且对所要画的图形方法进行定义。
3.定义动作监听器,对鼠标的动作进行处理。
画板重绘时需在实现简单画板的基础上定义一个Shape类和一个自定义队列,Shape类用来对所画图形进行记录和保存,队列用来实现重绘的方法。
示例代码如下:
1.测试类
import java.awt.Graphics;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class Huaban extends JFrame {
private ListDiy listDiy = new ListDiy();
//在Huaban类中定义一个方法去初始化窗体的参数
public void showUI() {
// 窗口设置
this.setTitle("画板");
this.setSize(600, 450);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
// 流式布局
java.awt.FlowLayout f1 = new java.awt.FlowLayout();
this.setLayout(f1);
// 增加三个按钮
javax.swing.JButton line = new javax.swing.JButton("直线");
javax.swing.JButton rect = new javax.swing.JButton("矩形");
javax.swing.JButton oval = new javax.swing.JButton("圆");
this.add(line);
this.add(rect);
this.add(oval);
this.setVisible(true);
// 从画板上得到画布对象
java.awt.Graphics g = this.getGraphics();
// 增加一个鼠标监听器
MouseListenerImp mouseListener = new MouseListenerImp(g, listDiy);
this.addMouseListener(mouseListener);
//增加动作监听器
ActionListener actionListener = new ActionListenerImpl(mouseListener);
line.addActionListener(actionListener);
rect.addActionListener(actionListener);
oval.addActionListener(actionListener);
}
public void paint(Graphics g) {
super.paint(g);
System.out.println("重绘函数已经调用啦!");
drawShape(g);
}
private void drawShape(Graphics g) {
for(int i = 0; i < listDiy.getSize(); i++) {
Shape shape = listDiy.get(i);
String type = shape.getType();
int x1 = shape.getX1();
int y1 = shape.getY1();
int x2 = shape.getX2();
int y2 = shape.getY2();
if(type.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
}
if(type.equals("矩形")) {
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
if(type.equals("圆")) {
g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
}
}
public static void main(String[] args) {
Huaban huaban = new Huaban();
huaban.showUI();
}
}
2.鼠标监听器
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MouseListenerImp extends MouseAdapter {
// 定义属性
private java.awt.Graphics g;
private int x1, y1, x2, y2;
private String type;
private ListDiy listDiy;
//重载构造器,对对象进行初始化
public MouseListenerImp(java.awt.Graphics g, ListDiy listDiy) {
this.g = g;
this.listDiy = listDiy;
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 =e.getY();
if(type.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
}
if(type.equals("矩形")) {
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
if(type.equals("圆")) {
g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
Shape shape = new Shape(x1, y1, x2, y2);
shape.setType(type);
listDiy.add(shape);
}
public void setShape(String shape) {
this.type = shape;
}
}
3.动作监听器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ActionListenerImpl implements ActionListener{
private MouseListenerImp mouseListener; //?
public ActionListenerImpl(MouseListenerImp mouseListener) {
this.mouseListener = mouseListener;
}
@Override
public void actionPerformed(ActionEvent e) {
Object object = e.getSource();
JButton button = (JButton) object;//强制类型转换
System.out.println("按钮名称 : " + button.getText());
mouseListener.setShape(button.getText());
}
}
4.Shape类
public class Shape {
private int x1, y1, x2, y2; //起点终点的坐标值
private String type; //类型
public Shape(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
5.自定义队列
public class ListDiy {
private Shape[] older = new Shape[0];
public int getSize() {
return older.length;
}
public void add(Shape shape) {
Shape[] newOne = new Shape[getSize() + 1];
newOne[getSize()] = shape;
for(int i = 0; i < older.length; i++) {
newOne[i] = older[i];
}
older = newOne;
}
public Shape get(int i) {
return older[i];
}
2.定义鼠标监听器,在这个监听器中,对鼠标按下与释放时的坐标进行记录,并且对所要画的图形方法进行定义。
3.定义动作监听器,对鼠标的动作进行处理。
画板重绘时需在实现简单画板的基础上定义一个Shape类和一个自定义队列,Shape类用来对所画图形进行记录和保存,队列用来实现重绘的方法。
示例代码如下:
1.测试类
import java.awt.Graphics;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class Huaban extends JFrame {
private ListDiy listDiy = new ListDiy();
//在Huaban类中定义一个方法去初始化窗体的参数
public void showUI() {
// 窗口设置
this.setTitle("画板");
this.setSize(600, 450);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
// 流式布局
java.awt.FlowLayout f1 = new java.awt.FlowLayout();
this.setLayout(f1);
// 增加三个按钮
javax.swing.JButton line = new javax.swing.JButton("直线");
javax.swing.JButton rect = new javax.swing.JButton("矩形");
javax.swing.JButton oval = new javax.swing.JButton("圆");
this.add(line);
this.add(rect);
this.add(oval);
this.setVisible(true);
// 从画板上得到画布对象
java.awt.Graphics g = this.getGraphics();
// 增加一个鼠标监听器
MouseListenerImp mouseListener = new MouseListenerImp(g, listDiy);
this.addMouseListener(mouseListener);
//增加动作监听器
ActionListener actionListener = new ActionListenerImpl(mouseListener);
line.addActionListener(actionListener);
rect.addActionListener(actionListener);
oval.addActionListener(actionListener);
}
public void paint(Graphics g) {
super.paint(g);
System.out.println("重绘函数已经调用啦!");
drawShape(g);
}
private void drawShape(Graphics g) {
for(int i = 0; i < listDiy.getSize(); i++) {
Shape shape = listDiy.get(i);
String type = shape.getType();
int x1 = shape.getX1();
int y1 = shape.getY1();
int x2 = shape.getX2();
int y2 = shape.getY2();
if(type.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
}
if(type.equals("矩形")) {
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
if(type.equals("圆")) {
g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
}
}
public static void main(String[] args) {
Huaban huaban = new Huaban();
huaban.showUI();
}
}
2.鼠标监听器
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MouseListenerImp extends MouseAdapter {
// 定义属性
private java.awt.Graphics g;
private int x1, y1, x2, y2;
private String type;
private ListDiy listDiy;
//重载构造器,对对象进行初始化
public MouseListenerImp(java.awt.Graphics g, ListDiy listDiy) {
this.g = g;
this.listDiy = listDiy;
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 =e.getY();
if(type.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
}
if(type.equals("矩形")) {
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
if(type.equals("圆")) {
g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
Shape shape = new Shape(x1, y1, x2, y2);
shape.setType(type);
listDiy.add(shape);
}
public void setShape(String shape) {
this.type = shape;
}
}
3.动作监听器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ActionListenerImpl implements ActionListener{
private MouseListenerImp mouseListener; //?
public ActionListenerImpl(MouseListenerImp mouseListener) {
this.mouseListener = mouseListener;
}
@Override
public void actionPerformed(ActionEvent e) {
Object object = e.getSource();
JButton button = (JButton) object;//强制类型转换
System.out.println("按钮名称 : " + button.getText());
mouseListener.setShape(button.getText());
}
}
4.Shape类
public class Shape {
private int x1, y1, x2, y2; //起点终点的坐标值
private String type; //类型
public Shape(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
5.自定义队列
public class ListDiy {
private Shape[] older = new Shape[0];
public int getSize() {
return older.length;
}
public void add(Shape shape) {
Shape[] newOne = new Shape[getSize() + 1];
newOne[getSize()] = shape;
for(int i = 0; i < older.length; i++) {
newOne[i] = older[i];
}
older = newOne;
}
public Shape get(int i) {
return older[i];
}