老师讲完简单画图板的制作之后,剩下的一些功能要求我们自己来实现。之前一直没有时间来完成这个作业,今天正好有点时间来完成就来做了一下。我自己觉得这里面一些简单的图形的绘制都还是挺简单的,因为在Graphices这个抽象父类中都提供了对应的方法,而我在JPanel中得到的是这个抽象类的子类,在得到的子类中已经实现了这些图形的绘制,唯一觉得难一点的就是多边形,虽然也提供了对应的方法,但是不能实现绘制任意多边形,所以我得自己想办法来实现。
关于实现绘制任意多边形,我的基本思路是:
当我点下绘制任意多边形的按钮时,第一条边是和绘制直线一样的实现,但是我得标记当前绘制的边是不是第一条边,后面就是每次点击那么绘制一条从上次点击的点到当前的点的直线即可。
我贴上我的实现代码,代码里写了一些注释。
画图板窗体类:
package com.why.draw;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 定义画板的窗体类,继承JFrame
*
* @author why
*
*/
public class DrawFrame extends JFrame {
private static final long serialVersionUID = 1L;
private String graphName = "Line";
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawFrame drawFrame = new DrawFrame();
drawFrame.init();
}
public void init() {
// JFrame的默认布局方式是边框布局
// 设置窗体的标题
this.setTitle("我的画板");
// 设置窗体大小
this.setSize(new Dimension(800, 600));
// 设置窗体的默认显示位置,当前设置为水平和竖直居中显示
this.setLocationRelativeTo(null);
// 设置点击关闭的按钮的响应动作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建JPanel容器对象
JPanel drawPanel = new JPanel();
// 设置画图区域的背景色
drawPanel.setBackground(Color.WHITE);
// 添加画图的区域
this.add(drawPanel, BorderLayout.CENTER);
// 调用创建上面工具栏的按钮
createToolsPanel();
// 设置窗体可见
this.setVisible(true);
/**
* 给绘图区域添加鼠标的监听器。
* 这里注意要得到JPanel的Graphics对象必须要在设置窗体可见之后才能取得,否则得不到该对象出现空指针的异常
*/
PanelListener panelListener = new PanelListener(this, drawPanel.getGraphics());
drawPanel.addMouseListener(panelListener);
}
private void createToolsPanel() {
JPanel toolsPanel = new JPanel();// JPanel的默认布局方式是流式布局
String[] array = { "Line", "Rect", "Oval", "RoundRect", "Polygon",
"FillRect", "FillOval" };
// 实例化一个按钮监听器对象
ButtonListener l = new ButtonListener();
for (int i = 0; i < array.length; i++) {
JButton btn = new JButton(array[i]);
// 给按钮添加监听器
btn.addActionListener(l);
// 向上面添加按钮组件
toolsPanel.add(btn);
}
// 将工具容器添加到JFrame中
this.add(toolsPanel, BorderLayout.NORTH);
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
graphName = e.getActionCommand();
System.out.println("按下的按钮是" + graphName);
}
}
public String getGraphName() {
return graphName;
}
public void setGraphName(String graphName) {
this.graphName = graphName;
}
}
画图区域监听器类:
package com.why.draw;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class PanelListener implements MouseListener {
private int x1;
private int y1;
private int x2;
private int y2;
private boolean flag;// 用以标记在按下绘制多边形按钮的时候是不是绘制第一条边
private DrawFrame drawFrame;// 画板窗体类
private Graphics graphics;// 绘图区域的图形上下文
public PanelListener(DrawFrame drawFrame, Graphics graphics) {
this.drawFrame = drawFrame;
this.graphics = graphics;
this.flag = false;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("当鼠标单击事件源的时候响应的方法");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("当鼠标进入事件源的时候执行的方法");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("当鼠标离开事件源的时候执行的方法");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("当鼠标在事件源上按下的时候执行的方法");
if (drawFrame.getGraphName().equals("Polygon") && !flag) {// 如果按下的是绘制多边形按钮
x1 = e.getX();
y1 = e.getY();
flag = true;
} else if (!drawFrame.getGraphName().equals("Polygon")) {// 如果按下的不是绘制多边形按钮
x1 = e.getX();
y1 = e.getY();
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("当鼠标在事件源上释放的时候执行的方法");
System.out.println(e.getX() + " " + e.getY());
x2 = e.getX();
y2 = e.getY();
String graphName = drawFrame.getGraphName();
if (graphName.equals("Line")) {
graphics.drawLine(x1, y1, x2, y2);
flag = false;
} else if (graphName.equals("Rect")) {
graphics.drawRect(x1, y1, x2 - x1, y2 - y1);
flag = false;
} else if (graphName.equals("Oval")) {
graphics.drawOval(x1, y1, x2 - x1, y2 - y1);
flag = false;
} else if (graphName.equals("RoundRect")) {
graphics.drawRoundRect(x1, y1, x2 - x1, y2 - y1, 15, 15);
flag = false;
} else if (graphName.equals("Polygon")) {
graphics.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
} else if (graphName.equals("FillRect")) {
graphics.fillRect(x1, y1, x2 - x1, y2 - y1);
flag = false;
} else if (graphName.equals("FillOval")) {
graphics.fillOval(x1, y1, x2 - x1, y2 - y1);
flag = false;
}
}
}
这个效果图