The Math program for applets
Here is the program in it's entirety:
- package com.blazinggames.jgd.chapter04;
-
- import java.applet.*;
- import java.awt.*;
-
- /**
- * Billy Spelchan's guide to Java Game Development
- * Chapter 4 sample applet
- * Copyright (c) 2003 Spelchan Software.
- * This source code is meant for educational purposes.
- * You may transfer and use this program for non-comercial purposes as long as
- * no fees are charged for this program.
- * The goal of this program is to demonstrate the structure of a Java applet.
- *
- * @author Billy D. Spelchan
- * @version 1.00.021118
- */
-
- public class Chapter04
- extends Applet
- {
- // *********************
- // ***** VARIABLES *****
- // *********************
-
- private int columnStart;
- private int rowStart;
- // ************************************
- // ***** METHODS - Parameter Aids *****
- // ************************************
-
- /**
- * Converts a string to a number, handling invalid numbers.
- *
- * @param s string to convert
- * @param n number to return if string is invalid
- */
- private int stringToInt(String s, int n)
- {
- int result = n;
-
- try
- {
- result = Integer.parseInt(s);
- }
- catch (NumberFormatException nfe)
- {
- }
-
- return result;
- }
-
- // *********************************
- // ***** METHODS - From Applet *****
- // *********************************
-
- /**
- * From Applet. Allows applet to initialize itself.
- * This is the applet equivelent of a main() method.
- */
- public void init()
- {
- // print copyright info to console
- System.out.println("Billy Spelchan's guide to Java Game Development");
- System.out.println("Chapter 4 sample applet");
- System.out.println("Copyright " + ((char)169) + " 2003 Spelchan Software.");
-
- // get parameters
- columnStart = stringToInt( getParameter("COLUMN_START"), 1);
- rowStart = stringToInt( getParameter("ROW_START"), 1);
- }
-
- /**
- * This gets called whenever the browser starts an applet.
- */
- public void start()
- {
- System.out.println("start()");
- }
-
- /**
- * This gets called whenever the browser stops an applet.
- */
- public void stop()
- {
- System.out.println("stop()");
- }
-
- /**
- * This gets called whenever the browser is going to destroy an applet.
- */
- public void destroy()
- {
- System.out.println("destroy()");
- }
-
- /**
- * Draws the applet, in this case a multiplication table.
- *
- * @param g the graphics context to draw in
- */
-
- public void paint(Graphics g)
- {
- int cntrX, cntrY;
- int math;
- String s;
-
- // draw grid
- g.setColor(Color.blue);
- for (cntrX = 0; cntrX < 12; ++cntrX)
- {
- g.drawLine(cntrX*50+45, 20, cntrX*50+45, 460);
- g.drawLine(45, cntrX*40+20, 595, cntrX*40+20);
- }
-
- // fill in multipication table
- FontMetrics fm = g.getFontMetrics();
- for (cntrX = 1; cntrX < 11; ++cntrX)
- {
- for (cntrY = 1; cntrY < 11; ++cntrY)
- {
- math = ((cntrX - 1) + columnStart) * ((cntrY - 1) + rowStart);
- s = "" + math;
- g.drawString(s, cntrX*50+95-fm.stringWidth(s), cntrY*40+50);
- }
- }
-
- // draw captions, in different color
- g.setColor(Color.red);
- for (cntrX = 1; cntrX < 11; ++cntrX)
- {
- math = ((cntrX - 1) + columnStart);
- s = "" + math;
- g.drawString(s, cntrX*50+95-fm.stringWidth(s), 50);
- }
-
- // these two loops could be combined, but separated for readablity
- for (cntrY = 1; cntrY < 11; ++cntrY)
- {
- math = ((cntrY - 1) + rowStart);
- s = "" + math;
- g.drawString(s, 95-fm.stringWidth(s), cntrY*40+50);
- }
- }
- }