public class Main extends JPanel implements Runnable {
private static final int WIDTH = 1000;
private static final int HEIGHT = 800;
TerranGenartor gen = new TerranGenartor();
public Main() {
setSize(WIDTH, HEIGHT);
gen.run();
}
public void paintComponents(Graphics g) {
repaint();
}
public void run() {
}
}
我确实收到了一个错误
TerranGenartor gen = new TerranGenartor();
我的TerranGenartor
班级
public class TerranGenartor implements Runnable {
Main main = new Main();
Graphics g;
Random r = new Random();
int SMIN = 1;
int SMAX = 3;
int i = 0;
int num;
int x = 0;
int y = 0;
public Color grass = new Color(124, 252, 0);
public Color stone = new Color(190, 190, 190);
public Color dirt = new Color(139, 69, 19);
boolean Runn = false;
public void start() {
Runn = true;
System.out.println("Working...");
}
public TerranGenartor() {
Runn = true;
}
protected void paintComponent(Graphics g) {
g = main.getGraphics();
while (Runn) {
if (i <= 1000) {
num = r.nextInt(SMAX - SMIN + 1) + SMIN;
switch (num) {
case 1:
g.setColor(grass);
break;
case 2:
g.setColor(stone);
break;
case 3:
g.setColor(dirt);
break;
default:
g.setColor(grass);
break;
}
g.fillRect(x, y, 50, 50);
x += 50;
System.out.println(i);
System.out.println("X: " + x);
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void run() {
while (Runn) {
paintComponent(g);
}
}
}
该类是不会在主类(JPanel
扩展)中绘制的类。我尝试了一些东西,但似乎没有任何效果。
我的The_Runner
班级
public class The_Runner {
Main main = new Main();
JFrame frame = new JFrame();
private static final int WIDTH = 1000;
private static final int HEIGHT = 800;
String TITLE = "Map gen.";
public The_Runner() {
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setTitle(TITLE);
frame.add(main);
}
public static void main(String[] args) {
The_Runner runner = new The_Runner();
}
}
这个类只是遵守并运行它,我认为这一切都很好。
这样做的问题是,当我想从TerranGenerator
课堂上绘画时,它不起作用。上没有显示任何内容JPanel
。