Domanda

I Have applet with a image of a java cup that can be repositioned by the clicking of 5 buttons to move it in the main area of the applet window. the issue im having is the buttons are not being displayed in im applet the only thing that is showing is my cup.gif on the blue background, can any one see the problem with the code ,i want the buttons to show and work And yes guys I know AWT is old but i have to learn it for my course...any help would be great thanks guys!

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


 public class moveIt extends Applet implements ActionListener

 {

private Image cup;
private Panel Keypad = new Panel();
public int top = 15;
public int left = 15;
private Button Keyarray[] = new Button[5];
public void init ()
{
    cup=getImage(getDocumentBase(), "cup.gif");
    Canvas myCanvas= new Canvas();


Keyarray[0] = new Button ("Up");
Keyarray[1] = new Button ("Left");
Keyarray[2] = new Button ("Down");
Keyarray[3] = new Button ("Right");
Keyarray[4] = new Button ("Center");
setBackground(Color.BLUE);

Panel frame = new Panel();
frame.setLayout(new BorderLayout());
frame.add(myCanvas, BorderLayout.NORTH);
frame.add(Keypad, BorderLayout.SOUTH);
Keypad.setLayout(new BorderLayout());


Keypad.add(Keyarray[0], BorderLayout.NORTH);
Keypad.add(Keyarray[1], BorderLayout.WEST);
Keypad.add(Keyarray[2], BorderLayout.SOUTH);
Keypad.add(Keyarray[3], BorderLayout.EAST);
Keypad.add(Keyarray[4], BorderLayout.CENTER);

Keyarray[0].addActionListener(this);
Keyarray[1].addActionListener(this);
Keyarray[2].addActionListener(this);
Keyarray[3].addActionListener(this);
Keyarray[4].addActionListener(this);


}//end of method init


public void paint(Graphics g)


{

    g.drawImage(cup, left, top, this);

}

public void actionPerformed(ActionEvent e)
{
    String arg= e.getActionCommand();

     if (arg.equals("Up"))
                top -= 15;
            if (arg.equals("down"))
                top += 15;
            if (arg.equals("Left"))
                left -= 15;
            if (arg.equals("Right"))
                left += 15;
            if (arg.equals("Center"))
            {
                top=60;
            }   left=125;

    repaint();



}//end paint method

  }//end of class
È stato utile?

Soluzione

  1. You never add the frame to the applet this.add(frame)
  2. Once you do, you will have to setOpaque(false) to the frame so you can see the background

Important Side Notes:

  • Instead of painting on the Applet directly, you should be painting rather on a JPanel and override it's paintComponent method.

  • You Need to call super.paint(g) or super.paintComponent(g)(for JPanel) in the paint method, as to not break the paint chain and see all kinds of wierd paint artifacts

  • I just noticed the AWT components. AWT is pretty much obsolete. You should move it up to using Swing. See the Swing Tutorials

  • Use Java naming convention. Variables begin with lower case letters, using camelCasing e.g. KeyarraykeyArray. Class names begin with capital letters using CamelCasing e.g. moveItMoveIt

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top