如何延迟 Java 代码直到另一部分代码完成?

所以我尝试了 java.awt.Graphics 库并在我的代码中遇到了一个问题。下面的代码返回空指针异常,因为在执行时未定义 Graphics 对象“g”。我可以循环调用并检查 g != null 但是否有更好的方法来执行此操作?


谢谢你的帮助。


这是我的代码:


package gui;


import java.awt.Color;

import java.awt.Graphics;


import javax.swing.JFrame;

import javax.swing.JPanel;


@SuppressWarnings("serial")

public class Gui extends JPanel{


Graphics g;


public Gui() 

{

    JFrame frame = new JFrame("test");

    frame.setSize(700, 700);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

    frame.add(this);

}


public void paintComponent(Graphics g) 

{

    super.paintComponent(g);

    this.g = g;


    g.setColor(Color.blue);


    for(int x = 0;x < 700; x += 20) 

    {

        g.drawLine(x, 0, x, 700);

    }


    for(int y = 0;y < 700; y += 20) 

    {

        g.drawLine(0, y, 700, y);

    }

}


public void draw(Tuple xy) 

{

    g.setColor(Color.blue);   // <--- null pointer exception

    g.fillOval(xy.x, xy.y, 5, 5);

}


}

和主要课程:


package gui;


public class Main {


public static void main(String[] args)

{


    new Gui().draw(new Tuple(200,200)); //Tuple is a custom class I wrote


}

}


繁华开满天机
浏览 133回答 3
3回答

qq_花开花谢_0

找到了我的空问题的解决方案。我修改了方法“draw()”来接受一个元组,然后将它传递给paintComponent()函数并调用repaint()。package gui;import java.awt.Color;import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JPanel;@SuppressWarnings("serial")public class Gui extends JPanel{&nbsp; &nbsp; Graphics g;&nbsp; &nbsp; Tuple xy;&nbsp; &nbsp; public Gui()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("test");&nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(700, 700);&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.add(this);&nbsp; &nbsp; }&nbsp; &nbsp; public void paintComponent(Graphics g)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; this.g = g;&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.blue);&nbsp; &nbsp; &nbsp; &nbsp; for(int x = 0;x < 700; x += 20)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawLine(x, 0, x, 700);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(int y = 0;y < 700; y += 20)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawLine(0, y, 700, y);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(xy != null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.fillOval(xy.x, xy.y, 5, 5);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void draw(Tuple xy)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.xy = xy;&nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; }}谢谢你的帮助

当年话下

你应该把所有的画都画在public&nbsp;void&nbsp;paintComponent(Graphics&nbsp;g)方法如https://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html所述。

慕婉清6462132

使用回调函数。比polling物业好很多。你得到一个新的图形对象。并且您的调用是在适当的上下文中执行的。这是一般的想法:实例化new Gui((Graphics graphics)-> {&nbsp; &nbsp;// your code here});CTOR 或初始化函数public Gui(GraphicsCallback callback)&nbsp;调用调用回调。您可以检查是否被调用,如果适合您的用例,则仅回调一次。或者回调实现可以管理多次调用。public void paintComponent(Graphics g)&nbsp;{&nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; this.g = g;&nbsp; &nbsp; this.callback(g)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java