使用Java包时出错 [英] Error when using java packages
问题描述
我正在写一个游戏。它的目录树基本上是这样的:
I am writing a game. Its directory tree is basically like this:
ShootGame------>game----------------------->input--------------------------------->InputHandler.class
|-->ShooterGame.class | |-->InputHandler.java
|-->ShooterGame.java |---->player-------------->Player.class
| |-->Player.java
|---->scenario---
|-->Block.class
|-->Block.java
我希望你理解我的图表,但重点是:游戏文件夹里面有3个文件夹,输入,‘玩家’,‘场景’。
I hope you understand my diagram, but the point is: the "game" folder has 3 folders inside it, "input","player","scenario".
在InputHandler.java中,我声明了包game.input
。
Inside "InputHandler.java", I have declared package game.input
.
在Player.java中,我已声明包game.player
。
Inside "Player.java", I have declared package game.player
.
在Block.java中,我声明了包game.scenario
。
And inside "Block.java", I have declared package game.scenario
.
到目前为止,这么好。
但是,当在Player.java中,我执行导入game.input.InputHandler
时,它说包game.input不存在,即使我已经宣布game.input。
But when, in the Player.java, i do import game.input.InputHandler
, it says "package game.input does not exist", even though I have already declared "game.input".
我在这里做错了什么?如果您需要文件中的代码,请发表评论。我不是在这里发布它们,因为我认为我遇到的主要问题是包
和 import
逻辑。
What have I done wrong here? If you need the codes inside the files, please leave a comment. I am not posting them here because I think the main problem I have is the package
and import
logic.
谢谢。
编辑:
代码
Code
InputHandler.java
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
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
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:
The main class(The game start) 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
compiling Player, Block and InputHandler(javac file.java) then run the game with _java ShooterGame
推荐答案
您可能正在尝试从文件所在的目录中进行编译,这会错误地设置类路径。
You are probably trying to compile from inside the directory that the file is in, which is incorrectly setting the classpath.
cd game/player
javac Player.java
不要进入子包,而是显式设置类路径并从顶层编译。我假设 ShooterGame.java
在你的游戏
文件夹中:
Instead of going into the subpackages, set the classpath explicitly and compile from the top level. I'm assuming that ShooterGame.java
is in your game
folder:
cd path/to/project/ShootGame
javac -cp . game/player/Player.java
... compile other classes
java -cp . game.ShooterGame
这篇关于使用Java包时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!