Java by Example - 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. Everything that might go wrong (causing the program to crush) can be put into the try{} statement, everything that is supposed to be executed then (if any) can be put into the catch{} part.
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project30 extends Applet
{
Image Buffer;
Graphics gBuffer;
boolean pressedLeft, pressedRight, pressedUp, pressedDown;
AudioClip mySound;
public void init()
{
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
try
{
mySound=getAudioClip(getCodeBase(),"sound.au");
}
catch (Exception e){}
}
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;
if(key=='s'||key=='S')
mySound.play();
repaint();
return true;
}
public boolean keyUp(Event e, int key)
{
pressedLeft=pressedRight=pressedUp=pressedDown=false;
repaint();
return true;
}
public void drawStuff()
{
gBuffer.setColor(Color.blue);
gBuffer.fillRect(0,0,size().width,size().height);
gBuffer.setFont(new Font("Helvetica",Font.PLAIN,14));
gBuffer.setColor(Color.orange);
//if we want to use "" inside a string, we have to mask it with \!
gBuffer.drawString ("Click inside the applet, then try",50,30);
gBuffer.drawString ("the cursor keys and the \"S\"-key!",45,50);
//left
int xl[] = {40,100,100};
int yl[] = {160,130,190};
if(pressedLeft)
gBuffer.setColor(Color.yellow);
else
gBuffer.setColor(Color.red);
gBuffer.fillPolygon(xl,yl,3);
//right
int xr[] = {260,200,200};
int yr[] = {160,130,190};
if(pressedRight)
gBuffer.setColor(Color.yellow);
else
gBuffer.setColor(Color.red);
gBuffer.fillPolygon(xr,yr,3);
//up
int xu[] = {120,150,180};
int yu[] = {110,60,110};
if(pressedUp)
gBuffer.setColor(Color.yellow);
else
gBuffer.setColor(Color.red);
gBuffer.fillPolygon(xu,yu,3);
//down
int xd[] = {120,150,180};
int yd[] = {210,260,210};
if(pressedDown)
gBuffer.setColor(Color.yellow);
else
gBuffer.setColor(Color.red);
gBuffer.fillPolygon(xd,yd,3);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
drawStuff();
g.drawImage (Buffer,0,0, this);
}
}
|
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);
}
}
|
|