Java绘图应用程序

标签 java swing graphics2d

我需要开发一个应用程序,它有 3 个按钮,用于绘制直线、矩形和圆形。应用程序的行为应如下所示:用户单击按钮来绘制想要的形状,鼠标光标变为点,用户将鼠标向下移动到某个容器,通过在所需位置单击鼠标两次来绘制两个点,然后绘制想要的形状是用这两个点绘制的。根据我已经收集的信息,我知道我应该使用 MouseClickListener 来绘制点,然后使用从点类传递的参数调用形状类来绘制形状。我需要知道的是用于形状的容器、将 MouseClickListener 放置在哪里以便仅允许在该容器中绘制以及如何限制用户在按钮被按下之前绘制更多点。再次按下。 到目前为止,这是我的代码: `公共(public)类 MyPanel {

private JFrame frame;

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

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

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

    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);
    panel.setBounds(10, 25, 474, 336);
    frame.getContentPane().add(panel);

    JButton btnLine = new JButton("Line");
    btnLine.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            drawPoint draw = new drawPoint();
        }
    });
    btnLine.setBounds(10, 0, 110, 23);
    frame.getContentPane().add(btnLine);

    JButton btnRectangle = new JButton("Rectangle");
    btnRectangle.setBounds(196, 0, 110, 23);
    frame.getContentPane().add(btnRectangle);

    JButton btnCircle = new JButton("Circle");
    btnCircle.setBounds(374, 0, 110, 23);
    frame.getContentPane().add(btnCircle);
 }
}
public class drawPoint implements MouseListener {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    getCoordinates
    drawAPoint
    drawLine(coordinates)
}

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

 }

}

最佳答案

What I need to know is what container to use for the shapes

通常,具有自定义绘制功能的组件是通过子类化 JPanel 并重写 paintComponent 方法来完成的。从 OO 的角度来看,不太习惯,而且可以说更干净,可以子类化 JComponent。但通过此途径,您会发现网络上的示例代码要少得多。

where to put the MouseClickListener

JPanel 子类上可能会起作用。

in order to allow drawing only in that container

您无法真正阻止用户在 JPanel 中单击并将其拖动到外部。但是,您可以尝试检测这种情况,并确保代码忽略这种输入。

and how to restrict the user from drawing any more points until a button is pressed again.

使用变量。例如, boolean 变量 ready 最初为 true,在绘图开始时设置为 false 并重置为 true > 只需按下按钮即可。并让您的绘图点处理程序检查 ready 值,并且仅在其为 true 时启动绘图。您可能需要其他变量来跟踪当前绘图操作中允许的额外点击次数。

关于Java绘图应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29385057/

相关文章:

java - 在 map 中使用列表 (Java)

java - 保护 Java 应用程序内的数据库信息

java - 如何以编程方式触发 ComponentListener?

java - 运行应用程序时框架出现空白(JAVA)

java - 当字符串包含 "*/"时,注释/* */不起作用

java - Android Studio 中的 OpenCL

java - Swing 布局问题

java - 通过 2 个点画一条无限长的线?

java - 如何以更高的像素密度显示我的 Java (Swing GUI) 应用程序?

java - 绘制圆角矩形