Results 1 to 12 of 12
- 03-20-2011, 11:14 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
No idea why my code won't work...
I'm writing a simple boxing match code, which compiles fine, but when I run it only one of the two boxers appears (in the middle of the ring), the keystroke for the right boxer to move right makes another boxer appear on the left, the punch and move left keystrokes for the right boxer do nothing, the punch and move right keystrokes for the left boxer do nothing and the move left keystroke for the left boxer (after he has appeared) doesn't erase the old position boxer (even though I repaint the background each time). My Boxer.java code is:
and the boxingmatch.java code is:Java Code:public class Boxer { //variables private int health=100; private int position; private boolean punching=false; private static int ringwidth; static int boxers; private int boxerno; //constructors public Boxer(){ if (boxers==0){ position=(int)(-ringwidth/2); boxers=1; boxerno=1; } else if(boxers==1) { position=(int)(ringwidth/2); boxers=2; boxerno=2; } } //getters and setters public static void setRingwidth(int x){ ringwidth=x; } public static int getRingwidth(){ return ringwidth; } public void setHealth(int x) { if(x<101){ health=x; } else { health=100; } } public int getHealth() { return health; } public void setPosition(int x){ if(2*x>ringwidth){ position=(int)(ringwidth/2); } else if(-2*x<ringwidth){ position=(int)(-ringwidth/2); } else { position=x; } } public int getPosition(){ return position; } public void setPunching(boolean b) { punching=b; } public boolean getPunching(){ return punching; } public int getBoxerno(){ return boxerno; } //methods public void punched(int[] powerposition){ if(position-powerposition[1]<11&&position-powerposition[1]>-11){ setHealth(getHealth()-powerposition[0]); } } public int[] punch(){ setPunching(true); int x=(int) (25+getHealth()/2); int[] powerposition=new int[2]; powerposition[0]=x; powerposition[1]=position; setPunching(false); return powerposition; } public void moveleft(){ setPosition(getPosition()-10); } public void moveright(){ setPosition(getPosition()+10); } }
sorry for so much code, but as it thinks it's a healthy program I can't find where the errors are coming from.Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.color.*; public class boxingmatch implements KeyListener{ public static void main (String[] args) { boxingmatch box=new boxingmatch(); box.go(); } Boxer a; Boxer b; Picture pic; public void go(){ JFrame frame=new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField tfield=new JTextField(); a=new Boxer(); b=new Boxer(); pic=new Picture(); pic.repaint(); frame.getContentPane().add(BorderLayout.SOUTH,tfield); frame.getContentPane().add(BorderLayout.CENTER,pic); frame.setSize(900,900); frame.setVisible(true); Boxer.setRingwidth(pic.getWidth()*10/11); tfield.setFocusable(true); tfield.addKeyListener(this); tfield.requestFocus(); } public void keyTyped(KeyEvent event) { } public void keyReleased(KeyEvent event) { } public void keyPressed(KeyEvent event) { if(event.getKeyCode()==KeyEvent.VK_A) { a.moveleft(); } else if(event.getKeyCode()==KeyEvent.VK_D) { a.moveright(); } else if(event.getKeyCode()==KeyEvent.VK_S) { b.punched(a.punch()); } else if(event.getKeyCode()==KeyEvent.VK_K) { a.punched(b.punch()); } else if(event.getKeyCode()==KeyEvent.VK_L) { b.moveright(); } else if(event.getKeyCode()==KeyEvent.VK_J) { b.moveright(); } pic.repaint(); } class Picture extends JPanel { public void paintComponent (Graphics g) { g.setColor(Color.black); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(Color.red); g.fillRect(0,(int)(this.getHeight()*49/100),this.getWidth(),(int)(this.getHeight()*1/50)); g.fillRect(0,(int)(this.getHeight()*59/100),this.getWidth(),(int)(this.getHeight()*1/50)); g.fillRect(0,(int)(this.getHeight()*69/100),this.getWidth(),(int)(this.getHeight()*1/50)); g.setColor(Color.blue); g.fillRect(0,(int)(this.getHeight()*4/5),this.getWidth(),(int)(this.getHeight()*1/5)); Color skin=new Color(239,238,207); g.setColor(skin); g.fillRect((int)(this.getWidth()/2+a.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int)(this.getHeight()*9/10)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int)(this.getHeight()*9/10)); g.setColor(Color.black); g.fillRect((int)(this.getWidth()/2+a.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int)(this.getHeight()*1/20)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int)(this.getHeight()*1/20)); g.fillRect((int)(this.getWidth()/2+a.getPosition()-1),(int)(this.getHeight()*3/4),2,(int)(this.getHeight()*1/4)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-1),(int)(this.getHeight()*3/4),2,(int)(this.getHeight()*1/4)); g.setColor(Color.green); g.fillRect((int)(this.getWidth()/2+a.getPosition()-this.getWidth()/20),(int)(this.getHeight()*11/20),(int)(this.getWidth()/10),(int)(this.getHeight()*1/5)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-this.getWidth()/20),(int)(this.getHeight()*11/20),(int)(this.getWidth()/10),(int)(this.getHeight()*1/5)); g.setColor(skin); if (a.getPunching()) { g.fillRect((int)(this.getWidth()/2+a.getPosition()),(int)(this.getHeight()*1/4),(int)(this.getWidth()*3/10),(int)(this.getHeight()*1/15)); } if (b.getPunching()) { g.fillRect((int)(this.getWidth()/2+b.getPosition()),(int)(this.getHeight()*1/4),-(int)(this.getWidth()*3/10),-(int)(this.getHeight()*1/15)); } } } }
-
Could you post formatted code so we are able to read it? Also, how is it not working? Errors? Exceptions? Incorrect behavior? Details and any and all error messages will help us.
- 03-20-2011, 03:56 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
How do I post in code?
I wrapped the code above in [c ode] [/c ode] but I can't work out how to show line numbers along the side...
There are no errors or exceptions whatsoever, it thinks it's a healthy code, and I can't find where the mistakes come from when I read the code :(
-
Heck, I don't want line numbers, but is all of your code without any indentations whatsoever and fully left justified? I don't know about you, but I find non-indented code very difficult if not impossible to read. And my experience here has been that the easier you make it for someone else to understand your problem and answer it, the better your chances of getting a decent answer. But up to you.
- 03-20-2011, 10:24 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
I'm sorry, I've never considered indentations (I write my code in notepad and run with command prompt).
Does the compiler ignore tabs as it does spaces?
Here is my indented code (one indentation in or out as appropriate at each { or }, is that the etiquette?)
boxingmatch.java:
Boxer.java:Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.color.*; public class boxingmatch implements KeyListener{ public static void main (String[] args) { boxingmatch box=new boxingmatch(); box.go(); } Boxer a; Boxer b; Picture pic; public void go(){ JFrame frame=new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField tfield=new JTextField(); a=new Boxer(); b=new Boxer(); pic=new Picture(); pic.repaint(); frame.getContentPane().add(BorderLayout.SOUTH,tfield); frame.getContentPane().add(BorderLayout.CENTER,pic); frame.setSize(900,900); frame.setVisible(true); Boxer.setRingwidth(pic.getWidth()*10/11); tfield.setFocusable(true); tfield.addKeyListener(this); tfield.requestFocus(); } public void keyTyped(KeyEvent event) { } public void keyReleased(KeyEvent event) { } public void keyPressed(KeyEvent event) { if(event.getKeyCode()==KeyEvent.VK_A) { a.moveleft(); } else if(event.getKeyCode()==KeyEvent.VK_D) { a.moveright(); } else if(event.getKeyCode()==KeyEvent.VK_S) { b.punched(a.punch()); } else if(event.getKeyCode()==KeyEvent.VK_K) { a.punched(b.punch()); } else if(event.getKeyCode()==KeyEvent.VK_L) { b.moveright(); } else if(event.getKeyCode()==KeyEvent.VK_J) { b.moveright(); } pic.repaint(); } class Picture extends JPanel { public void paintComponent (Graphics g) { g.setColor(Color.black); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(Color.red); g.fillRect(0,(int)(this.getHeight()*49/100),this.getWidth(),(int)(this.getHeight()*1/50)); g.fillRect(0,(int)(this.getHeight()*59/100),this.getWidth(),(int)(this.getHeight()*1/50)); g.fillRect(0,(int)(this.getHeight()*69/100),this.getWidth(),(int)(this.getHeight()*1/50)); g.setColor(Color.blue); g.fillRect(0,(int)(this.getHeight()*4/5),this.getWidth(),(int)(this.getHeight()*1/5)); Color skin=new Color(239,238,207); g.setColor(skin); g.fillRect((int)(this.getWidth()/2+a.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int) (this.getHeight()*9/10)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int) (this.getHeight()*9/10)); g.setColor(Color.black); g.fillRect((int)(this.getWidth()/2+a.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int) (this.getHeight()*1/20)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-this.getWidth()/20),(int)(this.getHeight()/10),(int)(this.getWidth()/10),(int) (this.getHeight()*1/20)); g.fillRect((int)(this.getWidth()/2+a.getPosition()-1),(int)(this.getHeight()*3/4),2,(int)(this.getHeight()*1/4)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-1),(int)(this.getHeight()*3/4),2,(int)(this.getHeight()*1/4)); g.setColor(Color.green); g.fillRect((int)(this.getWidth()/2+a.getPosition()-this.getWidth()/20),(int)(this.getHeight()*11/20),(int)(this.getWidth()/10),(int) (this.getHeight()*1/5)); g.fillRect((int)(this.getWidth()/2+b.getPosition()-this.getWidth()/20),(int)(this.getHeight()*11/20),(int)(this.getWidth()/10),(int) (this.getHeight()*1/5)); g.setColor(skin); if (a.getPunching()) { g.fillRect((int)(this.getWidth()/2+a.getPosition()),(int)(this.getHeight()*1/4),(int)(this.getWidth()*3/10),(int)(this.getHeight() *1/15)); } if (b.getPunching()) { g.fillRect((int)(this.getWidth()/2+b.getPosition()),(int)(this.getHeight()*1/4),-(int)(this.getWidth()*3/10),-(int)(this.getHeight() *1/15)); } } } }
As I say, neither the compiler nor the jvm have any problem with my code, it just doesn't do what it's meant to.Java Code:public class Boxer { //variables private int health=100; private int position; private boolean punching=false; private static int ringwidth; static int boxers; private int boxerno; //constructors public Boxer(){ if (boxers==0){ position=(int)(-ringwidth/2); boxers=1; boxerno=1; } else if(boxers==1) { position=(int)(ringwidth/2); boxers=2; boxerno=2; } } //getters and setters public static void setRingwidth(int x){ ringwidth=x; } public static int getRingwidth(){ return ringwidth; } public void setHealth(int x) { if(x<101){ health=x; } else { health=100; } } public int getHealth() { return health; } public void setPosition(int x){ if(2*x>ringwidth){ position=(int)(ringwidth/2); } else if(-2*x<ringwidth){ position=(int)(-ringwidth/2); } else { position=x; } } public int getPosition(){ return position; } public void setPunching(boolean b) { punching=b; } public boolean getPunching(){ return punching; } public int getBoxerno(){ return boxerno; } //methods public void punched(int[] powerposition){ if(position-powerposition[1]<11&&position-powerposition[1]>-11){ setHealth(getHealth()-powerposition[0]); } } public int[] punch(){ setPunching(true); int x=(int) (25+getHealth()/2); int[] powerposition=new int[2]; powerposition[0]=x; powerposition[1]=position; setPunching(false); return powerposition; } public void moveleft(){ setPosition(getPosition()-10); } public void moveright(){ setPosition(getPosition()+10); } }
-
rather than throw 5 problems at us you should've taken it one method at a time. anyhow, let's try to break-down your program-logic.
when I run it only one of the two boxers appears (in the middle of the ring)
your program follows this path when run...
Java Code:BoxingMatch.go() ... a = new Boxer() b = new Boxer() public Boxer(){ if (boxers==0){ position=(int)(-ringwidth/2); boxers=1; boxerno=1; } else if(boxers==1) { position=(int)(ringwidth/2); boxers=2; boxerno=2; } }
firstly, i'm surprised that any boxer shows up since the variable 'boxers' was not initialised:
Java Code:static int boxers;
which means that boxers should = 'null' rather than '0' to start off with. secondly, the second boxer will never show up unless the first boxer has been created, because 'if boxers==1' doesn't happen in your script except when the first boxer is made. i understand that you wanted to limit the number of boxers created to two, but there are better ways to do this, e.g.
Java Code:public Boxer() { if (boxers == 0) { //set position for boxer 1 } else if (boxers == 1) { //set position for boxer 2 } boxerno = ++boxers; //the boxerno = boxers+1 & boxers = boxers+1 }
as for the boxers spawning in the middle, this is easily explainable by your calculation of setting the position of the boxers. you've set boxer 1's position to:
Java Code:position=(int)(-ringwidth/2);
say the ring is 300 pixels wide. 300/2 = 150 pixels. that would be the middle of the ring. however, for boxer 1 you have used negative-ringwidth. which means that the boxer1s position is set outside of the viewable area of your applet which would be 0 to 300 pixels (i.e. -150 horizontal is not shown on your screen).
as for the next boxer,
Java Code:position=(int)(ringwidth/2);
positive-ringwidth/2 = middle of the ring. change this to something like:
Java Code:position = (int) (ringwidth - 20 - WIDTH_OF_BOXER);
to make boxer2 start 20pixels from the right-border of the ring. similarly you could set boxer1 to:
Java Code:position = 20;
which would set it 20pixels from the left border of the ring.Last edited by ozzyman; 03-21-2011 at 01:48 AM.
-
the keystroke for the right boxer to move right makes another boxer appear on the left
i'm guessing this "new" boxer is actually the boxer1 whose initial position was set to -150px (i.e. hidden)
- 03-21-2011, 01:53 AM #8
-
the punch and move left keystrokes for the right boxer do nothing, the punch and move right keystrokes for the left boxer do nothing and the move left keystroke for the left boxer (after he has appeared) doesn't erase the old position boxer (even though I repaint the background each time).
if i want to move the position to the right, and x=20, setPosition is called and this is what happens...
Java Code:if (2*20 > 300) { //does not become true unless x is between 150-300pixels - why? position=(int)(ringwidth/2); //set position to middle of the ring } else if (-2*20 < 300) { //if the boxer is off the screen - why? - as with boxer1 (-150 in your case) { position=(int)(-ringwidth/2); //set position off-screen } else { position = x; //set to the position it is supposed to be set to }
...which means that if the any boxer is between (150 to 300) or (0 to -infinity) the boxers position is not set. i think what you mean to write is something to the tune of this pseudo-code:
for boxer1-only and something else but similar for boxer2Java Code:if (boxer1.position is between 0 and half way) { //set new position } else if (boxer1.position is < 0) { //set boxer1.position to 0 } else if (boxer1.position > half-way) { //set boxer1.position to half-way }
-
- 03-21-2011, 03:34 AM #11
No, it isn't. It's (char) 0, also known as the nul character (note: nul, not null).For char it is a space.
Java Code:public class DefaultCharValue { private char c; public static void main(String[] args) { DefaultCharValue dfv = new DefaultCharValue(); System.out.println("Default value of c is [" + dfv.c + "] (numeric value = " + (int) dfv.c + ")"); dfv.c = ' '; System.out.println("Value of c is now [" + dfv.c + "] (numeric value = " + (int) dfv.c + ")"); } }
- 03-21-2011, 03:41 AM #12
Similar Threads
-
Will this code work
By rajat16 in forum XMLReplies: 3Last Post: 09-24-2010, 11:52 AM -
I can't get my code to work?
By ComicStix in forum New To JavaReplies: 5Last Post: 09-19-2010, 03:15 PM -
Code does not work
By scotts in forum New To JavaReplies: 12Last Post: 06-19-2010, 09:37 AM -
Why this code don't work?
By artemff in forum CLDC and MIDPReplies: 6Last Post: 04-16-2010, 02:57 AM -
How does the '*' work in the code?
By DrMath in forum New To JavaReplies: 3Last Post: 10-28-2009, 09:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks