MOVE ZOMBIE UP DOWN LEFT RIGHT CONTINUOSLY WITH ONE CLICK
public class j extends java.applet.Applet implements Runnable {
Image z[] = new Image [2];
Image crImg;
Thread r;
int x;
int y;
public void init() {
String zSrc[] = {"z1.png", "z2.png",};
for (int i = 0; i < zombies.length; i++) {
z[i] = getImage(getCodeBase(), "images/" + zSrc[i]); }}
public void start() {
if (r == null) {
r = new Thread(this);
r.start();}}
public void stop() {
if (r != null) {
r.stop();
r = null;}
void pause (int time) {
try { Thread.sleep(time); }
catch (InterruptedException e) { }
}
public void run() {
walk (0, size().width); }
void walk (int start, int end) {
for (int i = start; i < end; i += 1) {
x = i;
if (crImg == zombies[0])
crImg = zombies[1];
else crImg = zombies[0];
repaint();
pause(150);}}
public void paint(Graphics g) {
if (crImg != null)
g.drawImage(crImg, x, y, width, height, this);
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Keyboard1 extends Applet
implements KeyListener, MouseListener {
int width, height;
int x, y;
String s = "";
public void init() {
width = getSize().width;
height = getSize().height;
setBackground( Color.black );
x = width/2;
y = height/2;
addKeyListener( this );
addMouseListener( this );
}
public void keyPressed( KeyEvent e ) { }
public void keyReleased( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
if ( c != KeyEvent.CHAR_UNDEFINED ) {
s = s + c;
repaint();
e.consume();
}
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) { }
public void mouseReleased( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) {
x = e.getX();
y = e.getY();
s = "";
repaint();
e.consume();
}
public void paint( Graphics g ) {
g.setColor( Color.gray );
g.drawLine( x, y, x, y-10 );
g.drawLine( x, y, x+10, y );
g.setColor( Color.green );
g.drawString( s, x, y );
}
}
http://www.dgp.toronto.edu/~mjmcguff/learn/java/05-keyboardInput/
Comments
Leave a comment