Reputation: 1
please can someone help me figure out why my Image will not move left or right.
I have an assignment to do where you create an applet that when you press a button (up, down, left, right or center) the image will move in that direction.
my image moves up, down and to the center but will not move left or right
please see below my coding
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MoveIt extends Applet implements ActionListener
{
private Image cup;
private Panel keypad;
public int top = 15;
public int left = 15;
private Button keysArray[];
public void init()
{
cup = getImage(getDocumentBase(), "cup.gif");
Canvas myCanvas = new Canvas();
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());
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);
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 - 15;
if (arg == "Down")
top = top + 15;
if (arg == "Left")
left = left - 15;
if (arg == "Right")
left = left + 15;
if (arg == "Center")
top = 60;
left = 125;
repaint();
}
}
Upvotes: 0
Views: 246
Reputation: 347184
There are two mistakes...
arg == "Up"
Is not how you do String
comparison in Jave, instead, you should be using String.equals
, for example...
"Up".equals(arg)
Have a look at How do I compare strings in Java? for more details...
You are trying to perform two actions within a if
condition, but only the first satememt is been executed...
if (arg == "Center")
top = 60;
left = 125;
The problem here, is left = 125;
will ALWAYS be executed, regardless of which button you've pressed, wch means the object can't move left, ever.
If you want multiple statements to be executed within a if
statement, you need to wrap them within {...}
, which is just a good idea for all your if
statements really
if (arg == "Center") {
top = 60;
left = 125;
}
Updated...
I should also have a discussion about the relevance of using a Applet
over an JApplet
or using applets as all as a teaching tool, but you may not have much control over those decisions...
Just beware, Applet
has been replaced with JApplet
so 15 years ago and applets generally are more difficult to develop then stand alone programs (using JFrame
).
As a general rule, you should always call super.paint
as painting is typically achieved by chain a series of other methods together, this could produce paint artifacts when updated.
It is also generally discouraged to override paint
of top level containers, like Applet
as well.
Upvotes: 1