我的朋友为我制作了一个简单的 JFrame,我打算对其进行开发和开发。我在使用它的前 30 分钟内遇到了一个问题:它不是在绘制图形!!
下面是绘制图形的代码,以及来自类的代码,这些代码调出 JFrame。
提前致谢。
GameCanvas.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.LinkedList;
/**
*
* @author Kris
*/
public class GameCanvas extends Canvas{
Player p;
LinkedList<Stalker> s = new LinkedList<>();
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
p.paintItAll(g2d);
for(int i = 0; i < s.size(); i++) {
s.get(i).paintItAll(g2d);
}
}
public GameCanvas() {
initComponents();
repaint();
}
public void initComponents() {
p = new Player(new Point(50,50));
s.add(new Stalker(new Point(50,100)));
}
public Point getPlayerPos() {
return p.getPos();
}
}
MainWindow.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @author Kris
*/
public class MainWindow extends JFrame{
static Toolkit tk = Toolkit.getDefaultToolkit();
static int xSize = ((int) tk.getScreenSize().getWidth());
static int ySize = ((int) tk.getScreenSize().getHeight());
/**
* @param args the command line arguments
*/
public MainWindow() {
super("STALKER!!!!!!!!!!");
GameCanvas canvas = new GameCanvas();
add(canvas, BorderLayout.CENTER);
setSize(xSize, ySize);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
canvas.createBufferStrategy(2);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow();
}
});
}
}