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

时间:2014-02-20 15:52:40

标签: 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();
    }

}

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"

    }


}

2 个答案:

答案 0 :(得分:2)

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

你可以通过新的com.sam.platform.window.Window(...)来解决这个问题,但我会反对它,它会让你感到困惑。

将类重命名为类似GameWindow的内容。

答案 1 :(得分:2)

您的Window课程中有两个名为Game的不同类。一个是com.sam.platform.window.Window。另一个是java.awt.Window。由于您已将java.awt.Window导入Game类,因此它认为您正在尝试实例化其中一个(不是您自己的Window类。)

我建议将您自己的类重命名为歧义(并避免混淆),例如GameWindow