0
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class MoveIt extends Applet implements ActionListener
{
    //declare variables and construct color
    private Image cup;
    public Panel keyPad;
    public int top = 15;
    public int left = 15;
    private Button keysArray[];

    //create components for applet
    public void init()
    {
        cup = getImage(getDocumentBase(), "cup.gif");
        Canvas myCanvas = new Canvas();
        setBackground(Color.blue);

            keyPad = new Panel();


        //Construct keyPad and 5 buttons
        keysArray = new Button[5];
        keysArray[0] = new Button("Up");
        keysArray[1] = new Button("Down");
        keysArray[2] = new Button("Right");
        keysArray[3] = new Button("Left");
        keysArray[4] = new Button("Center");

        //set frame and keypad layout to border layout
        setLayout(new BorderLayout());
        keyPad.setLayout(new BorderLayout());

        //add components to keyPadPanel
        keyPad.add(keysArray[0], BorderLayout.NORTH);
        keyPad.add(keysArray[1], BorderLayout.SOUTH);
        keyPad.add(keysArray[2], BorderLayout.EAST);
        keyPad.add(keysArray[3], BorderLayout.WEST);
        keyPad.add(keysArray[4], BorderLayout.CENTER);

        //Add keypadPanel and Canvas to frame
        add(keyPad, BorderLayout.SOUTH);
        add(myCanvas, BorderLayout.NORTH);

        //add actionListener to each button
        for(int i=0; i<5; i++)
        {
            keysArray[i].addActionListener(this);
        }
    }
    //Drag image using paint method
    public void paint(Graphics g)
    {
        g.drawImage(cup, left, top, this);
    }

    public void actionPerformed(ActionEvent e)
    {
        String arg = e.getActionCommand();
        if(arg=="Up")
        {
            top-=15;
        }
        if(arg=="Down")
        {
            top+=15;
        }
        if(arg=="Left")
        {
            left-=15;
        }
        if(arg=="Rigth")
        {
            top+=15;
        }
        if(arg=="Center")
        {
            top=60;
            left=125;
        }

        repaint();
    }
}

The image cup.gif is in the C drive along with the class, java and Html file. Whenever I run the applet no image is produce. everything else works fine. Please any help to get this working will be greatly appreciated. The image is supposed to move in accordance to the button commands on the applet but there is no image being displayed on the applet.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
yogi
  • 1
  • 1
  • 1
    `if(arg=="Up")` while it may work here it is worth reading: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo May 14 '18 at 13:05
  • And "Rigth" has a typo - "Right" is right – Joop Eggen May 14 '18 at 13:22
  • 1) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 3) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and .. – Andrew Thompson May 15 '18 at 10:05
  • .. [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). – Andrew Thompson May 15 '18 at 10:06

0 Answers0