gpt4 book ai didi

java - "The constructor Window(int, int, String, Game) is undefined "

转载 作者:行者123 更新时间:2023-11-29 03:24:14 26 4
gpt4 key购买 nike

我在玩 2D 平台游戏时遇到问题。我收到一个错误,上面写着问题的标题。这是我的 Game.java 和 Window.java 文件。请告诉我应该怎么做。我已经尝试了很多东西,但我只是不知道该去哪里或该做什么。提前致谢:)

Window.java

package com.sam.platform.window;    
import java.awt.Dimension;
import javax.swing.JFrame;

public class Window
{
public Window(int w, int h, String title, Game game)
{
game.setPreferredSize(new Dimension(w, h));
game.setMaximumSize(new Dimension(w, h));
game.setMinimumSize(new Dimension(w, h));

JFrame frame = new JFrame(title);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

game.start();
}

}

游戏.java

package com.sam.platform.window;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Window;
import java.awt.image.BufferStrategy;

import com.sam.platform.framework.ObjectId;

public class Game extends Canvas implements Runnable
{
private static final long serialVersionUID = -414187095722102896L;
private boolean running = false;
private Thread thread;

public static int WIDTH, HEIGHT;
//Object
Handler handler;


private void init()
{
WIDTH = getWidth();
HEIGHT = getHeight();


handler = new Handler();

handler.addObject(new Player(100, 100, handler, ObjectId.Player));

handler.createLevel();

this.addKeyListener(new KeyInput(handler));
}


public synchronized void start(){
if(running)
return;

running = true;
thread = new Thread(this);
thread.start();

}

public void run()
{
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;

if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS:" + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}

private void tick()
{
handler.tick();
}

private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
this.createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();
//////////////////////////////////
//Draw Here
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());

handler.render(g);

//////////////////////////////////
g.dispose();
bs.show();

}
public static void main(String args[]){
new Window(900, 900, "Hop", new Game()); //error is here "The constructor Window(int, int, String, Game) is undefined"

}


}

最佳答案

您的 Window 类没问题,但您在 Game 中导入的类是 java.awt.Window

你可以通过执行 new com.sam.platform.window.Window(...) 来解决这个问题,但我建议不要这样做,它只会让你感到困惑。

将类重命名为类似 GameWindow 的名称。

关于java - "The constructor Window(int, int, String, Game) is undefined ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21912874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com