Results 1 to 12 of 12
  1. #1
    hopelessnoob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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:
    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);
    }
    }
    and the boxingmatch.java code is:

    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));
    }
    }
    }
    }
    sorry for so much code, but as it thinks it's a healthy program I can't find where the errors are coming from.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    hopelessnoob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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 :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hopelessnoob View Post
    I wrapped the code above in [c ode] [/c ode] but I can't work out how to show line numbers along the side...
    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.

  5. #5
    hopelessnoob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    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:
    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));
    		}
    	}
    }
    }
    Boxer.java:
    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);
    	}
    }
    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.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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 02:48 AM.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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)

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by ozzyman View Post
    which means that boxers should = 'null' rather than '0' to start off with.
    No. Primitives can never by null. Uninitialised variables are given their default values. For int/long etc it is 0. For double/float it is 0.0. For boolean it is false. For char it is a space. Reference variables (objects) it is null.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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:
    Java 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
    }
    for boxer1-only and something else but similar for boxer2

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    No. Primitives can never by null. Uninitialised variables are given their default values. For int/long etc it is 0. For double/float it is 0.0. For boolean it is false. For char it is a space. Reference variables (objects) it is null.
    Thanks for that Junky, I never knew.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    For char it is a space.
    No, it isn't. It's (char) 0, also known as the nul character (note: nul, not null).
    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 + ")");
      }
    }

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Ahh my bad. When I tested printing out the default value it looks like there is a space. But I guess the default value for char is unprintable.

Similar Threads

  1. Will this code work
    By rajat16 in forum XML
    Replies: 3
    Last Post: 09-24-2010, 12:52 PM
  2. I can't get my code to work?
    By ComicStix in forum New To Java
    Replies: 5
    Last Post: 09-19-2010, 04:15 PM
  3. Code does not work
    By scotts in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 10:37 AM
  4. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 03:57 AM
  5. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •