編輯:這個錯誤只發生在我在我的Ubuntu 14.04筆記本電腦上運行我的比賽。當我在Windows學校計算機上運行它時,它工作正常。正常運行時,當過我運行它,它落後於像瘋了似的的Java窗口落後於Ubuntu的但不是窗口時代碼不落後
窗口滯後,但滯後停止,而我調整窗口的大小與我的鼠標
我試圖做一個簡單的遊戲在Java中,但是,我查看了代碼,並使用System.out.println和System.currentTimeMillis進行了一些檢查,代碼運行時間遠遠少於1毫秒,所以這不是問題。
程序停止的時候我拖到調整窗口的大小,不斷改變窗口(大概強制屏幕重繪)的大小,它有很好的流暢的動畫效果當我這樣做。
當我的程序運行時,它以平滑的動畫開始,然後大約1秒鐘後它變爲大約2 FPS,然後另一秒鐘後變爲1 FPS,然後大約0.5FPS並停留在那裏直到我強制通過調整窗口
我的代碼重繪是:
public class WindowSetup extends JPanel{
private static final long serialVersionUID = 6486212988269469205L;
static JFrame frame;
static JPanel panel;
protected void paintComponent(Graphics g){
super.paintComponent(g);
GameClass.draw(g);
}
public static void main(String[] args) {
frame = new JFrame("Evolver");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
panel = new WindowSetup();
frame.add(panel);
panel.setVisible(true);
frame.setVisible(true);
GameClass.init();
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
GameClass.tick();
panel.repaint();
}
};
Timer timer = new Timer(50,al); // lower numbers here make it run faster, but it still ends up slowing down to 1FPS
timer.start();
}
}
我GameClass:
public class GameClass {
static Random random;
static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
static Wall wall;
static void init(){
random = new Random();
wall=new Wall();
for(int i=0;i<5;i++){
enemies.add(new Enemy(random.nextInt(200)+100,random.nextInt(200)+100,10,10,(float) (random.nextFloat()*(Math.PI*2)),1));
}
}
static void tick(){
for(Enemy d:enemies){
d.tick();
}
}
static void draw(Graphics g){
for(Enemy d:enemies){
d.draw(g);
}
wall.draw(g);
}
}
敵對階級:
public class Enemy {
float x;
float y;
float width;
float height;
float direction;
float speed;
public Enemy(float x, float y, float width, float height, float direction, float speed) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.direction = direction;
this.speed = speed;
}
public void tick() {
direction += 0.01;
if (!(GameClass.wall.collides((int) (x + (Math.cos(direction) * speed)), (int) (y + (Math.sin(direction) * speed)),(int)width,(int)height))) {
x += Math.cos(direction) * speed;
y += Math.sin(direction) * speed;
}
}
public void draw(Graphics g) {
g.drawRect((int) x, (int) y, (int) width, (int) height);
}
}
牆代碼:
public class Wall {
ArrayList<Rectangle> rectlist = new ArrayList<Rectangle>();
public Wall() {
// create list of walls here
rectlist.add(new Rectangle(10, 10, 50, 400));
rectlist.add(new Rectangle(410,10,50,400));
rectlist.add(new Rectangle(60,10,350,50));
rectlist.add(new Rectangle(60,360,350,50));
}
public void draw(Graphics g) {
for (Rectangle d : rectlist) {
g.drawRect(d.x, d.y, d.width, d.height);
}
}
public boolean collides(int x,int y,int width,int height){
for(Rectangle d:rectlist){
if(d.intersects(x, y, width, height)){
return true;
}
}
return false;
}
}
我們可以看看你的'GameClass'的內容嗎?如果它隨着時間的推移而下降,那麼你很可能沒有清理你應該在的地方。 –
這真的是基本的移動代碼,我添加了我的GameClass和Enemy。 GameClass.wall.collides()只是一個基本的矩形碰撞檢查器,它絕對不會產生滯後。 –
調整框架的大小會在您拖動時調用'repaint()',並添加到由'Timer'排隊的那些框架中。 – trashgod