[英]“ The constructor Window(int, int, String, Game) is undefined ”
I am having issues with a 2D platform game. 我在2D平台游戏中遇到问题。 I get an error that reads what the title of the question said. 我收到一个错误,上面写着该问题的标题。 Here are my Game.java and Window.java files. 这是我的Game.java和Window.java文件。 Please tell me what I should do. 请告诉我该怎么办。 I've tried a ton of things and I just don't know where to go or what to do. 我尝试了很多事情,但我不知道该去哪里或做什么。 Thanks in advance :) 提前致谢 :)
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();
}
}
Game.java Game.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"
}
}
Your Window
class is fine, but the class you're importing in Game
is java.awt.Window
. 您的Window
类很好,但是您要在Game
导入的类是java.awt.Window
。
You could solve this by doing new com.sam.platform.window.Window(...)
, but I would advise against it, it will just confuse you. 您可以通过执行新的com.sam.platform.window.Window(...)
解决此问题,但我建议您这样做, com.sam.platform.window.Window(...)
会使您感到困惑。
Rename the class to something like GameWindow
instead. 将类重命名为类似GameWindow
。
You have two different classes called Window
in use in your Game
class. 您在Game
类中使用了两个不同的类,分别称为Window
。 One is com.sam.platform.window.Window
. 一种是com.sam.platform.window.Window
。 The other is java.awt.Window
. 另一个是java.awt.Window
。 Since you have imported java.awt.Window
into your Game
class, it thinks you are trying to instantiate one of those (not your own Window
class). 由于您已将java.awt.Window
导入到Game
类中,因此它认为您正在尝试实例化其中的一个(不是您自己的Window
类)。
I suggest renaming you own class to disambiguate (and avoid confusion) to, say, GameWindow
. 我建议重命名自己的类,以消除(并避免造成混淆)例如GameWindow
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.