画图板(二)
在上一个版本中,我们把所有的功能都写在一个类里面,这样有很多缺点,第一,不符合Java面向对象的思想,第二,代码都在一个类里面,不方便管理;在这个版本中,我们将对上一个版本进行代码重构,将每个功能模块抽出来,单独的形成一个类,优点是将功能分开,每个类的职责明确,提高协同效率,方便排错
我们将原来的一个类重构为四个类,主要是GrawBoard类(主要负责将每个功能模块整合到一个窗体中),PanelBottom类(底部面板,主要负责画笔颜色的设置),PanelLeft类(左边面板主要负责画笔应该画什么图形的选择),PanelCenter类(中间面板,主要负责画笔画图形)
- GrawBoard类
public class DrawBoard extends JFrame {
Graphics2D graphics = null;// 创建一个画笔
public DrawBoard() {
// 第一步:设置窗体属性
setFrameParams();// 窗体属性
// 第二步:添加组件
PanelLeft panelLeft = new PanelLeft(null);// 左边面板
this.add(panelLeft, BorderLayout.WEST);
PanelCenter panelCenter = new PanelCenter(panelLeft, null);// 中间面板
this.add(panelCenter, BorderLayout.CENTER);
PanelBottom panelBottom = new PanelBottom(panelLeft, null);// 底部面板
this.add(panelBottom, BorderLayout.SOUTH);
// 第三步:设置窗体可见
this.setVisible(true);
// 获得画笔
graphics = (Graphics2D) panelCenter.getGraphics();
panelLeft.graphics = graphics;
panelCenter.graphics = graphics;
panelBottom.graphics = graphics;
}
/**
* @Description 设置窗体属性
*/
public void setFrameParams() {
// 设置窗体的属性
this.setTitle("画图板");
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setLayout(new BorderLayout());// 布局
}
}
2、PanelLeft类
public class PanelLeft extends JPanel {
String currentCommand = "line";// 获取当前点击的是哪一个按钮
Color currentColor = Color.black;// 用于还原画笔颜色
int count = 0;// 记录多边形图形是不是画第一笔
Graphics2D graphics = null;
public PanelLeft(Graphics2D g) {
this.graphics = g;// 给画笔赋值
// 左边面板
this.setPreferredSize(new Dimension(70, 400));
this.setBackground(Color.white);
String[] value = { "star", "dot_rect", "eraser", "fill", "color_picker", "magnifier", "pencil", "brush",
"airbrush", "word", "line", "curve", "rect", "polygon", "oval", "round_rect" };
// 添加一些画图按钮组件
// 创建监听器
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentCommand = e.getActionCommand();// 获取当前按钮标识,命令
count = 0;// 当切换成其他组件的时候矩形第一笔还原
graphics.setColor(currentColor);// 还原画笔的颜色
}
};
for (int i = 0; i < value.length; i++) {
JButton btnIcon = new JButton(new ImageIcon("images/" + value[i] + ".jpg"));
btnIcon.setPreferredSize(new Dimension(24, 24));
// btn.setBorderPainted(false);
this.add(btnIcon);
btnIcon.addActionListener(actionListener);// 添加事件
btnIcon.setActionCommand(value[i]);// 给按钮一个标识,命令,用来区分这些按钮
}
}
}
3、PanelCenter类
public class PanelCenter extends JPanel {
// 画直线等图形的起始坐标
int xStart = 0;
int yStart = 0;
// 终止坐标
int xEnd = 0;
int yEnd = 0;
Graphics2D graphics = null;// 创建一个画笔
public PanelCenter(PanelLeft panelLeft, Graphics2D g) {
this.graphics = g;// 给画笔赋值
// 创建鼠标监听器
MouseListener mouseListener = new MouseListener() {
int xFirstStart = 0;// 记录起始坐标
int yFirstStart = 0;
int xCurrentStart = 0;// 记录当前起始坐标
int yCurrentStart = 0;
@Override
public void mouseReleased(MouseEvent e) {
// 释放鼠标
xEnd = e.getX();
yEnd = e.getY();
// 确定图形
switch (panelLeft.currentCommand) {
case "line":
// 直线
graphics.drawLine(xStart, yStart, xEnd, yEnd);
break;
case "curve":
// 曲线
break;
case "rect":
// 矩形
graphics.drawRect(Math.min(xStart, xEnd), Math.min(yStart, yEnd), Math.abs(xEnd - xStart),
Math.abs(yEnd - yStart));
break;
case "polygon":
// 多边形
if (panelLeft.count == 0) {
xFirstStart = xStart;// 保存起始坐标
yFirstStart = yStart;
xCurrentStart = xEnd;// 保存当前起始坐标
yCurrentStart = yEnd;
graphics.drawLine(xStart, yStart, xEnd, yEnd);
panelLeft.count++;
}
break;
case "oval":
// 椭圆
graphics.drawOval(Math.min(xStart, xEnd), Math.min(yStart, yEnd), Math.abs(xEnd - xStart),
Math.abs(yEnd - yStart));
break;
case "round_rect":
// 圆角矩形
graphics.drawRoundRect(Math.min(xStart, xEnd), Math.min(yStart, yEnd), Math.abs(xEnd - xStart),
Math.abs(yEnd - yStart), Math.abs(xEnd - xStart) / 10, Math.abs(yEnd - yStart) / 10);
break;
}
}
@Override
public void mousePressed(MouseEvent e) {
// 按下了鼠标
xStart = e.getX();
yStart = e.getY();
if (!("brush".equals(panelLeft.currentCommand) || "eraser".equals(panelLeft.currentCommand))) {
// 还原画笔宽度
graphics.setStroke(new BasicStroke(1));
}
if ("brush".equals(panelLeft.currentCommand) || "eraser".equals(panelLeft.currentCommand)) {
// 设置橡皮擦和刷子的宽度
graphics.setStroke(new BasicStroke(10));
}
if ("eraser".equals(panelLeft.currentCommand)) {
// 橡皮擦
graphics.setColor(Color.white);
}
}
@Override
public void mouseExited(MouseEvent e) {
// 移开了鼠标
}
@Override
public void mouseEntered(MouseEvent e) {
// 进入了
}
@Override
public void mouseClicked(MouseEvent e) {
// 鼠标点击了
if ("polygon".equals(panelLeft.currentCommand)) {
int clickCount = e.getClickCount();
if (clickCount == 1 && panelLeft.count == 1) {
// 点击一下
graphics.drawLine(xCurrentStart, yCurrentStart, xEnd, yEnd);
xCurrentStart = xEnd;
yCurrentStart = yEnd;
// System.out.println("单击");
} else if (clickCount == 2 && panelLeft.count == 1) {
// 点击两下
graphics.drawLine(xCurrentStart, yCurrentStart, xFirstStart, yFirstStart);
panelLeft.count = 0;
// System.out.println("双击");
}
}
}
};
// 创建鼠标拖动监听器
MouseMotionListener mouseMotionListener = new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// 移动鼠标(鼠标不按下的移动)
}
@Override
public void mouseDragged(MouseEvent e) {
// 拖拽鼠标(鼠标左键按下时的移动)
if ("pencil".equals(panelLeft.currentCommand)) {
// 铅笔
graphics.drawLine(xStart, yStart, e.getX(), e.getY());
xStart = e.getX();
yStart = e.getY();
} else if ("airbrush".equals(panelLeft.currentCommand)) {
// 喷枪
Random random = new Random();
for (int i = 0; i < 30; i++) {
int randomX = random.nextInt(10) - 5;
int randomY = random.nextInt(10) - 5;
graphics.drawLine(e.getX() + randomX, e.getY() + randomY, e.getX() + randomX,
e.getY() + randomY);
}
} else if ("brush".equals(panelLeft.currentCommand)) {
// 刷子
graphics.drawLine(xStart, yStart, e.getX(), e.getY());
xStart = e.getX();
yStart = e.getY();
} else if ("eraser".equals(panelLeft.currentCommand)) {
// 橡皮擦
graphics.drawLine(xStart, yStart, e.getX(), e.getY());
xStart = e.getX();
yStart = e.getY();
}
}
};
// 中间面板
this.setBackground(Color.white);
this.addMouseListener(mouseListener);// 鼠标点击事件
this.addMouseMotionListener(mouseMotionListener);
}
}
4、PanelBottom类
public class PanelBottom extends JPanel {
Graphics2D graphics = null;// 创建一个画笔
public PanelBottom(PanelLeft panelLeft, Graphics2D g) {
this.graphics = g;// 给画笔赋值
this.setPreferredSize(new Dimension(this.getWidth(), 70));// 设置面板大小
// 设置默认颜色
Color[] colors = { Color.black, Color.blue, Color.cyan, Color.DARK_GRAY, Color.gray, Color.green,
Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.RED, Color.WHITE, Color.yellow };
ActionListener actionListener = new ActionListener() {
// 添加按钮监听器
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
int i = Integer.valueOf(actionCommand);
graphics.setColor(colors[i]);
panelLeft.currentColor = graphics.getColor();// 获取画笔当前颜色,用于还原画笔颜色
}
};
for (int i = 0; i < colors.length; i++) {
JButton btnColor = new JButton();
btnColor.setBackground(colors[i]);// 设置按钮的背景颜色
btnColor.setPreferredSize(new Dimension(30, 30));// 设置按钮的大小
btnColor.addActionListener(actionListener);// 添加事件
btnColor.setActionCommand(i + "");// 设置每个按钮的标识,命令
this.add(btnColor);// 将按钮添加到面板
}
}
}