So the objective (well the starting point anyway) of this program is to press the arrow keys and have bullets come from the middle of the screen in the direction of the arrow. I have four images of bullets I created in paint and am using these two classes:
this is the class that creates and organizes the bullets:
public class BulletAnimator extends JPanel implements ActionListener
{
int startX,startY;
boolean yAxis = false;
boolean xAxis = false;
Timer animator;
String direction;
public BulletAnimator(int sX, int sY, String d)
{
direction = d;
startX = sX;
startY = sY;
animator = new Timer (10, this);
animator.start();
}
//chooses the right picture for the right direction
public ImageIcon chooseBulletPicture ()
{
String path = "bullet";
if (direction.equals("left"))
path += "LEFT";
else if (direction.equals ("right"))
path += "RIGHT";
else if (direction.equals ("up"))
path += "UP";
else
path += "DOWN";
path += ".png";
ImageIcon b= new ImageIcon (path);
return b;
}
public void paintComponent (Graphics g)
{
super. paintComponent(g);
if (startX >= 500 || startY >= 500)
animator.stop();
if (direction.equals ("up"))
startY -=2;
else if (direction.equals ("down"))
startY +=2;
else if (direction.equals ("right"))
startX += 2;
else
startX -=2;
chooseBulletPicture().paintIcon (this, g, startX, startY);
}
public void actionPerformed(ActionEvent e)
{
repaint ();
}
}
and this is the class is to add keyListeners and test it:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame implements KeyListener
{
JFrame f;
public Test ()
{
addKeyListener (this);
setFocusable (true);
f = new JFrame ();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(500, 500);
}
public void keyPressed(KeyEvent e)
{
BulletAnimator s = new BulletAnimator (250, 250, "initialized---blank");
//creates bullet w/ correct direction
if (e.getKeyCode() == KeyEvent.VK_RIGHT )
{
s = new BulletAnimator (250, 250, "right");
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT )
{
s = new BulletAnimator (250, 250, "left");
}
else if (e.getKeyCode() == KeyEvent.VK_UP )
{
s = new BulletAnimator (250, 250, "up");
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN )
{
s = new BulletAnimator (250, 250, "down");
}
System.out.println ("keyPressed method read");//checks if keyPressed method was looked at
f.add (s);
repaint();
}
public void keyReleased (KeyEvent e)
{}
public void keyTyped (KeyEvent e)
{}
public static void main (String [] args)
{
Test t = new Test ();
}
}
As you can see I tried to put in a test that says “keyPressed method read”… when the program runs it doesn’t print. In fact nothing at all happens its just a grey screen… quite frustrating really. Well thank you in advance if you took the time to look at it, I would greatly appreciate any advice!
You are extending the
JFrameclass so thatnew Test()is creating an (extended) instance of aJFrameand then you are adding a KeyListener to this instance. However, the frame you are making visible is the one you created in theTest()constructor throughYou did not add your
KeyListenerto this instance of JFrame. So the KeyListener would only respond if theTestinstance was visible and in focus.I would suggest doing this:
To paint the Icons, you also need to modify
BulletAnimatorcode:And change
f.add(s)toadd(s)inkeyPressedmethod to add the icons to the Test frame.Disclaimer: As others pointed out, there are better solutions to handling key presses. My suggestions above only identified the problems with your original code posted above.