如果我已经声明了 Graphics a,我似乎无法弄清楚为什么会出现此错误。我将如何去创建一个新的图形?(或者它是一个对象,我只是很愚蠢并试图将它声明为一个变量?)
仅供参考:我知道您可以使用 g Graphics 但我想创建一个本地 Graphics 变量(/或者它是一个对象,我只是愚蠢)。
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class World_Gen
{
public World_Gen( int a, int b, String Name)
{
JFrame aFrame = new JFrame (Name);
aFrame.setSize (a,b);
aFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
aFrame.setVisible(true);
}
}
import java.awt.Graphics.*;
import java.awt.Graphics;
public class launcher
{
public static void main (String [] args)
{
World_Gen Gen = new World_Gen (1000,2000,"My map");
draw Box = new draw (10,10);
Box.drawRect();
}
}
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Graphics.*;
import java.awt.Graphics;
public class draw extends JFrame
{
int XCords;
int YCords;
Graphics a; // Declared graphics a here.
public draw (int Xcord, int Ycord)
{
XCords = Xcord;
YCords = Ycord;
}
public void drawRect ()
{
a.drawRect (XCords, YCords, 10,10); // This is where the run-time error pops up
}
}