Sign in to follow this  

[java] How do you use the key listener on the applet

This topic is 4596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know how to add a keylistener to a text box or button or w/e, but how do you add it to the window itself. So that when I push a key, it will change an x-value to move an image. Jake

Share this post


Link to post
Share on other sites
i don't see a problem...just add it in your init-code for example..

like : this.addKeyListener(this);

and implement the keylistener in the applet:


public void keyPressed(KeyEvent key)
{
int kc = key.getKeyCode();

switch(kc)
{
case KeyEvent.VK_LEFT:
car.sv.turn(-.1);
break;
case KeyEvent.VK_RIGHT:
car.sv.turn(.1);
break;
case KeyEvent.VK_UP:
car.sv.accelerate(.4f);
break;
case KeyEvent.VK_DOWN:
car.sv.accelerate(-.4f);
break;
}
}

Share this post


Link to post
Share on other sites
well i tried that and it still doesnt work, heres my code...


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnimationApplet2 extends Applet implements Runnable, ActionListener
{
//Declare variables and components
Image imgCar;
Thread animationThread;
int intXPos = -300;

// The object we will use to write with instead of the standard screen graphics
Graphics bufferGraphics;
// The image that will contain everything that has been drawn on
// bufferGraphics.
Image offscreen;
// To get the width and height of the applet.
Dimension dim;

Button btnStop = new Button ("Stop");
Button btnStart = new Button ("Start");

public void init ()
{
resize (800, 600);
add (btnStop);
btnStop.addActionListener (this);
add (btnStart);
btnStart.addActionListener (this);

//Load image file
imgCar = getImage (getDocumentBase (), "sonic.gif");

// We'll ask the width and height by this
dim = getSize ();
// We'll redraw the applet each time the mouse has moved.
setBackground (Color.white);
// Create an offscreen image to draw on
// Make it the size of the applet, this is just perfect, Larger
// size could slow it down unnecessarily.
offscreen = createImage (dim.width, dim.height);
// by doing this everything that is drawn by bufferGraphics
// will be written on the offscreen image.
bufferGraphics = offscreen.getGraphics ();
}


public void run ()
{
//keep putting the thread to sleep to allow other processing to occur
while (true)
{
try
{
animationThread.sleep (50);
intXPos += 25; //Increment position for image
if (intXPos > getSize ().width) //Check for right edge of applet panel
{
intXPos = -300; //Start at left edge agian
}
//The speed up animation, pause for a shorter time
//repaint (); //Repaing the screen (calls Update Method)
}

catch (Exception error)
{
}
}
}


public void paint (Graphics gr)
{
// Wipe off everything that has been drawn before
// Otherwise previous drawings would also be displayed.
bufferGraphics.clearRect (0, 0, dim.width, dim.width);
// draw the rect at the current mouse position
// to the offscreen image
bufferGraphics.drawImage (imgCar, intXPos, 100, 300, 300, this); //Draw image in new spot
// draw the offscreen image to the screen like a normal image.
// Since offscreen is the screen width we start at 0,0.
gr.drawImage (offscreen, 0, 0, this);
}


public void update (Graphics gr)
{
//Override the update method
//so that the screen is not cleared between images
paint (gr);
}


public void actionPerformed (ActionEvent e)
{
if (e.getSource () == btnStop)
{
animationThread.stop ();
}
else if (e.getSource () == btnStart)
{
if (animationThread != null)
{
animationThread.stop ();
animationThread = null;
}

animationThread = new Thread (this);
animationThread.start ();
}
}
}



Jake

Share this post


Link to post
Share on other sites
hi!

i think your actionListener is okay...you can check that by a simple output (System.out.println(" it works")); in your actionEvents..

i think all you need to do is put in your call to repaint()...so you can see the changes....

otherwise...please tell us exactly what the applet is doing now...and what it's supposed to do.

hope this helps

Share this post


Link to post
Share on other sites
well this is what I did to get the KeyListener. I couldn't get the keylistner to work when i typed this.addKeyListener(this) it doesnt work. So i had to add a text field to get it to work. Heres the code...


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Program2 extends Applet implements Runnable, KeyListener
{
//Declare variables and components
Image imgCar;
Thread animationThread;
int intXPos = 400;
int intYPos = 100;

// The object we will use to write with instead of the standard screen graphics
Graphics bufferGraphics;
// The image that will contain everything that has been drawn on
// bufferGraphics.
Image offscreen;
// To get the width and height of the applet.
Dimension dim;

boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;

TextField txtOutput = new TextField (25);

public void init ()
{
resize (800, 600);
this.addKeyListener (this);

add (txtOutput);
txtOutput.addKeyListener (this);
//Load image file
imgCar = getImage (getDocumentBase (), "Data/alien_getting_angry_lg_wm.gif");

// We'll ask the width and height by this
dim = getSize ();
// We'll redraw the applet each time the mouse has moved.
setBackground (Color.white);
// Create an offscreen image to draw on
// Make it the size of the applet, this is just perfect, Larger
// size could slow it down unnecessarily.
offscreen = createImage (dim.width, dim.height);
// by doing this everything that is drawn by bufferGraphics
// will be written on the offscreen image.
bufferGraphics = offscreen.getGraphics ();

animationThread = new Thread (this);
animationThread.start ();
}


public void run ()
{
//keep putting the thread to sleep to allow other processing to occur
while (true)
{
try
{
animationThread.sleep (50); // Pause 200 milliseconds (2/10 seconds)
if (left)
{
intXPos -= 25;
}

if (right)
{
intXPos += 25;
}

if (up)
{
intYPos -= 25;
}

if (down)
{
intYPos += 25;
}

if (intXPos < -100)
{
intXPos = getSize ().width + 100;
}
else if (intXPos > getSize ().width + 100)
{
intXPos = -100;
}

if (intYPos < -100)
{
intYPos = getSize ().height + 100;
}
else if (intYPos > getSize ().height + 100)
{
intYPos = -100;
}


}

catch (Exception error)
{
}
}
}


public void paint (Graphics gr)
{
// Wipe off everything that has been drawn before
// Otherwise previous drawings would also be displayed.
bufferGraphics.clearRect (0, 0, dim.width, dim.width);
// draw the rect at the current mouse position
// to the offscreen image
//bufferGraphics.drawImage (imgCar, intXPos, intYPos, this); //Draw image in new spot
bufferGraphics.drawImage (imgCar, 0, 0, 800, 600, this); //Draw image in new spot
// draw the offscreen image to the screen like a normal image.
// Since offscreen is the screen width we start at 0,0.
gr.drawImage (offscreen, 0, 0, this);
}


public void update (Graphics gr)
{
//Override the update method
//so that the screen is not cleared between images
paint (gr);
}


public void keyTyped (KeyEvent e)
{

}


public void keyPressed (KeyEvent e)
{
int c = e.getKeyCode ();

switch (c)
{
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
}
}


public void keyReleased (KeyEvent e)
{
int c = e.getKeyCode ();

switch (c)
{
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
}
}
}

Share this post


Link to post
Share on other sites
hi!

you got to make sure that the applet has the focus...if you add a button it gets the focus by default. if you want the applet to get the focus you must either click into the applet manually without selecting any components...or use :
this.requestFocus();

but remember that the applet loses the focus if you switch windows or select any other component.

hope this helps

Share this post


Link to post
Share on other sites

This topic is 4596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this