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

标签 java string constructor int undefined

我在玩 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/

相关文章:

java - Android 字符串替换全部不起作用

Javascript类成员函数通过setInterval调用成员函数,无法访问成员变量

Javafx 将 ActionListener 添加到按钮

c++ - 修剪空格 C++ POINTERS

java - Spring MVC 默认值不起作用

java - 将 Scanner next 字符串转换为 Int 并检查输入的文本是否为数字

java - 字符串作为新类的唯一私有(private)字段

constructor - 为什么有 "key"是一些构造函数的参数,它有什么用?

java - Birt 中的日期参数验证

java - 使用 AES 算法进行图像加密