import java.awt.*; import java.awt.event.*; import java.applet.*; public class MoveIt extends Applet implements ActionListener { //declare variables private Image cup; private Panel keyPad; public int top = 15; public int left = 15; private Button keysArray[]; public void init() { Image cup = getImage(getDocumentBase(), "cup.gif"); Canvas myCanvas = new Canvas(); Panel keyPad = new Panel(); Button up = new Button("Up"); Button left = new Button("Left"); Button right = new Button("Right"); Button down = new Button("Down"); Button center = new Button("Center"); setBackground(Color.blue); setLayout(new BorderLayout()); keyPad.setLayout(new BorderLayout()); //Adding the Buttons keyPad.add(up, BorderLayout.NORTH); keyPad.add(down, BorderLayout.SOUTH); keyPad.add(right, BorderLayout.EAST); keyPad.add(left, BorderLayout.WEST); keyPad.add(center, BorderLayout.CENTER); add(myCanvas, BorderLayout.NORTH); add(keyPad, BorderLayout.SOUTH); //ActionListener statements up.addActionListener(this); down.addActionListener(this); right.addActionListener(this); left.addActionListener(this); center.addActionListener(this); } public void paint( Graphics g ) { g.drawImage(cup, left, top, this ); } public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand(); if(arg == "up") top = top - 10; if(arg == "down") top = top + 20; if(arg == "left") left = left - 10; if(arg == "right") left = left + 10; if(arg == "center") top = 60; left = 125; repaint(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)