Presentation is loading. Please wait.

Presentation is loading. Please wait.

BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.

Similar presentations


Presentation on theme: "BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects."— Presentation transcript:

1 BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects

2 The BallWorld program Two classes are created The BallWorld features (from the previous lectures) : 1.Execution is done through the procedure called “main” which are declared as static, void and public 2. All main programs must take arguments as a string values (ignored in this case). 3.The class defines some private internal variables, some of which are constant, some are initialized and some that are not initialized. 4.The main creates an instance of the class BallWorld. This object is initialized through the constructor that created it. 5.The class is declared as an extension of an existing java class called “frame”. This is built through inheritance. We also use the classes “graphics” and “rectangle” provided by the java library and the jave run-time library, respectivly. 6.The output is displayed through the use of primitives provided by the Java runtime library. Recap

3 The Ball class file and compilation Compiling the file Recap

4 This class cannot be executed. It has no main section. However it is a public class that can be used in other programs (such as our BallWorld) Hint: Always think “what is the instigator of this program”, as well as “what type of program is this (applet vs. application) The bytecode file Recap

5 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

6 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

7 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

8 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

9 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

10 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

11 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

12 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

13 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

14 import java.awt.*; public class Ball { protected Rectangle location; protected double dx, dy; protected Color color; public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; } public void setColor (Color newColor) { color = newColor; } public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; } public int radius () { return location.width / 2; } public int x () { return location.x + radius(); } public int y () { return location.y + radius(); } public double xMotion (){ return dx; } public double yMotion (){ return dy; } public Rectangle region () { return location; } public void moveTo (int x, int y) / { location.setLocation(x, y); } public void move () { location.translate ((int) dx, (int) dy); } public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }} How is BallWorld using the Ball class? 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) { setSize (FrameWidth, FrameHeight); 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); } BallWorld

15 Next we will look at MultiBall World To do: Study the presentations on Ball and BallWorld. They are integral parts of what you will be working on for the next couple of weeks.


Download ppt "BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects."

Similar presentations


Ads by Google