FileDocCategorySizeDatePackage
Bicycle.javaAPI DocExample2737Thu Feb 22 16:09:40 GMT 2001None

Bicycle

public class Bicycle extends Applet implements ActionListener

Fields Summary
public Button
Pedal
public Button
GearUp
public Button
GearDown
int
pos
Bike
MyBike
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent ae)

    if(ae.getSource()==Pedal)
      MyBike.pedal();

    if(ae.getSource()==GearUp)
      MyBike.gear++; //agh more tweaking with internal data gearUp();

    if(ae.getSource()==GearDown)
      MyBike.gear--;  //horrible, what happens if I get gear -1!?
  
public voidinit()

 //make a bike object

  //Initialize the applet
    
  //set up the interface
    resize(600,60);
    add(Pedal);
    add(GearUp);
    add(GearDown);
    Pedal.addActionListener(this);
    GearUp.addActionListener(this);
    GearDown.addActionListener(this);
  
public voidpaint(java.awt.Graphics g)

    //let's make the bike move
    //first get its position
    MyBike.processBike(); //update the bike
    pos = MyBike.position/10;  //yuk, direct access to member data
    //finally let's draw the bike       ok it's doesn't look great, so make it better

    g.fillOval(pos,50,10,10);
    g.fillOval(pos+25,50,10,10);
    g.drawLine(pos+5,55,pos+30,55);
    g.drawLine(pos+30,55,pos+22,35);
     showStatus("xBike pos = "+MyBike.position+" Gear = "+MyBike.gear+" speed = " +MyBike.speed);
   repaint();