Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Another issue to the same applet

  Asked By: Matilda    Date: Nov 17    Category: Java    Views: 775
  

when i try to move the image using the buttons..
nothing happens .. can any1 enlighten me on this ? thanks !

----------------------CODE------------------------------------------
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 =10;
public int left =10;

public void init()
{
cup = getImage(getDocumentBase(),"cup.gif");
Canvas myCanvas = new Canvas();
keyPad = new Panel();
Button Up = new Button("Up");
Button Down = new Button("Down");
Button Left = new Button("Left");
Button Right = new Button("Right");
Button Center = new Button("Center");
setBackground(Color.blue);

setLayout(new BorderLayout());

add(keyPad,BorderLayout.SOUTH);
keyPad.add(Up);
keyPad.add(Down);
keyPad.add(Left);
keyPad.add(Right);
add(myCanvas,BorderLayout.NORTH);
keyPad.add(Center);



Up.addActionListener(this);
Down.addActionListener(this);
Left.addActionListener(this);
Right.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 -= 10;
if(arg == "Down")
top +=10;
if(arg == "Left")
left -= 10;
if(arg == "Right")
left += 10;
if(arg == "Center")
{
top = 50;
left = 50;
}
}
repaint();

}



Share: 

 

1 Answer Found

 
Answer #1    Answered By: Russell Burns     Answered On: Nov 17

The following tested okay on with appetviewer. your repaint() call was
outside the actionPerformed method.
I modified the center  command so that it would center your image  to the
center of the applet  panel.
************************

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 =10;
public int left  =10;


public void  init()
{
cup = getImage(getDocumentBase(),"cup.gif");
Canvas myCanvas = new Canvas();
keyPad = new Panel();
Button Up = new Button("Up");
Button Down = new Button("Down");
Button Left = new Button("Left");
Button Right = new Button("Right");
Button Center = new Button("Center");
setBackground(Color.blue);


setLayout(new BorderLayout());


add(keyPad,BorderLayout.SOUTH);
keyPad.add(Up);
keyPad.add(Down);
keyPad.add(Left);
keyPad.add(Right);
add(myCanvas,BorderLayout.NORTH);
keyPad.add(Center);




Up.addActionListener(this);
Down.addActionListener(this);
Left.addActionListener(this);
Right.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 -= 10;
if(arg == "Down")
top +=10;
if(arg == "Left")
left -= 10;
if(arg == "Right")
left += 10;
if(arg == "Center")
{
//top = 50;
//left = 50;
top = getHeight()/2 - cup.getHeight(this)/2;
left = getWidth()/2 - cup.getWidth(this)/2;
}
repaint();
}
}

 
Didn't find what you were looking for? Find more on Another issue to the same applet Or get search suggestion and latest updates.




Tagged: