Are Uppman
Home>bowling>applet>code applet
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

/** 
    BowlingGameApplet presents a GUI which lets you edit the number of 
    pins knocked down by each throw in a bowlinggame, and then shows
    you the corresponding scores.
    The applet consists of a text editor for the sequence of pins, a 
    bowlingPanel for the scores of the corresponding frames, and an 
    errorLabel for error messages.
    The bowlingPanel is an instance of the BowlingPanel class.
 */
public class BowlingGameApplet extends Applet {
  TextField editor = new TextField();
  Label label = new Label("Edit throws");
  BowlingPanel bowlingPanel = new BowlingPanel();
  Label errorLabel = new Label();
  public void init() {
    setLayout(new BorderLayout());
    Panel panel = new Panel();
    panel.setLayout(new GridLayout(2, 1));
    panel.add(label);
    panel.add(editor);
    add(panel, BorderLayout.NORTH);
    add(bowlingPanel, BorderLayout.CENTER);
    add(errorLabel, BorderLayout.SOUTH);
    editor.addTextListener(new TextListener() {
      public void textValueChanged(TextEvent te) {
        bowlingPanel.reset();
        StringTokenizer st = new StringTokenizer(editor.getText());
        parse : while (st.hasMoreTokens()) {
          try {
            bowlingPanel.add(Integer.parseInt(st.nextToken()));
            errorLabel.setText("");
          } catch (Throwable ex) {
            errorLabel.setText("Error: "+ex.getMessage());
            break parse;
          }
        }
        bowlingPanel.repaint();
      }
    } );
  }

/** 
    BowlingPanel is dedicated to the drawing of all the rectangles and the 
    scores of the game. This is graphics without much interrest for the
    programmer, because the calculations are delegated to an instance of 
    the BowlingGame class. 
 */
  class BowlingPanel extends Canvas {
    BowlingGame bg = new BowlingGame();
    int score;
    public BowlingPanel() {
      setBackground(Color.yellow);
    }
    public void reset() {
      bg = new BowlingGame();
    }
    public void add(int pins) throws Exception {
      bg.add(pins);
    }
    public void paint(Graphics g) {
      g.setColor(Color.blue);
      int l = getSize().width/11;
      int h = getSize().height;
      for (int i = 1; i <= 10; i++) {
        paintFrame(g, i, (i-1)*l, l, h);
      }
      paintExtra(g, 10*l, l, h);
    }
    void paintFrame(Graphics g, int i, int x, int l, int h) {
      drawFirstRect(g, i, x, 0, l/2+1, h/3);
      drawSeconRect(g, i, x+1+l/2, 0, l/2, h/3);
      drawGreatRect(g, i, x, h/3, l, h-h/3-1);
    }
    void drawFirstRect(Graphics g, int i, int x, int y, int l, int h) {
      g.drawRect(x, y, l, h);
      if (isNotStrike(i)) {
        g.drawString("" + bg.throwForFrame(i, 1), x+l/3, y+h*4/5);
      }
    }
    void drawSeconRect(Graphics g, int i, int x, int y, int l, int h) {
      g.drawRect(x, y, l, h);
      if (isStrike(i)) {
        g.drawLine(x, y, x+l, y+h);
        g.drawLine(x, h, x+l, y);
      } else if (isSpare(i)) {
        g.drawLine(x, y+h, x+l, y);
      } else if (isNotStrikeNorSpare(i)) {
        g.drawString("" + bg.throwForFrame(i, 2), x+l/3, y+h*4/5);
      }
    }
    void drawGreatRect(Graphics g, int i, int x, int y, int l, int h) {
      g.drawRect(x, y, l, h);
      if (isReady(i)) {
        g.drawString("" + bg.cumulatedScore(i), x+l/3, y+h*3/4);
      }
    }
    void paintExtra(Graphics g, int x, int l, int h) {
      int y = 0;
      if (isStrike(10)) {
        g.drawRect(x, y, l/2, h/3);
        g.drawRect(x+l/2, y, l/2, h/3);
        g.drawString("Extra", x+l/6, y+h*6/10);
        g.drawString("throws", x+l/6, y+h*9/10);
        if (bg.frameState(10) > 1) {
          g.drawString("" + bg.throwForFrame(10, 2), x+l/2/3, y+h/3*4/5);
        }
        if (bg.frameState(10) == 3) {
          g.drawString("" + bg.throwForFrame(10, 3), x+l/2+l/2/3, y+h/3*4/5);
        }
      } else if (isSpare(10)) {
        g.drawRect(x, y, l/2, h/3);
        g.drawString("Extra", x+l/6, y+h*6/10);
        g.drawString("throw", x+l/6, y+h*9/10);
        if (bg.frameState(10) == 6) {
          g.drawString("" + bg.throwForFrame(10, 3), x+l/2/3, y+h/3*4/5);
        }
      }
    }
    boolean isStrike(int i) {
      int s = bg.frameState(i);
      return (s >= 1) && (s <= 3);
    }
    boolean isNotStrike(int i) {
      int s = bg.frameState(i);
      return s >= 4;
    }
    boolean isSpare(int i) {
      int s = bg.frameState(i);
      return (s >= 5) && (s <= 6);
    }
    boolean isNotStrikeNorSpare(int i) {
      int s = bg.frameState(i);
      return (s == 7);
    }
    boolean isReady(int i) {
      int s = bg.frameState(i);
      return (s == 3) || (s == 6) || (s == 7);
    }
  }
}
Site updated 08 May, 2017    Remarks and questions? areusite at free dot fr