/* * BookExFrame.java * * Created on August 26, 2002, 1:31 PM * Copied from Peter van der Linden's Just Java book, modified by * Michael A. Redmond * Purpose: Simple Java example, with some GUI * * Example is not very OO * Over commented for teaching purposes */ package simpleEx; // group of classes containing simple examples import java.awt.*; // GUI libraries // By importing we can use names from the library with // whole name e.g. java.awt.Frame can be referred to as // just Frame // By importing awt.* we import the whole awt library /** * * @author redmond * Class definition starts here * this BookExFrame "isa" Frame - it is a subclass of Frame * - it inherits from the class Frame * Frame is in AWT - we are creating a specialized frame */ public class BookExFrame extends Frame { ///////////////////////////////////////////////////////////////// // static instance variables exist once for the class // (sometimes called class variables) // This exists in C++ too // We then refer to these using the class name, rather than via an // object of the class ///////////////////////////////////////////////////////////////// // coordinates are position that words will be drawn within a window private static int xcoord = 0; private static int ycoord = 120; // used to loop through different colors and words private static int indx = 0; // indicates whether words are moving horizontally (vs vertically) private static boolean horizScroll = true; ///////////////////////////////////////////////////////////////// // instance variables - exist once for each object in the class // (C++ call data members) ///////////////////////////////////////////////////////////////// // declare and create an instance of a Font // Font is part of AWT // This is different from C++ // fontToUse is actually a reference to a Font variable, the new Font // runs a constructor, creating the font object, and then fontToUse // essentially has the address of that object - as if it were a pointer // but without any pointer notation. This is what I mean when I say // Java hides pointers _a_little_bit_ private Font fontToUse = new Font("TimesRoman", Font.BOLD, 44); // declare an array of strings and initialize to values // don't have to declare size of array in Java private String msg[] = {"Java", "Portable", "Popular", "Challenging", "Enriching"}; // declare an array of Colors and initialize to values // Color is part of AWT, and colors blue, yellow etc are part of AWT // (actually part of the color class of the AWT) class.member - these // are static! // this is the same as below - private Color color[] = new Color[] {Color.blue, Color.yellow, Color.green, Color.red, private Color color[] = {Color.blue, Color.yellow, Color.green, Color.red, Color.orange }; /** Creates a new instance of BookExFrame */ // nothing to do in this example public BookExFrame() { } // gets called by runtime library when repaint is called /** * paint the window * overrides paint in Frame class - replaces it * Given: a graphics object (Graphics is part of AWT) */ public void paint(Graphics g) { g.setFont( fontToUse); g.setColor(color[indx] ); // use current color g.drawString(msg[indx],xcoord,ycoord); // use current message, // put in current position } // main is where execution of an application starts // - signature is pretty standard - a lot of people will name the // param args - this param holds command line args if called from // a command line // main is void instead of C++ usually returns an int // in Java, every class can have a main - a little OO impurity // Useful for testing classes though static public void main (String s[]) throws Exception { // declare and create a BookExFrame which is a specialization // of frame BookExFrame myFrame = new BookExFrame(); //BookExFrame myBunch[] = { new BookExFrame(), new BookExFrame(), // new BookExFrame(), new BookExFrame() }; BookExFrame myBunch[] = new BookExFrame[4]; for (int indx = 0; indx < myBunch.length; indx++) { myBunch[indx] = new BookExFrame(); } // use method defined under Frame and inherited here myFrame.setSize(300,250); // width, height int pixelsPerLine = 300; int totalLines = 5; myFrame.setVisible(true); // show frame // mess with other frames myBunch[0].setSize(300,250); myBunch[1].setSize(300,250); myBunch[2].setSize(300,250); myBunch[3].setSize(300,250); myBunch[0].setVisible(true); myBunch[1].setVisible(true); myBunch[2].setVisible(true); myBunch[3].setVisible(true); myFrame.setLocation(50,50); myBunch[0].setLocation(700,50); myBunch[1].setLocation(50,500); myBunch[2].setLocation(700,500); myBunch[3].setLocation(375,275); // loop through all possible positions for (int cnt = 0; cnt < pixelsPerLine*totalLines; cnt++) { Thread.sleep(25); // slow down to make more visible myFrame.repaint(); // re-display contents of frame myBunch[0].repaint(); myBunch[1].repaint(); myBunch[2].repaint(); myBunch[3].repaint(); // see if scrolling horizontally or vertically if (horizScroll) { // scrolling horizontally // prepare for next position - over three places xcoord += 3; // see if reached the end of the line if (xcoord >= 300) { // at end of line // move to next message and color (implicitly, // by changing index into those arrays indx++; indx = indx % 5; // make sure we don't go past end of arrays // preparing for vertical scrolling - make vertical 0, // make horizontal 50 so not pinned to the edge xcoord = 50; ycoord = 0; horizScroll = false; // move message down next time } } else { // scrolling vertically // prepare for next position - down three places ycoord += 3; // see if reached the bottom of the frame if (ycoord >= 250) { // at bottom of the frame // move to next message and color (implicitly, // by changing index into those arrays indx++; indx = indx % 5; // make sure we don't go past end of arrays // preparing for horizontal scrolling - make // horizontal 0 - far left, // make vertical 120 so not pinned to either edge xcoord = 0; ycoord = 120; horizScroll = true; // move message left to right next time } } } // end for loop System.exit(0); } // end main } // end whole class