使用Swing,我想绘制几点

时间:2009-10-04 12:23:41

标签: java swing drawing 2d

...在图像中并对其[x y]坐标进行一些计算。

我的第一个想法是使用图像作为JPanel的背景,然后注册点,但我不确定是否有办法在JPanel上标记这些。还有我不熟悉的绘图库,但我不确定我是否可以将它们与Swing结合起来。

你能说出我可以使用的包/类来完成任务吗?已经提到的代码的参考也是受欢迎的。

谢谢!

4 个答案:

答案 0 :(得分:7)

这里的问题有三个方面:

  1. 需要一种显示背景图像的方法。
  2. 必须能够找到点击鼠标的位置。
  3. 必须有一种方法可以在面板上绘制点。
  4. 实现上述要点的一种方法是继承JPanel并提供这些功能。

    <强> 1。在面板中显示背景图像。

    首先,由于JPanel默认无法显示背景图像,因此必须有一种方法可以在JPanel中保存图像,然后在面板上绘制它,可以通过paintComponent方法执行。

    实现此目的的一种方法是在类中放置一个字段,用于绘制Image

    class MyPanel extends JPanel {
        // Background image. Initialize appropriately.
        Image backgroundImage;
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            // Draw background image each time the panel is repainted.
            g.drawImage(backgroundImage, 0, 0, null);
        }
    }
    

    paintComponent中的Graphics对象与MyPanel相关联,可用于执行图形操作。

    <强> 2。找到点击鼠标的点。

    其次,为了检索单击鼠标的点,可以为MyPanel分配MouseListener。在以下示例中,扩展MouseAdapter的匿名内部类用于最小化编写额外代码:

    class MyPanel extends JPanel {
        // Background image. Initialize appropriately.
        Image backgroundImage;
    
        public MyPanel() {
             // Add a MouseListener which processes mouse clicks.
             this.addMouseListener(new MouseAdapter() {
                 public void mouseClicked(MouseEvent e) {
                     // Process mouse-click.
                 }
             })
        }
    
        // paintComponents method here.
    }
    

    单击鼠标时需要执行的处理可以包含在mouseClicked方法中。

    第3。如何在面板上画一个点。

    第三,为了找到点击鼠标的一个点,可以从MouseEvent方法传入的mouseClicked对象中获取它:

    class MyPanel extends JPanel {
        // Background image. Initialize appropriately.
        Image backgroundImage;
        Point pointClicked;
    
        public MyPanel() {
             // Add a MouseListener which processes mouse clicks.
             this.addMouseListener(new MouseAdapter() {
                 public void mouseClicked(MouseEvent e) {
                     // Retrieve the point at which the mouse was clicked.
                     pointClicked = e.getPoint();
    
                     // Repaint the panel.
                     this.repaint();
                 }
             })
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            // Draw background image each time the panel is repainted.
            g.drawImage(backgroundImage, 0, 0, null);
    
            // Draw a little square at where the mouse was clicked.
            g.fillRect(pointClicked.x, pointClicked.y, 1, 1);
        }
    }
    

    虽然上面的代码没有经过测试,但它应该是一个起点。

    例如,如果需要绘制多个点,可能需要List<Point>来保持点,并且可以在paintComponents方法中绘制每个Point

    如果在单击鼠标时需要执行其他处理,则可以向mouseClicked方法添加其他代码。

    其他资源:

    感谢zedoo在评论中指出,在覆盖super.paintComponent方法时应该执行对paintComponent的调用。

答案 1 :(得分:1)

对JPanel进行子类化并覆盖方法paintComponent:

public void paintComponent(Graphics g) {
    super.paintComponent(g);

}

在该方法中,您可以使用传递给它的Graphics对象的方法。每次需要重新绘制面板时都会调用此方法,因此您需要将点存储在数组中,然后在paintComponent中读取和绘制每个点。

答案 2 :(得分:1)

你可能也觉得这很有用,如果你想要一个重量级组件(AWT),this教程解释了如何扩展Canvas类来绘制东西。

答案 3 :(得分:1)

Background Panel提供了一些关于如何根据您的要求显示背景图片的建议。

Custom Painting Approaches然后,如果您需要为图片添加自定义点,则会提供有关如何进行自定义绘画的一些想法。