一,界面的实现:
1.首先我们需要一个简单的界面,这个很简单我们只需要定义一个类来继承JFrame组件类即可
public class Draw extends JFrame {
public static void main(String[] args) {
Draw dr=new Draw();
dr.initUI();
}
public void initUI(){
this.setTitle("画图程序");
this.setSize(700, 500);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
FlowLayout f = new FlowLayout();
this.setLayout(f);//将f设置为窗体的布局方式
this.setVisible(true);
}
二,事件监听方法的使用
我们为了得到鼠标在界面上点击得到的坐标来画一条直线,这时候就需要使用到事件监听机制
事件监听机制包括三部分:1.事件源对象2.事件监听方法3.事件接口
我们这里的事件源对象是:窗体
需要用到的监听方法是:addMouseListener(MouseListener l)
事件接口是:MouseListener
1.首先我们先定义一个DrawListener事件处理类,由该类实现MouseListener鼠标事件接口,并重写接口中的所有抽象方法
2.实例化DrawListener事件处理类对象()
3.给窗体事件源对象添加addMouseLisrenerdl(),给监听方法指定事件处理类对象为dl
public class DrawListener implements MouseListener,MouseMotionListener{
public void mouseClicked(MouseEvent e){
}
public void mousePressed(MouseEvent e){
//鼠标按下时获得坐标
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){
//鼠标释放时获得坐标
x2=e.getX();
y2=e.getY();
//调用画图的方法
g.drawLine(x1, y1, x2, y2);//画直线
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
//实例化DrawListener对象
DrawListener dl=new DrawListener();
//获取窗体上的画笔对象
Graphics g =this.getGraphics();
//将g传递给dl对象g的属性
dl.setG(g);
//给事件源对象添加监听方法(addMouseListener())给监听方法指定事件处理类对象为dl。
this.addMouseListener(dl);
this.addMouseMotionListener(dl);
然后我们就能监听到鼠标按下和释放时的两组坐标(x1,y1)和(x2,y2)
这时我们就可以调用DrawLine方法画出一条直线
同样的我们也可以通过这个监听方法来画矩形、圆、圆角矩形、弧、文字、图片、填充圆、填充弧、填充3D矩形、等腰三角形、任意多边形。
//g.drawLine(x1, y1, x2, y2);//画直线
//g.drawRect(x1, y1, x2-x1, y2-y1);//画矩形
//g.drawOval(x1, y1, x2-x1, y2-y1);//画椭圆
//g.drawOval(x1, y1, x2-x1, x2-x1);//画圆形
//g.drawRoundRect(x1, y1, x2-x1, y2-y1,1 , 1);//画圆角矩形
//g.drawArc(x1, y1, x2-x1, x2=x1, 40, 140);
//g.drawString("戴港", x1, y1);//文字
//g.drawOval(x1, y1, x2-x1, x2-x1);//画填充圆
//g.fillOval(x1, y1, x2-x1, x2-x1);//画填充圆
//g.draw3DRect(x1, y1, x2-x1, y2-y1, false);//画填充3D矩形
//g.fill3DRect(x1, y1, x2-x1, y2-y1, false);//画填充3D矩形
//g.drawLine(x1, y2, (x2+x1)/2, y1);//画等腰三角形
//g.drawLine(x1, y2, x2, y2);//画等腰三角形
//g.drawLine((x1+x2)/2, y1, x2, y2);//画等腰三角形
//javax.swing.ImageIcon icon =new javax.swing.ImageIcon("C:\\Users\\13973\\Desktop\\1\\IMG_8354.JPG");
//g.drawImage( icon.getImage(), x1, y1, null);//画图片
三,更多功能实现
接下来我们将使画图板通过点击按钮,来选择画的图像,颜色以及曲线,刷子,橡皮擦等新功能
我们需要添加按钮至窗体上,添加的数量太多,为了减少代码的冗余我们使用数组和循环来添加
为了实现新的功能,我们需要新增的API类有:
JButton 按钮类
ActionListener 动作事件监听
ActionEvent 动作事件接口
Color 颜色类
FlowLayout 流式布局
Dimension 组件的高度和宽度类
数组
MouseMotionListener 鼠标移动事件接口,提供处理移动和拖动动作的方法。MouseListener 鼠标事件接口,提供处理按下,释放,点击,进入和离开动作的方法。MouseAdapter 鼠标适配器类,该类实现MouseListener,MouseMotionListener,MouseWheelListener。
Graphics2D 画笔类,是Graphics的子类,提供了画图方法,设置画笔样式的方法。
Stroke|BasicStroke 线条粗细的类。
RenderingHints 设置画笔样式的类
Random 随机数类
下面我们以直线,多边形和曲线来举例:
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("")){
JButton button=(JButton)e.getSource();//获取事件源对象
co=button.getBackground();
}
else {
command=e.getActionCommand();//得到按钮上的字符
}
}
public void mouseClicked(MouseEvent e){
System.out.println("鼠标点击了窗体!");
p=e.getClickCount();
if(p==2&&command.equals("任意多边形")){
g.drawLine(x3, y3, x1, y1);
x1=0;x2=0;x3=0;
y1=0;y2=0;y3=0;
}
}
public void mousePressed(MouseEvent e){
System.out.println("鼠标按下了!");
x1=e.getX();
y1=e.getY();
//获得新的x1的值之后,再画下一条
if(x2>0&&y2>0&&command.equals("任意多边形")){
g.drawLine(x2, y2, x1, y1);
}
if(x2<1&&y2<1&&command.equals("任意多边形")){
System.out.println(x3);
System.out.println(y3);
x3=e.getX();
y3=e.getY();
}
}
public void mouseReleased(MouseEvent e){
System.out.println("鼠标释放了!");
x2=e.getX();
y2=e.getY();
//调用画图的方法
if(command.equals("直线")){
g.setColor(co);
g.drawLine(x1, y1, x2, y2);//画直线
}
else if(command.equals("任意多边形")){
g.setColor(co);
g.drawLine(x1, y1, x2, y2);//画多边形
}
else if(command.equals("图片")){
g.setColor(co);
javax.swing.ImageIcon icon =new javax.swing.ImageIcon("C:\\Users\\13973\\Desktop\\1\\IMG_8354.JPG");
g.drawImage(icon.getImage(), x1, y1, null);//画图片
}
}
public void mouseDragged(MouseEvent e){
if(command.equals("刷子")){
x=e.getX();
y=e.getY();
g.setColor(co);//设置画笔颜色
BasicStroke s = new BasicStroke(20, BasicStroke.CAP_BUTT, BasicStroke.CAP_ROUND, 10.0f);
g.setStroke(s);
g.drawLine(x1, y1, x, y);
x1=x;
y1=y;
}
else if(command.equals("曲线")){
x=e.getX();
y=e.getY();
g.setColor(co);
g.drawLine(x1, y1, x, y);
x1=x;
y1=y;
}
}
到这里我们简单画图板大部分功能就已经实现了,但是在实际测试过程中却发现,当我们改变窗体大小之后,我们之前所画出的图形会消失,我的下一篇博客将去介绍如何解决这个问题。