学习了JColorChooser,JFileChooser,以及图形界面的一些知识,做个简易的画图板巩固练习
画图类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JFrame{
//将一些常用量定义为属性
private String item = "Line";//默认画直线
private Color color = Color.BLACK;//默认是用黑色
//主函数部分
public static void main(String[] args) {
Draw draw = new Draw();
draw.init();
}
//初始化界面的方法
public void init(){
//设置窗体的属性
this.setTitle("这是一个简单的画图板");
this.setSize(600,500);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setLocationRelativeTo(null);
//创建画图面板
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
this.add(panel,BorderLayout.CENTER);
//创建简单的工具面板的对象
ToolPanel tp = new ToolPanel();
this.add(tp, BorderLayout.WEST);
this.setVisible(true);
//面板上获取Graphics对象,用于相应图形的绘制
//技巧:Graphics对象必须在窗体可见之后才可获取到
Graphics gh = panel.getGraphics();
//创建画图监听事件处理类对象dl
DrawListener dl = new DrawListener(gh,this);
//将创建好的鼠标监听器添加到面板上
panel.addMouseListener(dl);
//将创建好的接收组件上的鼠标移动事件的监听器添加到面板上
panel.addMouseMotionListener(dl);
}
//定义一个内部类,此类用于创建一个简单的工具面板
class ToolPanel extends JPanel{
public ToolPanel(){
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(100,200));
//定义一个匿名内部类,用于创建一个动作监听器
//实例化一个动作监听器al
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e){
//判断点击的是否是color按钮
if(e.getActionCommand().equals("Color")){
color = JColorChooser.showDialog(null,"选择颜色",Color.BLACK);
System.out.println(color);
}else{
//获取选择的图形
item = e.getActionCommand();
}
}
};
//创建按钮
JButton btnLine = new JButton("Line");
JButton btnRect = new JButton("Rect");
JButton btnOval = new JButton("Oval");
JButton btnCurve = new JButton("Curve");
JButton btnColor = new JButton("Color");
//添加按钮
panel.add(btnLine);
panel.add(btnRect);
panel.add(btnOval);
panel.add(btnColor);
panel.add(btnCurve);
// 往按钮上添加监听器
btnLine.addActionListener(al);
btnRect.addActionListener(al);
btnOval.addActionListener(al);
btnColor.addActionListener(al);
btnCurve.addActionListener(al);
// 将面板添加到窗体上
this.add(panel);
}
}
// 根据动作得到要画图形的指令
public String getItem() {
return item;
}
// 根据颜色选择界面的选择情况确定接下来绘制的颜色
public Color getColor() {
return color;
}
}
监听类
import java.awt.*;
import java.awt.event.*;
public class DrawListener implements MouseListener,MouseMotionListener {
// 定义存储坐标值的变量
private int x1, y1, x2, y2;
// 定义一个画图形的对象
private Graphics g;
// 获取图形和颜色的对象
private Draw draw;
/**
* 定义带参数的构造函数
*/
public DrawListener(Graphics g, Draw draw) {
this.g = g;
this.draw = draw;
}
/**
* 鼠标在事件源上按下时执行的方法
*/
public void mousePressed(MouseEvent e) {
// 获取第一个点的坐标值
x1 = e.getX();
y1 = e.getY();
}
/**
* 鼠标在事件源上释放时执行的方法
*/
public void mouseReleased(MouseEvent e) {
// 获取第二个点的坐标值
x2 = e.getX();
y2 = e.getY();
// 设置图形的颜色
g.setColor(draw.getColor());
// 判断选择的图形
if (draw.getItem().equals("Line")) {
// 画直线
g.drawLine(x1, y1, x2, y2);
} else if (draw.getItem().equals("Rect")) {
// 画矩形
if (x2 > x1 && y2 > y1) {
g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (x2 < x1 && y2 < y1) {
g.drawRect(x2, y2, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (x2 > x1 && y2 < y1) {
g.drawRect(x1, y2, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (x2 < x1 && y2 > y1) {
g.drawRect(x2, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
}
} else if (draw.getItem().equals("Oval")) {
// 画圆
if (x2 > x1 && y2 > y1) {
g.drawOval(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (x2 < x1 && y2 < y1) {
g.drawOval(x2, y2, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (x2 > x1 && y2 < y1) {
g.drawOval(x1, y2, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (x2 < x1 && y2 > y1) {
g.drawOval(x2, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
}
} else if (draw.getItem().equals("Curve")) {
// 画曲线
g.drawOval(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
/**
* 鼠标在事件源上单击时执行的方法
*/
public void mouseClicked(MouseEvent e) {
}
/**
* 鼠标进入事件源时执行的方法
*/
public void mouseEntered(MouseEvent e) {
}
/**
* 鼠标退出事件源时执行的方法
*/
public void mouseExited(MouseEvent e) {
}
@Override
// 重写父类的方法
public void mouseDragged(MouseEvent e) {
// 获取第二个点的坐标值
x2 = e.getX();
y2 = e.getY();
// 设置图形的颜色
g.setColor(draw.getColor());
// 判断选择的图形
if (draw.getItem().equals("Curve")) {
// 画曲线
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
public void mouseMoved(MouseEvent e) {
}
}