【问题标题】:Calling paintComponent() with parameters using repaint()使用 repaint() 调用带有参数的 paintComponent()
【发布时间】:2014-05-17 06:56:24
【问题描述】:

我对 Java 图形非常陌生,所以请询问是否需要任何其他信息 :)

我正在尝试根据鼠标在屏幕上的点击位置绘制形状。因此,我需要将单击位置的 x 和 y 坐标传递给 paintComponent() 方法,以便它知道在哪里绘制形状。

public void mouseClicked(MouseEvent e) {
    System.out.println("Adding Shape");
    repaint();
}

class CanvasDrawArea extends JPanel{
    //this should run when the program first starts
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        canvas.setBackground(CANVAS_COLOR);
    }

    //here is where the question is
    public void paintComponent(Graphics g, int x, int y){
        super.paintComponent(g);
        g.fillRect(x, y, RECTANGLE_WIDTH, RECTANGLE_HEIGHT);
    }
}

基本上,我试图通过调用repaint() / pack() 方法让一个在程序启动时正确运行的paintComponent 以及一个在我给它x 和y 坐标时将运行的paintComponent 重载。但是,我不确定应该如何传递 x 和 y 参数,因为无法在 repaint() 方法中传递它们。

【问题讨论】:

    标签: java graphics jframe


    【解决方案1】:
    1. 您永远不需要直接调用paintComponentpaint,它们会在需要时由重绘管理器自动调用...
    2. 创建java.util.List并将每次鼠标点击的Point存储在其中,完成此操作后从mouseClicked方法调用repaint
    3. 在您的paintComponent(Graphics) 方法中,迭代List 点并绘制您需要的形状。

    举个简单的例子……

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Dotty {
    
        public static void main(String[] args) {
            new Dotty();
        }
    
        public Dotty() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new DottyPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class DottyPane extends JPanel {
    
            private List<Point> points;
    
            public DottyPane() {
                points = new ArrayList<>(25);
                addMouseListener(new MouseAdapter() {
    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        points.add(e.getPoint());
                        repaint();
                    }
    
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(Color.RED);
                for (Point p : points) {
                    g2d.fillOval(p.x - 5, p.y - 5, 10, 10);
                }
                g2d.dispose();
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-26
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多