[SOLVED] KeyListeners in JavaApplets
So I've decided to continue on my gun applet. I've gotten the stick figure to move his pistol up to a certain angle and fire bullets off that angle. Now what I want to do is replace the if statements that I used to fire the gun (it was a simple loop once a certain angle was reached) to a key listener of when I hit the spacebar and replace the if statement that moves the gun up or down, with keylisteners to the arrow keys.
So here is what I am looking to find.
1. How do I make a KEY listener like the one I described
2. What are the names for the up arrow, down arrow, and spacebar keys?
3. Given the bellow code how do I do this?
Thanks in advance!!!1
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import java.awt.geom.Line2D;
import javax.swing.JApplet;
import java.awt.*;
public class Gunmaster extends JApplet implements Runnable
{
final static int WIDTH = 400, HEIGHT = 200;
private bulletfire bullet;
double stickx = 50, sticky = 50, angle = -Math.PI/3, angle2 = angle, shoot = 0;
double arrowx = stickx +4, arrowy = sticky + 16;
Thread runner = null;
private Image dbImage;
private Graphics dbg;
private double vy, vx, anglex, angley, angle3;
public void init()
{
bullet = new bulletfire(stickx+24,sticky+15);
bullet.start();
runner = new Thread(this);
runner.start();
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
setSize(WIDTH,HEIGHT);
Rectangle clear = new Rectangle(0,0,WIDTH,HEIGHT);
Gunman stickbob = new Gunman(stickx,sticky,angle);
g2.setColor(Color.white);
g2.fill(clear);
g2.setColor(Color.black);
stickbob.draw(g2);
if(angle == Math.PI/3)
{
bullet.chooseon();
}
bullet2 projectile1 = new bullet2(bullet.getX(),bullet.getY());
bullet.countadd();
g2.rotate(-angle,stickx+4,sticky+16);
projectile1.shoot(g2);
g2.rotate(angle,stickx+4,sticky+16);
}
public void update(Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while ( runner != null) {
if(angle < Math.PI/3)
{
angle = angle + Math.PI/80;
if(angle > Math.PI/3)
{
angle = Math.PI/3;
}
}
repaint();
try {
Thread.sleep(20);
} catch ( InterruptedException e ) {
// do nothing
}
}
}
class bulletfire extends Thread
{
private Rectangle r;
private int count = 0, chooser = 0;
private int xcord, initialx, initialy;
public bulletfire(double xcor, double ycor)
{
xcord = (int)xcor;
initialx = (int)xcor;
initialy = (int)ycor;
r = new Rectangle((int)xcor,(int)ycor,20,20);
}
public void countadd()
{
count++;
}
public void chooseon()
{
chooser = 1;
}
public void chooseoff()
{
chooser = 0;
}
public void turnoff()
{
count = 22;
}
public void turnon()
{
count = 0;
}
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(count < 21)
{
if(chooser == 0)
{
count = 0;
}
if(chooser == 1 && count < 20)
{
xcord = xcord + 10;
r.x = xcord;
}
if(count == 20)
{
count = 0;
chooser = 0;
xcord = initialx;
}
try {
Thread.sleep(20);
} catch ( InterruptedException e ) {
// do nothing
}
}
}
public double getX()
{
return r.x;
}
public double getY()
{
return r.y;
}
}
}