使用 java 包时出错

Error when using java packages

我正在写游戏。它的目录树基本上是这样的:

ShootGame------>game----------------------->input--------------------------------->InputHandler.class
            |-->ShooterGame.class     |                                        |-->InputHandler.java
            |-->ShooterGame.java      |---->player-------------->Player.class                        
                                      |                      |-->Player.java
                                      |---->scenario---
                                                      |-->Block.class
                                                      |-->Block.java

我希望你能理解我的图表,但重点是:"game"文件夹里面有3个文件夹,"input"、"player"、"scenario"。

在"InputHandler.java"里面,我声明了package game.input

在"Player.java"里面,我声明了package game.player

在 "Block.java" 中,我声明了 package game.scenario

到目前为止,还不错。

但是,当我在 Player.java 中执行 import game.input.InputHandler 时,它显示 "package game.input does not exist",即使我已经声明了 "game.input"。

我做错了什么?如果您需要文件中的代码,请发表评论。我不会在这里发布它们,因为我认为我遇到的主要问题是 packageimport 逻辑。

谢谢。

编辑:

代码

InputHandler.java

package game.input;

import java.awt.Component;
import java.awt.event.*;

public class InputHandler implements KeyListener{

    static boolean[] keys = new boolean[256];

    public InputHandler(Component c){
        c.addKeyListener(this);
    }

    public boolean isKeyDown(int keyCode){
        if (keyCode > 0 && keyCode < 256){
            return keys[keyCode];
        }

        return false;
    }

    public void keyPressed(KeyEvent e){
        if (e.getKeyCode() > 0 && e.getKeyCode() < 256){
            keys[e.getKeyCode()] = true;
        }
    }

    public void keyReleased(KeyEvent e){
        if (e.getKeyCode() > 0 && e.getKeyCode() < 256){
            keys[e.getKeyCode()] = false;
        }
    }

    public void keyTyped(KeyEvent e){}
}

Player.java

package game.player;

import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import game.input.InputHandler;

public class Player{

    private BufferedImage sprite;
    public int x, y, width, height;
    private final double speed = 5.0d;

    public Player(int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

        try{
            URL url = this.getClass().getResource("ship.png");
            sprite = ImageIO.read(url);
        } catch(IOException e){}
    }

    public void keyPlayer(double delta, InputHandler i){
        if(i.isKeyDown(KeyEvent.VK_D)){
            if(this.x>=1240) return;
            else this.x+=speed*delta;
        }

        if(i.isKeyDown(KeyEvent.VK_A)){
            if(this.x<=0) return;
            else this.x-=speed*delta;
        }

    }

    public void update(InputHandler inputP){
        keyPlayer(1.7, inputP);
    }

    public void Draw(Graphics a){
        a.drawImage(sprite,x,y,width,height,null);
    }

    public Rectangle getBounds(){
        return new Rectangle(x,y,width,height);
    }
}

Block.java

package game.scenario;

import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;

public class Block{

    private Timer timer;
    private BufferedImage sprite;
    private final int t = 1;
    public int x, y, width, height;

    public Block(int x, int y, int width, int height){
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;

        try{
            URL url = this.getClass().getResource("meteor.png");
            sprite = ImageIO.read(url);
        } catch(IOException e){}
    }

    public void Draw(Graphics g){
        g.drawImage(sprite,x,y,width,height,null);
    }

    public boolean destroy(Block b){
        if(b.y>=630){
            b = null;
            timer.cancel();
            timer.purge();
            return true;
        } else { return false; }
    }

    public void update(int sec){
        //if(getBounds().intersects(ShooterGame.ShooterGameClass.player.getBounds())){ System.out.println("Collision detected!"); }
        timer = new Timer();
        timer.schedule(new Move(), sec*1000);
        destroy(this);
    }

    class Move extends TimerTask{
        public void run(){
            int keeper = 5;
            if(keeper>0){
                y+=5;
            }
        }
    }

    public Rectangle getBounds(){
        return new Rectangle(x,y,width,height);
    }

}

主课(游戏开始) ShooterGame.java:

import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.*;
import java.net.URL;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.event.MouseEvent;
import game.input.InputHandler;
import game.player.Player;
import game.scenario.Block;

public class ShooterGame extends JFrame{
    static int playerX=500;
    static int playerY=520;

    InputHandler input = new InputHandler(this);
    public static Player player = new Player(playerX,playerY,50,50);
    Block meteor = new Block(100,100,30,30);

    public static void main(String[] args){
        ShooterGame game = new ShooterGame();
        game.run();
        System.exit(0);
    }

    static int windowWidth = 1300;
    static int windowHeight = 600;
    static int fps = 30;
    static BufferedImage backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);

    public void run(){
        boolean running = true;

        initialize();

        while(running){
            long time = System.currentTimeMillis();

            update();
            draw();

            time = (1000 / fps) - (System.currentTimeMillis() - time);

            if (time > 0) {
                try{
                    Thread.sleep(time);
                }
                catch(Exception e){};
            };
        }
    }

    public void initialize(){
        setTitle("--- Shooter Game ---");
        setSize(windowWidth, windowHeight);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void update(){
        player.update(input);
        meteor.update(0);
    }

    public void draw(){

        Graphics g = getGraphics();

        Graphics bbg = backBuffer.getGraphics();

        bbg.setColor(Color.BLACK);
        bbg.fillRect(0, 0, windowWidth, windowHeight);
        player.Draw(bbg);
        meteor.Draw(bbg);

        g.drawImage(backBuffer, 0, 0, this);
    }
}

命令:

编译Player、Block和InputHandler(javac file.java) 然后使用 _java ShooterGame

运行游戏

您可能正在尝试从文件所在的目录内部进行编译,这错误地设置了类路径。

cd game/player
javac Player.java

与其进入子包,不如显式设置类路径并从顶层编译。我假设 ShooterGame.java 在您的 game 文件夹中:

cd path/to/project/ShootGame
javac -cp . game/player/Player.java
... compile other classes
java -cp . game.ShooterGame