0

I am in a beginners java course. We are in the early stage of graphics. I have been tasked with creating a program where a use a picture to shoot bullets. I have a spaceship image icon and my listeners.

My Bullet class is as follows

package Shooting;

int x, y, height;
int xShoot, yShoot;

Color bullet = new Color(0,255,0);



public Bullet(int x, int y)
{
    this.x = x;
    this.y = y;
}

public void setY(int y)
{
    this.y = y;
}
public int getY()
{
    return y;
}
public void setX(int x)
{
    this.x = x;
}
public int getX()
{
    return x;
}
public void draw(Graphics page)
{
    page.setColor(bullet);
    page.fillOval(xShoot, yShoot, 10, 10);
}
public void redraw(Graphics page)
{
    page.setColor(bullet);
    page.fillOval(xShoot, yShoot - 5, 10, 10);
}
public void setXShoot(int xShoot)
{
    this.xShoot = xShoot;
}
public void setYShoot(int yShoot)
{
    this.yShoot = yShoot;
}
public void remove(Graphics page)
{
    x = -40;
    y = 10;
    page.setColor(Color.GRAY);

}

}

My panel is as follows

package Shooting;

import javax.swing.*;   
import javax.swing.Timer;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class Shooter extends JPanel
{   

int shipX = 50;
int bulletX = shipX + 25;
int shipY = 600;
int y = 595;
int bulletY = 700 - 5;

JPanel panel = new JPanel();

final int MOVE = 10;

Bullet bullet = new Bullet(bulletX, bulletY);

ArrayList<Bullet> bullets = new ArrayList<>();

ImageIcon ship = new ImageIcon("klingon.png");; 

final int DELAY = 2;

private Timer timer;

///Graphics page;

public void paintComponent(Graphics page)
{   
    super.paintComponent(page);
    ///this.page = page;
    ship.paintIcon(this, page, shipX, shipY);
}       

public Shooter()
{   
    addKeyListener(new ShipListener());
    addKeyListener(new BulletListener());

    timer = new Timer(DELAY, new FiringListener());
    setBackground(Color.BLACK);
    timer.start();



}   

private class ShipListener implements KeyListener
{   
    public void keyPressed(KeyEvent event)
    {
        switch(event.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
                shipX = shipX - MOVE;
                repaint();
                break;
            case KeyEvent.VK_RIGHT:
                shipX = shipX + MOVE;
                repaint();
                break;
        }


        repaint();
    }
    public void keyTyped(KeyEvent event){}
    public void keyReleased(KeyEvent event){}
}

private class BulletListener implements KeyListener
{
    public void keyPressed(KeyEvent event)
    {
        switch(event.getKeyCode())
        {
        case KeyEvent.VK_SPACE:
        bullet.setX(shipX + 25);
        bullet.setY(shipY);
        bullet.draw(getGraphics());
        repaint();
        }
        repaint();
    }
    public void keyTyped(KeyEvent event){}
    public void keyReleased(KeyEvent event){}
}
private class FiringListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        bullet.setY(y - 5);
        repaint();
    }
}

}

And my driver

package Shooting;
import javax.swing.*;  
import javax.swing.Timer;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class ShooterDriver 
{   
public static void main(String []args)
{       
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Shooter shooter = new Shooter();

    frame.getContentPane().add(shooter);
    frame.pack();
    frame.setVisible(true); 
}

}

Any help is appreciated. I just need my listeners to work. Left and right keys to move the ship and space to fire. Thank you.

Todd
  • 129
  • 1
  • 1
  • 9
  • This is probably good time to abandon the user of `KeyListener` in favor of the [Key Bindings API](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) which will help solve the focus related issues of `KeyListener` – MadProgrammer Feb 29 '16 at 03:49
  • For [example](http://stackoverflow.com/questions/35003520/program-not-painting-screen-properly/35004777#35004777). Also have a look at [Java KeyListener vs Keybinding](http://stackoverflow.com/questions/23486827/java-keylistener-vs-keybinding/23486873#23486873) – MadProgrammer Feb 29 '16 at 04:13

0 Answers0