小编Moc*_*chi的帖子

repaint()没有调用PaintComponent来使用Graphics2D

我花了几天时间试图让Graphics2D类在我的代码中工作.我以这样的方式构造它,当注册click事件时,对repaint的调用完成,但是当它到达调用repaint()的阶段时,它只产生一个nullpointer异常.

它在调试时都按预期工作,而不是在paintComponent方法中调用,但是当尝试使用paintComponent和repaint()正确调用代码以允许Graphics2D类显示每个点的行时,它不起作用.

我已经包含了我的代码部分,我很难开始工作.任何帮助都将非常感激.先感谢您.

下面是包含我的mouseListener的GUI类.

public class GUI extends JPanel implements MouseListener {

private JLabel label;

    public BufferedImage getImg() {
    return img;
    }

    public void mouseClicked(java.awt.event.MouseEvent e) {
    // TODO Auto-generated method stub
    label = new JLabel();

    //set point equal to the location of the mouse click on the image label
    Point b = e.getPoint();

    //place we are going to print the dots
    segmentation.x = b.x; //gets the x coordinate 
    segmentation.y = b.y; //gets the y coordinate

    System.out.println("x = " + …
Run Code Online (Sandbox Code Playgroud)

java swing bufferedimage graphics2d paintcomponent

1
推荐指数
1
解决办法
2040
查看次数

标签 统计

bufferedimage ×1

graphics2d ×1

java ×1

paintcomponent ×1

swing ×1