Input Devices
Keyboard Commands and Playing Sound
Most applets rely on mouse input, but sometimes you neeed keyboard input as well. How this is accomplished shows the next applet. You have to klick inside the applet first, to enable keyboard input. Pressing the arrow keys (cursor keys) makes the corresponding triangles turn from red to yellow. Here are some other key constants: F1..F12, HOME, END, PGDN, PGUP. See the documentation for the Event class. Pressing the "S"-key playes a sound file. Sound files for Java applets have to be in the au-format. Since loading a soundfile might go wrong, we have to use the try{} / catch{} statement, to catch exceptions.
The following applet explains how to scroll a tiled background. The tiles are 100 x 100 pixels in size and are tiled seamlessly, so they fill the whole surface. To allow scrolling, they have to overlap 100 pixels on each side and at the same moment they would uncover the window, the variable is set to 0, to cover it again. See the code for how this is accomplished. You can even scroll up/left or down/left etc. by holding more than one key pressed.
|
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project31 extends Applet implements Runnable
{
Image Buffer;
Graphics gBuffer;
Thread runner;
Image tile;
int x, y;
boolean pressedLeft, pressedRight, pressedUp, pressedDown;
public void init()
{
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
tile=getImage(getCodeBase(),"tile.jpg");
}
public boolean keyDown(Event e, int key)
{
if(key==Event.LEFT)
pressedLeft=true;
if(key==Event.RIGHT)
pressedRight=true;
if(key==Event.UP)
pressedUp=true;
if(key==Event.DOWN)
pressedDown=true;
return true;
}
public boolean keyUp(Event e, int key)
{
pressedLeft=pressedRight=pressedUp=pressedDown=false;
return true;
}
public void start()
{
if (runner == null)
{
runner = new Thread (this);
runner.start();
}
}
public void stop()
{
if (runner != null)
{
runner.stop();
runner = null;
}
}
public void run()
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while(true)
{
//Thread sleeps for 10 milliseconds here
try {runner.sleep(10);}
catch (Exception e) { }
if(pressedLeft)x--;
else if(pressedRight)x++;
if(pressedUp)y--;
else if(pressedDown)y++;
drawStuff();
repaint();
}
}
public void drawStuff()
{
//our tile is 100x100 pixels, our applet window 300x300 pixels
if(x>100||x<-100)x=0;
if(y>100||y<-100)y=0;
for(int i=-100;i<400;i+=100)
for(int j=-100;j<400;j+=100)
gBuffer.drawImage(tile,x+i,y+j,this);
gBuffer.setFont(new Font("Helvetica",Font.PLAIN,14));
gBuffer.setColor(Color.blue);
gBuffer.drawString ("Click inside the applet,",70,30);
gBuffer.drawString ("then press the cursor keys to scroll around",15,50);
gBuffer.setColor(Color.black);
gBuffer.drawString ("x="+x+" ,y="+y,110,90);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
drawStuff();
g.drawImage (Buffer,0,0, this);
}
}
|
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|