用java的swing工具包绘制简单画图板

    首先建包,在其中引入swing和awt工具包,通过使用其中的类来构建界面应用程序和完成画图板内容;

package yyy1110;


import javax.swing.*;
import java.awt.*;
创建画板实现图形的正常绘制

1、创建窗口对象,创建面板。

public class DrawPad {
    DrawListener dl=new DrawListener();
    public void showUI(){
        JFrame jf=new JFrame();
        jf.setTitle("画图板pro");
        jf.setSize(600,600);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLayout(new FlowLayout());
 2、利用数组添加图形和颜色按钮(方便后续修改)。
String[] btnstrs={"直线", "圆形","矩形","实心圆形","实心矩形","三角形","多边形"};
        for (int i = 0; i < btnstrs.length; i++) {
            JButton btn=new JButton(btnstrs[i]);
            jf.add(btn);
            btn.addActionListener(dl);
        }
        Color[] colors={Color.BLACK,Color.BLUE,Color.CYAN,Color.GRAY,Color.GREEN,Color.magenta,Color.WHITE};
        for (int i = 0; i < colors.length; i++) {
            JButton btn=new JButton("color");
            btn.setBackground(colors[i]);
            btn.setPreferredSize(new Dimension(30,30));
            jf.add(btn);
            btn.addActionListener(dl);
        }
jf.addMouseListener(dl);

上列代码中的

DrawListener dl=new DrawListener();
btn.addActionListener(dl);
btn.addActionListener(dl);
jf.addMouseListener(dl);

为创建监听器和给按钮添加监听器对象,根据监听事件执行相应功能(画“直线”、“曲线”等)。

3、为画板添加画板和画笔,画笔是java中有的,直接获得就可以使用(先让窗体可视化)。

        jf.setVisible(true);
        Graphics g1= jf.getGraphics();
        dl.g1=g1;
}

 4、为主界面添加主函数

public static void main(String[] args){
        DrawPad dp=new DrawPad();
        dp.showUI();
    }

}

 <2> 创建接口,监听事件

1、重新引入类名,创建接口,定义未知数(在创建接口时要写出所有包含的方法)

package yyy1110;

import javax.swing.*;
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 DrawListener implements MouseListener, ActionListener {
    Graphics g1;
    int x1,y1,x2,y2;
    String shapeType = "";

2、在接口监听事件中,完善代码(图形绘制)—通过判断按钮上的文字(“直线”,“曲线”)来执行相应代码

@Override
    public void actionPerformed(ActionEvent e) {
        String ac=e.getActionCommand();
        if(ac.equals("color")){
            JButton btn=(JButton)e.getSource();
            Color color=btn.getBackground();
            g1.setColor(color);
        }
        else{
        shapeType=ac;
        }
        System.out.println(ac);



    }
    int x3, y3, x4, y4, x5, y5;
    int count = 0;
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("点击");
            int x = e.getX();
            int y = e.getY();
            if(shapeType.equals("三角形")){
            if (count == 0) {
                x3 = x;
                y3 = y;
                count++;
            } else if (count == 1) {
                x4 = x;
                y4 = y;
                g1.drawLine(x3, y3, x4, y4);
                count++;
            } else if (count == 2) {
                x5 = x;
                y5 = y;
                g1.drawLine(x3, y3, x5, y5);
                g1.drawLine(x5, y5, x4, y4);
                count = 0;
            }
            }
            //多边形: 多次点击 不确定边数 也就不确定需要的坐标数量
            if(shapeType.equals("多边形")){
            if (count == 0) {
                x3=x;
                y3=y;
                count++;
            } else if (count==1) {
                x4 = x;
                y4 = y;
                g1.drawLine(x3, y3, x4, y4);
                count++;
            } else if (count == 2) {
                x5 = x;
                y5 = y;
                if (abs(x3-x5)<15&&abs(y3-y5)<15){
                    g1.drawLine(x3,y3,x4,y4);
                    count=0;
                    return;
                }


                g1.drawLine( x4, y4,x5, y5);
                x4=x5;y4=y5;


            }
            }

    }




    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("按下");
        x1=e.getX();
        y1=e.getY();

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("松开");
        x2 = e.getX();
        y2 = e.getY();
        if (shapeType.equals("直线")) {
            g1.drawLine(x1, y1, x2, y2);
        }
        if (shapeType.equals("矩形")) {
            g1.drawRect(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
        }
        if (shapeType.equals("圆形")) {
            g1.drawOval(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
        }
        if (shapeType.equals("实心矩形")) {
            g1.fillRect(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
        }
        if (shapeType.equals("实心圆形")) {
            g1.fillOval(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
        }

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("进入");

    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("离开");

    }

、定义一些简单方法,协助上列图形的绘制

public int abs(int n) {
        if (n < 0) {
            return -n;
        }
        return n;
    }
    public int min(int a, int b) {
        return a < b ? a : b;
    }
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java SwingJava平台的一部分,是一组GUI类库,可以用于创建桌面应用程序。下面是一个简单Java Swing画图板的实现: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawingBoard extends JFrame implements ActionListener, MouseListener, MouseMotionListener { private int x, y, x1, y1; private JButton clearButton, redButton, greenButton, blueButton; private JPanel buttonPanel, drawingPanel; private Graphics g; public DrawingBoard() { super("Drawing Board"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonPanel = new JPanel(); clearButton = new JButton("Clear"); redButton = new JButton("Red"); greenButton = new JButton("Green"); blueButton = new JButton("Blue"); buttonPanel.add(clearButton); buttonPanel.add(redButton); buttonPanel.add(greenButton); buttonPanel.add(blueButton); drawingPanel = new JPanel(); drawingPanel.setBackground(Color.WHITE); drawingPanel.addMouseListener(this); drawingPanel.addMouseMotionListener(this); Container contentPane = getContentPane(); contentPane.add(buttonPanel, BorderLayout.NORTH); contentPane.add(drawingPanel, BorderLayout.CENTER); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == clearButton) { g.setColor(Color.WHITE); g.fillRect(0, 0, drawingPanel.getWidth(), drawingPanel.getHeight()); } else if (e.getSource() == redButton) { g.setColor(Color.RED); } else if (e.getSource() == greenButton) { g.setColor(Color.GREEN); } else if (e.getSource() == blueButton) { g.setColor(Color.BLUE); } } public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); } public void mouseDragged(MouseEvent e) { x1 = e.getX(); y1 = e.getY(); g.drawLine(x, y, x1, y1); x = x1; y = y1; } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public static void main(String[] args) { new DrawingBoard(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值