Java-简单绘图应用程序

Java-简单绘图应用程序,java,swing,awt,Java,Swing,Awt,我需要做一个简单的绘图应用程序,能够画一条线,一个矩形和2个用户给定点之间的圆 确切的应用程序行为应如下所示: 用户单击按钮以启用特定形状图形,即线, 将鼠标移动到JPanel,使光标变为 十字线 用户单击两次,绘制两点小圆圈和 将在两点之间绘制选定形状 到目前为止,我的想法是: import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JPanel; import jav

我需要做一个简单的绘图应用程序,能够画一条线,一个矩形和2个用户给定点之间的圆

确切的应用程序行为应如下所示:

用户单击按钮以启用特定形状图形,即线, 将鼠标移动到JPanel,使光标变为 十字线 用户单击两次,绘制两点小圆圈和 将在两点之间绘制选定形状 到目前为止,我的想法是:

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.LineBorder;

import javax.swing.AbstractAction;
import javax.swing.Action;

public class MainFrame
{
    private boolean readyToDraw = false;
    private int clickCount = 0;
    private JFrame frame;
    private JPanel drawPanel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame window = new MainFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MainFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 800, 600);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
        buttonPanel.setBounds(10, 11, 100, 85);
        frame.getContentPane().add(buttonPanel);
        buttonPanel.setLayout(null);

        JButton btnLine = new JButton("Line");
        btnLine.setBackground(Color.LIGHT_GRAY);
        btnLine.setBounds(4, 4, 92, 25);

        btnLine.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // Execute when button is pressed
                if (readyToDraw == true) {
                    System.out.println("Let's draw!");
                }

                else {

                }
                System.out.println("Line");

            }
        });

        JButton btnRectangle = new JButton("Rectangle");
        btnRectangle.setBackground(Color.LIGHT_GRAY);
        btnRectangle.setBounds(4, 30, 92, 25);

        btnRectangle.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // Execute when button is pressed
                if (readyToDraw == true) {
                    System.out.println("Let's draw!");
                }

                else {

                }
                System.out.println("Rectangle");
            }
        });

        JButton btnCircle = new JButton("Circle");
        btnCircle.setBackground(Color.LIGHT_GRAY);
        btnCircle.setBounds(4, 56, 92, 25);

        btnCircle.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // Execute when button is pressed
                if (readyToDraw == true) {
                    System.out.println("Let's draw!");
                }

                else {

                }
                System.out.println("Circle");
            }
        });

        buttonPanel.add(btnLine);
        buttonPanel.add(btnRectangle);
        buttonPanel.add(btnCircle);
    }
@SuppressWarnings("serial")
private class Paint extends JPanel implements MouseListener{
    drawPanel = new JPanel();
    drawPanel.setBackground(Color.WHITE);
    drawPanel.setBounds(120, 11, 664, 550);
    frame.getContentPane().add(drawPanel);

    this.addMouseListener(new MouseListener());

    @Override
    public void mouseEntered(MouseEvent arg0) {
        Cursor dotCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
        drawPanel.setCursor(dotCursor);
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mouseClicked(MouseEvent e) {
        int x1 = 0;
        int y1 = 0;
        int x2 = 0;
        int y2 = 0;
        if (clickCount == 0) {
            x1 = e.getX();
            y1 = e.getY();
            clickCount++;
        } else if (clickCount == 1) {
            x2 = e.getX();
            y2 = e.getY();
            clickCount++;
            readyToDraw = true;
        } else {
            clickCount = 0;
            readyToDraw = false;
        }

        System.out.println(x1 + " " + y1 + " " + clickCount + " " + x2 + " "
                + y2 + readyToDraw);
    }

    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }


 }
}  
这包含一些我无法解决的错误。我需要的是一个如何使我的申请能够至少画一条线的解释

在Paint类中,您似乎试图在方法或构造函数的上下文之外执行功能

private class Paint extends JPanel implements MouseListener {

    // Undefined variable...
    drawPanel  = new JPanel();

    // Executing functionality outside of a method or constructor
    drawPanel.setBackground (Color.WHITE);

    drawPanel.setBounds (

    120, 11, 664, 550);
    frame.getContentPane ()
    .add(drawPanel);



    this.addMouseListener(

    new MouseListener());
所以,你可以做一些像

private class Paint extends JPanel implements MouseListener {

    private JPanel drawPanel = new JPanel();

    public Paint() {
        drawPanel.setBackground(Color.WHITE);

        drawPanel.setBounds(
                        120, 11, 664, 550);
        frame.getContentPane()
                        .add(drawPanel);

        this.addMouseListener(
                        new MouseListener());

    }
但这最终会导致this.addMouseStener调用出错,因为MouseStener是一个接口,需要先实现才能使用,但是你猜怎么着,你可以这样做

这会处理编译器错误,但是会有很多逻辑错误

您似乎在JPanel中的JPanel类drawPanel中创建了一个JPanel实例,然后将这个子面板drawPanel直接添加到框架中!?这看起来完全是浪费时间,一般来说,这是一个糟糕的设计

但是,您可以将鼠标侦听器添加到“绘制”面板,然后尝试使用它更新drawPanel

简单地说,您不需要drawPanel,只需去掉它并直接使用Paint panel,但不要从Paint的构造函数中将其添加到框架中,Paint不应该在意


避免使用空布局,像素完美布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计宗旨是与布局管理器一起工作,放弃这些管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题,真的吗?这包含一些错误,但你不打算告诉我们它们是什么?如果我知道它们是什么,我会告诉你。Eclipse控制台的输出不仅告诉我们:线程main java.lang中的异常。错误:未解决的编译问题:在MainFrame.mainMainFrame.java:22我想在将一个类插入到主类时会出现语法问题。我意识到最后的一些文本不知怎的消失了,因此变得不清楚。我对我的问题进行了一些编辑。我使用setLayoutnull仅将应用程序窗口居中。至于像素完美的布局,因为这是我的第一个GUI应用程序,我试图简化事情,以避免所有可能的情况对大脑造成损害。无论如何,感谢您的回答,这应该可以解决现有的错误。您可以使用GridBagLayout将组件集中在窗口中心,在框架上使用pack和setLocationRelativeTonull将窗口集中在屏幕上,但仅在与布局管理器一起使用时才有用。