所以我需要我在StaticGUI中创建的图形变量用于更新......获取/设置方法已经出来,它在两个不同的线程上运行,我一无所知......
编辑:另外,有没有比 Canvas 更好的方法呢?
主类:
package sanity;
public class Client {
int X = 1024;
int Y = 680;
StaticGUI gui;
public void launch() {
gui = new StaticGUI();
gui.draw();
}
public static void main(String[] args) {
Client game = new Client();
game.launch();
Thread t = new Thread(new Update());
t.start();
}
}
带有图形变量的类:
package sanity;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
class StaticGUI extends Client {
JFrame mainWin;
Canvas UI;
BufferStrategy Buffer;
Graphics g;
Graphics2D g2d;
public StaticGUI() {
mainWin = new JFrame("Game");
UI = new Canvas();
}
void draw() {
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.setIgnoreRepaint(true);
UI.setIgnoreRepaint(true);
UI.setBounds(0, 0, X, Y);
UI.setBackground(Color.WHITE);
mainWin.add(UI);
mainWin.pack();
mainWin.setResizable(false);
mainWin.setLocationRelativeTo(null);
mainWin.setVisible(true);
UI.createBufferStrategy(2);
Buffer = UI.getBufferStrategy();
g = Buffer.getDrawGraphics();
g2d = (Graphics2D) g;
}
}
我需要它们的类:
package sanity;
import java.awt.*;
public class Update extends StaticGUI implements Runnable {
public Update() {
}
@Override
public void run() {
System.out.println(X + "," + Y);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, X, Y);
g.dispose();
Buffer.show();
}
}