Hello,
I havr created a program in which there are two classes. One ship, and shooter. However it is not working.
Any idea?
package shooter;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Ship implements Runnable {
int x, y, xDirection;
boolean shoot = false;
boolean ready = false;
int bullX;
int bullY;
Rectangle bullet;
public Ship(){
x = 175;
y = 275;
}
public void draw(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(x, y, 40, 10);
g.fillRect(x+18, y-7, 4, 7);
if (shoot)
{
g.setColor(Color.BLACK);
g.fillRect(bullet.x, bullet.height, bullet.width, bullet.height);
}
}
public void move(){
x += xDirection;
if(x <= 0)
x = 0;
if(x >= 360)
x = 360;
}
public void setXDirection(int xdir){
xDirection = xdir;
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
setXDirection(-1);
}
if(keyCode == e.VK_RIGHT){
setXDirection(1);
}
if(keyCode == e.VK_SPACE){
if(bullet == null)
ready = true;
if (ready)
bullY = y-7;
bullX = x + 18;
bullet = new Rectangle(bullX, bullY, 3 , 5); // 3 and 5 width and height
shoot = true;
}
}
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
setXDirection(0);
}
if(keyCode == e.VK_RIGHT){
setXDirection(0);
}
if(keyCode == e.VK_SPACE){
ready = false;
if(bullet.y <=-5)
bullet = new Rectangle(0,0,0,0);
shoot = false;
}
}
public void shoot(){
if(shoot)
{
bullet.y--;
}
}
@Override
public void run(){
try{
while(true){
move();
Thread.sleep(5);
}
}catch(Exception e){System.err.println(e.getMessage());}
}
}
My second class
package shooter;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class Shooter extends JFrame {
private Image dbImage;
private Graphics dbg;
static Ship s1 = new Ship();
public Shooter(){
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
addKeyListener(new AL());
}
@Override
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
//Draw the ship
s1.draw(g);
s1.shoot();
repaint();
}
public class AL extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e){
s1.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e){
s1.keyReleased(e);
}
}
public static void main(String[] args) {
Shooter shoot = new Shooter();
//Threads
Thread ship = new Thread(s1);
ship.start();
}
}
Program is running but shooting is not working