Java学习之路-GUI-画板的超级简单Demo

完成鼠标绘直线和圆
package GUI;

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 DrawingBoardTest{

    public String mode;
    public Color color;
    public int x_s, x_e, y_s, y_e;
    public JPanel jp;
    public JFrame jf;
    public JMenuBar jMenuBar;
    public JMenu jMenu;
    public JMenuItem jMenuItem_Color, jMenuItem_Line, jMenuItem_Circle;

    public DrawingBoardTest(){

        mode = "直线画笔";
        color = new Color(0, 0, 0);

        jf = new JFrame("画板");
        jf.setSize(800, 600);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLayout(new FlowLayout());
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);

        jp = new JPanel(null){
            public void paint(Graphics graphics){

                graphics.setColor(color);

                System.out.println(color + "," + mode);
                if(mode.equals("椭圆画笔")){
                    graphics.drawOval(x_s, y_s, Math.abs(x_e - x_s), Math.abs(y_e - y_s));
                }else if(mode.equals("直线画笔")){
                    graphics.drawLine(x_s, y_s, x_e, y_e);
                }

            }

        };

        jp.setPreferredSize(new Dimension(800,500));

        jMenuBar = new JMenuBar();

        jMenu = new JMenu("选项");

        jMenuItem_Color = new JMenuItem("选择颜色");
        jMenuItem_Line = new JMenuItem("直线画笔");
        jMenuItem_Circle = new JMenuItem("椭圆画笔");

        MenuL ml = new MenuL();

        jMenuItem_Color.addActionListener(ml);
        jMenuItem_Line.addActionListener(ml);
        jMenuItem_Circle.addActionListener(ml);

        jp.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent mouseEvent) {

            }

            @Override
            public void mousePressed(MouseEvent mouseEvent) {
                x_s = mouseEvent.getX();
                y_s = mouseEvent.getY();
            }

            @Override
            public void mouseReleased(MouseEvent mouseEvent) {
                x_e = mouseEvent.getX();
                y_e = mouseEvent.getY();
                jp.repaint();
            }

            @Override
            public void mouseEntered(MouseEvent mouseEvent) {

            }

            @Override
            public void mouseExited(MouseEvent mouseEvent) {

            }
        });

        jMenuBar.add(jMenu);
        jMenu.add(jMenuItem_Color);
        jMenu.add(jMenuItem_Line);
        jMenu.add(jMenuItem_Circle);

        jf.setJMenuBar(jMenuBar);
        jf.setContentPane(jp);
        jf.setVisible(true);
    }

    class MenuL implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if(actionEvent.getActionCommand().equals("选择颜色")){
                // 显示颜色选取器对话框, 返回选取的颜色(线程将被阻塞, 直到对话框被关闭)
                color = JColorChooser.showDialog(jp, "选取颜色", null);

                // 如果用户取消或关闭窗口, 则返回的 color 为 null
                if (color == null) {
                    return;
                }
            } else {
                mode = actionEvent.getActionCommand();
            }
        }
    }

    public static void main (String[] args){
        new DrawingBoardTest();
    }
}
就为了完成任务, 完完全全是个残次品, 虽然是个简单的东西,
却一直在反复调试, 以至于肝了几个小时
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注