我在按照教程创建游戏时遇到错误。它显示 Graphics2D 无法解析为类型。我是 java 的新手,我正在使用 Eclipse IDE (JavaSE-12)。我导入了 import java.awt.Graphics2D,它似乎消除了错误,但只要我运行程序,错误就会再次出现。其他一切正常,让我导入其他东西(例如图形、维度和 JPanel)。我会很感激你的回答!
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable{
//screen settings
final int originalTileSize = 16;
final int scale = 3; // 48x48
final int tileSize = originalTileSize * scale;
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol;
final int screenHeight = tileSize * maxScreenRow;
Thread gameThread;
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.pink);
this.setDoubleBuffered(true);
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
while(gameThread != null) {
update();
repaint();
}
}
public void update() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g; <--- this line shows an error
// (I'm trying to create a rectangle after I resolve the issue with Graphics)
//g2.setColor(Color.white);
//g2.fillRect(100, 100, tileSize, tileSize);
//g2.dispose();
}
}