使用Java设计简单的画图工具

一、步骤:

1、显示绘图界面,包括一些功能按钮;

2、设置监听器,这里是鼠标监听器;

3、添加画笔:显示绘制内容。

1、显示绘图界面:

JFrame jf = new JFrame();
jf.setTitle("画图1.0");
jf.setSize(800,700);
jf.setLocationRelativeTo(null);//居中显示
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new FlowLayout());

注:FlowLayout为流式布局

显示出一个界面:

2、给窗体添加鼠标监听器

首先定义事件处理类DrawMouse重写MouseListener接口:

public class DrawMouse implements MouseListener

MouseListener中有五个函数分别是:

分别是点击鼠标、按下鼠标、释放鼠标、进入窗体、退出窗体

注意按下鼠标和点击鼠标的区别:

点击鼠标表示按下鼠标并且释放鼠标,整个过程鼠标没有移动,如果移动了,就不叫点击鼠标了。

DrawMouse mouse = new DrawMouse();//定义事件处理类对象
jf.addMouseListenser(mouse);//给界面添加鼠标监听器

这样就给界面设置了鼠标监听器。

3、添加画笔

jf.setVisible(true);//首先要设置页面可见
Graphics g = jf.getGraphics();//给界面增加一个画笔
mouse.SetGr(g);//将该对象g传给mouse对象的画笔对象

我们查看getGraphics函数,发现其返回值是一个Graphics对象

所以可以用语句:Graphics g=jf.getGraphics()定义画笔。

(1)画直线

确定一条直线需要两个坐标(x1,y1),(x2,y2),所以要怎样得到这两个坐标呢?

在按下鼠标时会有第一个坐标(x1,y1),所以我们在mousePressed函数中调用get函数获取坐标:

public void mousePressed(MouseEvent e){
       x1 = e.getX();//获取x坐标
       y1 = e.getY();//获取y坐标
}

同理,在释放鼠标时会有第二个坐标(x2,y2),所以我们在mouseReleased函数中调用get函数获取坐标:

public void mouseReleased(MouseEvent e){
       x2 = e.getX();//获取x坐标
       y2 = e.getY();//获取y坐标
}

这时再在mouseReleased函数中调用库函数drawLine,drawline有四个参数x1,x2,y1,y2,其中(x1,y1)和(x2,y2)分别表示直线的两个端点的坐标:

public abstract void drawLine(int x1, int y1, int x2, int y2)

运行效果:

 (画出直线)

(2)画矩形

同理,确定一个矩形需要一个顶点,长和高(因为不能斜着画),调用drawRect函数可以画出矩形,该函数有四个函数x1,y1,width,height,分别表示起始顶点的坐标,宽,高:

public void drawRect(int x, int y, int width, int height)

怎么得到宽和高呢?很简单,在按下鼠标时获取第一个坐标(x1,y1),释放鼠标时获取第二个坐标(x2,y2),width=x1-x2的绝对值,height=y1-y2的绝对值。

width = Math.abs(x1 - x2);
height = Math.abs(y1 - y2);

运行效果:

 //画出一个矩形

(3)、画椭圆

画椭圆需要调用drawOval函数:查看API

 x和y代表椭圆外接矩形的左上角横纵坐标,width和height分别代表外接矩形的宽高

//画出一个椭圆

大家可以参照上述步骤试试怎么画等腰三角形、直角三角形(系统没有直接提供等腰三角形、直角三角形的函数,需要自己设计)。

二、其他问题

现在我们在mouseReleased函数定义了画直线、画矩形、画椭圆的函数,但是却不知道你究竟是想画直线、矩形还是椭圆,所以就会出现下面的情况:

我们可以加入三个按钮(“直线”、“矩形”、“椭圆”):

JButton j1 = new JButton("直线");
jf.add(j1);
JButton j2 = new JButton("矩形");
jf.add(j2);
JButton j3 = new JButton("椭圆");
jf.add(j3);

同时让DrawMouse(事件处理类继承ActionListener接口),这样一来DrawMouse就继承了两个接口:(类可以继承多个接口,但不能继承多个类,这样事件处理类DrawMouse既可以实现鼠标监听器的功能,又可以实现动作监听器的功能)

public class DrawMouse implements MouseListener,  ActionListener

给三个按钮加上监听器:

j1.addActionListener(mouse);
j2.addActionListener(mouse);
j3.addActionListener(mouse);

接下来重写ActionListener里的函数:

public void actiobPerformed(ActionEvent e){
      str = e.getActionCommand();
}

//getActionCommand的返回值是一个String类型,返回按钮里的字符串,比如直线按钮就返回一个字符串“直线”。

接下来就可以用条件语句判断:

if(str.equals("直线")) gr.drawLine(x1, y1, x2, y2);//直线
else if(str.equals("矩形")) gr.drawRect(x1, y1, width, height);//矩形
else if(str.equals("椭圆")) gr.drawOval(x1, y1, width, height);//椭圆

注意Java里的字符串比较不能用==,而是要用equals函数。

完整代码:

DrawUI类:

package p1;

import javax.swing.*;
import java.awt.*;

public class DrawUI extends JFrame {
    public void init(){
        DrawMouse mouse = new DrawMouse();
        JFrame jf = new JFrame();
        jf.setTitle("画图1.0");
        jf.setSize(800,700);
        jf.setLocationRelativeTo(null);//居中显示
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLayout(new FlowLayout());

        String[] string = new String[]{"直线","矩形","椭圆"};
        JButton[] jButton = new JButton[string.length];
        for (int i=0;i< jButton.length;i++){
            jButton[i] = new JButton(string[i]);
            jf.add(jButton[i]);
            jButton[i].addActionListener(mouse);
        }

        jf.setVisible(true);
        Graphics g = jf.getGraphics();
        jf.addMouseListener(mouse);
        mouse.setG(g);
    }

    public static void main(String[] args) {
        DrawUI D = new DrawUI();
        D.init();
    }
}

DrawMouse类:

package p1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class DrawMouse implements MouseListener, ActionListener {
    private String str;
    private int x1,y1,x2,y2;
    private Graphics g;
    public void setG(Graphics g) {
        this.g = g;
    }
    public void actionPerformed(ActionEvent e){
        str = e.getActionCommand();
    }
    public void mouseClicked(MouseEvent e){}

    /**
     * Invoked when a mouse button has been pressed on a component.
     * @param e the event to be processed
     */
    public void mousePressed(MouseEvent e){
        x1 = e.getX();
        y1 = e.getY();
    }

    /**
     * Invoked when a mouse button has been released on a component.
     * @param e the event to be processed
     */
    public void mouseReleased(MouseEvent e){
        x2 = e.getX();
        y2 = e.getY();
        if(str.equals("直线")){
            g.drawLine(x1,y1,x2,y2);
        }
        else if (str.equals("矩形")){
            int width = Math.abs(x2-x1);
            int height = Math.abs(y2-y1);
            g.drawRect(x1,y1,width,height);
        }
        else if(str.equals("椭圆")){
            int width = Math.abs(x2-x1);
            int height = Math.abs(y2-y1);
            g.drawOval(x1,y1,width,height);
        }
    }

    /**
     * Invoked when the mouse enters a component.
     * @param e the event to be processed
     */
    public void mouseEntered(MouseEvent e){}

    /**
     * Invoked when the mouse exits a component.
     * @param e the event to be processed
     */
    public void mouseExited(MouseEvent e){}
}

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值