Click here to Skip to main content
15,907,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've written a code for a Clickable Applet.
The problem I'm having is displaying the .gif file.
The code compiles successfully and once I run the applet, the buttons work. It just that the picture doesn't show.

What I have tried:

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();
	}
}
Posted
Comments
M4rLask 5-Sep-18 12:34pm    
Have you already checked that the path of your image is correct?
Member 13652359 5-Sep-18 16:25pm    
Yes, it is the correct path.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900