Presentation is loading. Please wait.

Presentation is loading. Please wait.

BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.

Similar presentations


Presentation on theme: "BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared."— Presentation transcript:

1 BallWorld.java A structured walkthrough

2 Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared as static void and public All main programs must take arguments as a string values (ignored in this case). The class defines some private internal varibales, some of which are constant, some are initialized and some that are not initialized. The main creates an instance of the class BallWorld. This object is initialized through the constructor that created it. The class is deceared as an extention of an existing java class called “frame”. This is built through inheritance. The output is displayed through the use of primitives provided by the Java runtime library.

3

4

5 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Import from Java Library Extention of the existing frame class The main section, that use the contructor to create an instance of Ballworld and passes a message to the show() behaviour.

6 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } The variable declerations (note the final and the private designations) “Static” means that there are only one instance of the data field shared by all instances of the class “final” means that this is the last time that the object is changed. “private” means that these variables can only be used within this class. (others can use the same names in other classes for other purposes.)

7 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } This variable in created as an instance of the ball class. It is a circle that can move around the display surface.

8 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Creates an instance of the class “Ballworld” (window) 1.Constructors are similar to functions, however they do not have a return type. 2.The name of the function should match the name of the class in which it appears. 3.Users never execute a constructor (directly) Constructors Creates an instance of the ball and the behaviors of the ball

9 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Import from Java Library Sets the size of the window Gives a name to this instance of the window ( frame )

10 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Creates a new ball 1.This is an instance of the class ball 2.Memory is allocated for this object 3.Arguments are matched by a constructor in the class ball which is used to initialize the new ball.

11 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Sets ball color and motion paramenters (direction)

12 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Import from Java Library but our class is an extention of the frame provided by Java. And we will use it for a specific purpose. Fuctions given to us by the Java provided frame Pixels

13 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } “awt” stands for the Abstract Windowing Toolkit. Which gives us access to a large portion of code we do not have to write. Fuctions given to us by the Java provided frame

14 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } The show method invokes a function called paint

15 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } The counter determins the number or repaint we will do before the program halts. Java system command

16 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } Drawing the ball and moving it Check if the ball has reached the frame. If so, it will move in opposite direction

17 import java.awt.*; import java.awt.event.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0; private BallWorld (Color ballColor) { // constructor for new ball world setSize (FrameWidth, FrameHeight); // resize our frame setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); }


Download ppt "BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared."

Similar presentations


Ads by Google