P5 画板与图形的绘制

画板与图形的的绘制

创建一个窗体,并加上按钮

  • 注意加上流式布局
public class DrawPad {
    public void initUI(){
        JFrame jf =new JFrame();
        jf.setTitle("画图");
        jf.setSize(800,600);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout fl = new FlowLayout();
        jf.setLayout(fl);
        JButton btn1 =new JButton();
        JButton btn2 = new JButton();
        JButton btn3 = new JButton();
        btn1.setText("直线");
        btn2.setText("矩形");
        btn3.setText("五角星");
        jf.add(btn1);
        jf.add(btn2);
        jf.add(btn3);
        jf.setVisible(true);
    }
    public static void main(String[] args){
        DrawPad dp= new DrawPad();
        dp.initUI();
    }
}

Graphics 图形绘制类

  • 绘制直线 矩形 圆 等
  • 在窗体上绘制,就需要从窗体对象上获取 Graphics 对象
  • Graphics g = jf.getGraphics();
  • g.drawLine(x1,y1,x2,y2);// 需要四个整数 两组坐标

监听器

按钮监听器

public class DrawListener implements ActionListener{
    String btnstr;//定义一个字符串的全局变量,用来点击按钮方法中获取按钮的字符串,
                  //用来在释放时绘制对应的图案。
    public void actionPerformed(ActionEvent e){
        btnstr=e.getActionCommand();//获取按钮中的字符串变量赋值给btnstr
        System.out.println("点击的按钮是:"+btnstr);
    }
}

鼠标监听器

  • 需要用到点击和释放两个方法

  • MouseEvent : 鼠标事件

  • 每一个鼠标操作的方法中都有这个参数
    MouseEvent e 可以获取鼠标操作的时坐标

int x = e.getX();
int y = e.getY();

int x1,y1,x2,y2;
  • 按下中:
x1= e.getX();
y1 = e.getY();
  • 释放中:
x2 = e.getX();
y2 = e.getY();
  • 绘制直线:
g.drawLine(x1,y1,x2,y2);
  • 绘制矩形:
 gr.drawLine(x1,y1,x1,y2);
 gr.drawLine(x1,y2,x2,y2);
 gr.drawLine(x2,y2,x2,y1);
 gr.drawLine(x1,y1,x2,y1);
  • 绘制五角星
x3 = (x1 + x2) / 2;
y3 = y1;
x4 = x1;
y4 = (y1 + y2) / 2;
x5 = x2;
y5 = (y1 + y2) / 2;
gr.drawLine(x1, y2, x3, y3);
gr.drawLine(x3, y3, x2, y2);
gr.drawLine(x4, y4, x5, y5);
gr.drawLine(x4, y4, x2, y2);
gr.drawLine(x1, y2, x5, y5);
  • 将鼠标监听器和按钮监听器合起来用if语句
public class DrawListener implements ActionListener,MouseListener{
    Graphics gr;
    String btnstr;//定义一个字符串的全局变量,用来点击按钮方法中获取按钮的字符串,
                  //用来在释放时绘制对应的图案。
    int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5;
    public void actionPerformed(ActionEvent e){
        btnstr=e.getActionCommand();//获取按钮中的字符串变量赋值给btnstr
        System.out.println("点击的按钮是:"+btnstr);
    }

    public void mouseClicked(MouseEvent e){}


    public void mousePressed(MouseEvent e){
        x1=e.getX();
        x2=e.getY();
    }


    public void mouseReleased(MouseEvent e){
        x2=e.getX();
        y2=e.getY();
        if(btnstr.equals("五角星")){
            x3 = (x1 + x2) / 2;
            y3 = y1;
            x4 = x1;
            y4 = (y1 + y2) / 2;
            x5 = x2;
            y5 = (y1 + y2) / 2;
            gr.drawLine(x1, y2, x3, y3);
            gr.drawLine(x3, y3, x2, y2);
            gr.drawLine(x4, y4, x5, y5);
            gr.drawLine(x4, y4, x2, y2);
            gr.drawLine(x1, y2, x5, y5);
        }
        if(btnstr.equals("直线")){
            gr.drawLine(x1,y1,x2,y2);}
        if(btnstr.equals("矩形")){
            gr.drawLine(x1,y1,x1,y2);
            gr.drawLine(x1,y2,x2,y2);
            gr.drawLine(x2,y2,x2,y1);
            gr.drawLine(x1,y1,x2,y1);
        }
    }


    public void mouseEntered(MouseEvent e){}


    public void mouseExited(MouseEvent e){}
}

将监听器添加到窗体和按钮上

        DrawListener dl = new DrawListener();
        jf.addMouseListener(dl);
        btn1.addActionListener(dl);
        btn2.addActionListener(dl);
        btn3.addActionListener(dl);

在窗体对象上获取Graphics对象 并将对象传递给监听器

        Graphics g = jf.getGraphics();
        dl.gr=g;//将获取到的g 传递给 监听器中的gr
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值