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
I hope you understand my diagram, but the point is: the "game" folder has 3 folders inside it, "input","player","scenario".
Inside "InputHandler.java", I have declared package game.input
.
Inside "Player.java", I have declared package game.player
.
And inside "Block.java", I have declared package game.scenario
.
So far, so good.
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".
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.
Thanks.
Edit:
Code
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);
}
}
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);
}
}
Commands:
compiling Player, Block and InputHandler(javac file.java) then run the game with _java ShooterGame