ballworld.java ball.java a functional walkthrough part 3: the interaction of objects

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

Upload: deirdre-jennings

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

BallWorld.javaBall.java

A functional walkthrough

Part 3: the interaction of objects

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

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

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

The Ball class file and compilation

Compiling the file

Recap

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

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

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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 fieldaBall = new Ball (10, 15, 5);aBall.setColor (ballColor);aBall.setMotion (3.0, 6.0);}

public void paint (Graphics g) {// first, draw the ballaBall.paint (g);// then move it slightlyaBall.move();if ((aBall.x() < 0) || (aBall.x() > FrameWidth))aBall.setMotion (-aBall.xMotion(), aBall.yMotion());if ((aBall.y() < 0) || (aBall.y() > FrameHeight))aBall.setMotion (aBall.xMotion(), -aBall.yMotion());// finally, redraw the framecounter = counter + 1;if (counter < 2000) repaint();else System.exit(0);}}

BallWorld

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

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.