简体   繁体   English

“构造函数Window(int,int,String,Game)未定义”

[英]“ 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.

相关问题 构造函数AdPreferences(int,int,String)是未定义的startapp吗? - The constructor AdPreferences(int, int, String) is undefined startapp? 构造函数Vehicle(String [],int)未定义 - The constructor Vehicle(String[], int) is undefined 错误“构造函数 HighScore(int,int,String) 未定义”( game = new HighScore((id, score, user); ) - Error "the constructor HighScore(int,int,String)is undefined" ( game = new HighScore((id, score, user); ) 为什么我的编译器显示“没有为 Window(int,int,String,Game) 找到合适的构造函数”错误? - Why does my compiler show a "no suitable constructor found for Window(int,int,String,Game)" error? 构造函数ClassCountry(String,String,String,int)未定义 - The constructor ClassCountry(String, String, String, int) is undefined 错误构造函数 Family(String, String, int) 未定义 - Error The constructor Family(String, String, int) is undefined spring boot 中未定义构造函数 String, String, int, int, Optional&lt;&gt; - The constructor String, String, int, int, Optional<> is undefined in spring boot 构造函数Item(int,String,double)是未定义的 - the constructor Item(int ,String ,double) is undefined 构造函数 Candidate(String, int[]) 是未定义的错误 - The constructor Candidate(String, int[]) is undefined error 构造函数 (int, int, string) 未定义; 在 LWJGL 中创建显示 - the constructor (int, int, string) is undefined; creating display in LWJGL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM