Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-27-2008, 09:08 PM
Member
 
Join Date: Sep 2008
Posts: 37
Rep Power: 0
shwein is on a distinguished road
Default applet problem
hello, I am a very dumb java-newbie with -probably- a very stupid question:
the case--> i have made an applet with 4 buttons standing for 4 direction, and a rectangle that needs to move in the submitted direction but there something wrong with the co-ordinates. i can't figure it out: only 'right' seems to work properly... its driving me insane!!!
here is the code
thx

<code>
public class AppletTest extends JApplet{
int x = 20;
int y = 220;
int width = 40;
int height = 40;
int a=0;

public class ButtonPanel extends JPanel {

Timer t = new Timer(500, new TimerHandler());


public ButtonPanel(){

setLayout(null);
setBackground(Color.blue);
JButton btnUp = new JButton("Up");
JButton btnDown = new JButton("Down");
JButton btnLeft = new JButton("Left");
JButton btnRight = new JButton("Right");
JButton btnStart = new JButton("Start");
JButton btnStop = new JButton("Stop");
btnUp.addActionListener(new btnUpHandler());
btnDown.addActionListener(new btnDownHandler());
btnLeft.addActionListener(new btnLeftHandler());
btnRight.addActionListener(new btnRightHandler());
btnUp.setBounds(215,10,70,30);
btnDown.setBounds(215,70,70,30);
btnLeft.setBounds(145,40,70,30);
btnRight.setBounds(285,40,70,30);
btnStart.setBounds(1,1,70,30);
btnStop.setBounds(1,40,70,30);
add(btnUp);
add(btnDown);
add(btnLeft);
add(btnRight);

}

class TimerHandler implements ActionListener{
public void actionPerformed (ActionEvent e){

switch(a){
case 1:x-=1;
case 2:y+=1;
case 3:x+=1;
case 4:y-=1;
case 0:
}
repaint();

}
}

public void paint(Graphics g){
super.paint(g);
g.fillRect(x, y,width, height);
}



class btnUpHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
t.stop();
a=1;
t.start();

}
}
class btnRightHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
t.stop();
a=2;
t.start();

}
}
class btnDownHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
t.stop();
a=3;
t.start();

}
}
class btnLeftHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
t.stop();
a=4;
t.start();

}
}


}


public void init(){
setSize(500, 300);
JPanel pnlButtons = new ButtonPanel();
setContentPane(pnlButtons);
}



}
</code>
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-27-2008, 09:14 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Look at the switch statement. It needs break after the case if you do not want the following case to be executed.

Add a println() at the end of the switch statement to display the values of x and y to see if they are getting the values you expect.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-28-2008, 12:17 AM
Member
 
Join Date: Sep 2008
Posts: 37
Rep Power: 0
shwein is on a distinguished road
Default
thx norm, and i also will post following applet-related questions in the therefor created section, i ve just noticed it
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem getting the image(APPLET) kof20012 Java Applets 10 09-11-2008 07:22 PM
Serious applet problem willemjav Java Applets 1 03-25-2008 06:01 PM
IMPORTANT: Applet Problem Please Help! nvidia Java Applets 1 01-22-2008 03:25 AM
applet problem plz HELP shibajisanyal Java Applets 1 01-09-2008 02:47 AM
Applet problem Ed Java Applets 2 07-02-2007 04:35 PM


All times are GMT +2. The time now is 12:16 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org