import java.applet.*; import java.awt.*; import java.awt.event.*; public class Esim3 extends Applet implements KeyListener, ActionListener { String jono = ""; Button nappi1,nappi2; int x,y; // sijainti public void init() { add (nappi1 = new Button("Aloita !!!")); add (nappi2 = new Button("Palauta alkutilanne")); nappi1.addKeyListener(this); nappi2.addActionListener(this); resize(300,300); x=125; y=125; } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "Palauta alkutilanne") { x=125; y=125; repaint(); } } public void keyPressed(KeyEvent e) { if (KeyEvent.VK_RIGHT == e.getKeyCode()) if (x < 250) x = x+3; if (KeyEvent.VK_LEFT == e.getKeyCode()) if (x > 0) x = x-3; if (KeyEvent.VK_UP == e.getKeyCode()) if (y > 0) y = y-3; if (KeyEvent.VK_DOWN == e.getKeyCode()) if (y < 250) y = y+3; repaint(); } // Pakolliset public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void paint (Graphics g) { g.fillRect(x,y,50,50); // x.drawString(jono,30,30); } }