© 2000-2003 A-Square, Inc. Cambridge, MA
All Exercises 3x use Logo and ASquareLogo, which you can find following the links.
This code has been purged from the comments that relate to the other 3x exercises.
The source for Banner3a with the comments retained can be found on Banner3-source.html
>
/*
* @Banner3b 2.0 030105
*
* © A-Square, Inc.
* 175 Richdale Ave, Cambridge MA 02140
*/
import java.awt.*;
import java.applet.Applet;
import java.util.Date;
import rartbase.*;
/**
* A simple Rart universe
* @version 2.0 030105
*
* @author Jan Aminoff
*/
final public class Banner3b extends Universe {
/*
* This source file is provided in sections corresponding to sections
* with the the same name and order of the Universe.java source file.
*/
// Section 1 Identifying the universe
// -------------------------------------------------------------------------
public String getName(){return "Banner3b, a Rart Universe";}
public String getDescription(){ return descr; }
public String descr=
"This universe is used to demonstrate the basic Rart framework \n"+
"in the Introduction to Java Tutorial. It should be compared \n"+
"to the Banner2a applet of the same tutorial. \n" +
"A uParameter now determines the number of Logos. \n"+
" "; // NEVER FORGET THE SEMICOLON!
public String getRartist(){ return "Jan Aminoff 2000";}
// -------------------------------------------------------------------------
// Section 2 What the universe does
// -------------------------------------------------------------------------
// Variables
private int maxv = 8; // Max speed of logo
private int size = 35; // Size of logo
// Following line is specific to Ex3b , where we have an array of Logos
private Logo logoarray[] = new Logo[10];
// init(), implementation of abstract method in Universe.
public void init(){
setParams(); //See Section 3
// We collect all reactions to changes in a reset method
reset();
}
private void reset(){
// Note: The two variables xm and ym are available in the parent class
// Universe. They give the size of the available window at all times.
// Following five lines are specific to Ex3b , where we have an array
// The max number of Logos are given by number.getMax()
int nn = number.getMax();
for (int i=0; i<nn ; i++){
int s = 10+RND(size);
logoarray[i] = new ASquareLogo(xm, ym, s);
logoarray[i].setMaxSpeed(maxv);
}
} // end of reset
// cycle(g), implementation of abstract method in Universe.
public void cycle(Graphics g){
// Compare with the update method of Banner2a!
// We reset the whole rectangle (0,0, xm, ym)
g.setColor(Color.lightGray);
g.fillRect(0, 0, xm, ym); // Paint the screen light gray
// we draw and move he current number of logos
for (int i=0; i< number.getCur(); i++){
logoarray[i].drawLogo(g);
logoarray[i].moveLogo();
}
String s1 = "Welcome to Introductory Java";
printString(g, s1, 50, 45, 20);
s1 = "Jan Aminoff, Instructor";
printString(g, s1, 120, 80 ,14);
s1 = "Also inventor of Rart, random Art for the Internet.";
printString(g, s1,xm-250,ym-5,10);
s1 = "Java Environment: "+
System.getProperty("java.vendor")+"..."+
System.getProperty("java.version");
printString(g, s1, 5, 15 ,12);
} // end of cycle(g)
// manageChange(uP), implementation of abstract method in Universe
public void manageChange(uParameter uP){
/*
* The Banner3b universe has three parameters, with
* index VIEWSIZE, CYCLETIME, and NUMBER respectively
* Section 3 gives the definitions. Here we have to program what to
* do if any of these parameters is modified.
*/
int type = uP.getIndex();
if (type == VIEWSIZE){ // If the size of the screen has changed.
reset(); // Just start all over
}else if (type == CYCLETIME){ // If the cycletime is modified
cycletime = uP; // Make sure cycletime has right value
}else if (type == NUMBER){
number = uP; // number has right value
reset(); // Just start all over.
}
} // end of manageChange(uP)
// getBackground(), overriding method in Universe
public Color getBackground(){return background;}
private Color background = Color.lightGray;
// -------------------------------------------------------------------------
// Section 3 uParameters as used in the universe
// -------------------------------------------------------------------------
// There are two uParameters defined here: cycletime defined almost always,
// and number which is specific to this universe.
private uParameter cycletime;
private uParameter number;
// The setParams() method is called only once, in init(), it is defined here
// in Section 3, so all parameter related stuff is together
private void setParams(){
// First cycletime
// Since "Cycle Time" is a compulsory parameter, the description is
// provided in Universe as CycleTimeDescr.
cycletime = new uParameter
(CYCLETIME,"Cycle Time", 10,50,100, CycleTimeDescr);
// we now have to add the parameter to the array of uParameters
// associated with this universe
adduParameter(cycletime);
// Second number
String ndescr = "In EX2b, EX2c, and EX2d double click \n"+
"to see this parameter change!";
number= new uParameter
(NUMBER, "Number of objects",0,5,10, ndescr);
adduParameter(number);
} // end of setParams()
// -------------------------------------------------------------------------
// Section 4 Utility Methods
// -------------------------------------------------------------------------
// All provided in Universe, include int RND(int x), printString, etc
// Section 5 Useful Variables
// -------------------------------------------------------------------------
// Provided in Universe, include xm, ym, for width and height of window and
// nc for number of frames.
// Section 6 Security related methods
// Provided in Universe, except for the following constructor.
public Banner3b()throws InstantiationException
{
// Empty statement. This will activate the constructor of Universe
// which only allows the activation of one universe at a time in
// order to preclude bogging down the system by mistake or intent.
}
} //end of definition of Banner3b
>