Overclock.net banner
1 - 2 of 2 Posts

·
Premium Member
Joined
·
11,025 Posts
Discussion Starter · #1 ·
I wrote this code for this basketball man awhile back. It is just a simple guy bouncing a ball that walks across the screen. Any java experts out there know how to shorten this code a little more? Also was curious at how much more detail can be put into the guy and the ball. Thanks for looking.

crim

Code:

Code:
import java.applet.*;
import java.awt.*;
public class Basketball extends Applet
{

private int i = 20;
private int LIMIT = 300;
private final int SLEEP_TIME = 10;

    public void init()
 {
   setBackground(Color.WHITE);
 }

 public void paint (Graphics g)
 {

 //for(i=20;i<300;i++)
 {                        
 g.setColor(Color.BLACK);
 g.drawOval(i+10,20,90,90); //head
 g.drawOval(i+80,40,10,10); //eye
 g.drawLine(i+100,60,i+106,65); //nose (horizontal start, vertical start, horizontal end, vertical end)
 g.drawLine(i+100,70,i+106,65); //nose
 g.drawArc(i+55,40,85,50,225,40);//mouth (start h,start  v,end h,end  v,angle of arc, length sides)
 g.setColor(Color.BLUE);
 g.drawLine(i+54, 110, i+54, 165); //body
 g.drawLine(i+55, 110, i+55, 165); //body
 g.drawLine(i+56, 110, i+56, 165); //body
 g.drawLine(i+57, 110, i+57, 165); //body
 g.drawLine(i+58, 110, i+58, 165); //body
 g.setColor(Color.BLACK);
 g.drawLine(i+55, 130, i+85, 135); //arm
 g.drawLine(i+55, 160, i+55 ,211); //back leg
 g.drawLine(i+55, 160, i+60 ,211); //front leg
 g.setColor(Color.ORANGE);
 g.drawOval(i+80,130,40,40); //ball
 g.setColor(Color.BLACK);
 g.drawLine(0, 211, 800, 211); //road
 g.drawLine(0, 212, 800, 212); //road
 g.drawLine(0, 213, 800, 213); //road
 g.drawLine(0, 214, 800, 214); //road
 g.setColor(Color.WHITE);
 for(int j=0;j<100000000;j++);

 g.drawLine(i+55, 130, i+85, 135); //arm
 g.drawLine(i+55, 160, i+55 ,211);//back leg 
 g.drawLine(i+55, 160, i+60 ,211); //front leg
 g.drawOval(i+80,130,40,40); //ball
       g.setColor(Color.WHITE);

 g.setColor(Color.BLACK);
 g.drawLine(i+55, 130, i+85, 150); //arm
 g.drawLine(i+55, 160, i+45 ,211); //back leg
 g.drawLine(i+55, 160, i+80 ,211); //front leg
 g.setColor(Color.ORANGE);
 g.drawOval(i+80,170,40,40); //ball
 g.setColor(Color.WHITE);
 for(int j=0;j<100000000;j++);

 g.drawLine(i+55, 130, i+85, 150); //arm
 g.drawLine(i+55, 160, i+45 ,211); //back leg
       g.drawLine(i+55, 160, i+80 ,211); //front leg
 g.drawOval(i+80,170,40,40); //ball
 g.setColor(Color.WHITE);

 i++;

    try
 {
  Thread.sleep(SLEEP_TIME);
 }
 catch(InterruptedException e)
 {
 }
      if(i < LIMIT)
repaint();
}
}

}
 

·
Premium Member
Joined
·
5,282 Posts
There's probably not a whole lot you can do to shorten the code, but it would be better if broken up into multiple methods (and perhaps multiple classes). Instead of a block of function calls, wrap them in drawPerson (which would call drawHead, drawBody etc) and drawBall methods. The code will look much cleaner and the paint method will be shorter.

Also, counting to 100M in a loop is a bad way to sleep. Just use sleep
 
1 - 2 of 2 Posts
Top