使用java包时出错

时间:2015-01-06 22:41:14

标签: java import package

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

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

我希望你理解我的图表,但重点是:"游戏"文件夹里面有3个文件夹,"输入","播放器","场景"。

Inside" InputHandler.java",我已声明package game.input

Inside" Player.java",我已声明package game.player

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

到目前为止,非常好。

但是,当在Player.java中,我做import game.input.InputHandler时,它表示"包game.input不存在",即使我已经宣布" game.input& #34;

我在这里做错了什么?如果您需要文件中的代码,请发表评论。我不是在这里发布它们,因为我认为我遇到的主要问题是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 文件 .java) 然后使用 _java ShooterGame

运行游戏

1 个答案:

答案 0 :(得分:0)

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

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