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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Lecture 2: Object Oriented Programming I
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
UML Basics & Access Modifier
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Java exercise review Lesson 25: Exercise 1, 2 and 3.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Object Oriented Programming Lecture 5: BallWorld.
Session 15 Chapter 8: Understanding Inheritance. Lab Exercise Solution Recall that we needed to know if a mousePress occurred on a target. We tried to.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
© A+ Computer Science - Chicken yeller = new Chicken();
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Intro to Applets August 19, 2008 Mrs. C. Furman. Java Applets vs. Java Applications Java Applet: a program that is intended for use on the web. Java Applet:
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
Even-Driven Programming and basic Graphical User Interface.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Session 22 Chapter 11: Implications of Inheritance.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Lesson 33: Layout management and drawing – Java GUI.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Block 1 Unit 2 Basic Constructs in Java. Objectives create a simple object using a constructor; create and display a window frame on your computer screen;
Introducing, the JFrame Gives us a work area beside System.out.println.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Session 8 Lab 4 comments, MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A structured walkthrough
Introduction to Graphics
Overloading and Constructors
Interface.
Simple Classes in Java CSCI 392 Classes – Part 1.
Chapter 10: Mechanisms for Software Reuse
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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

The Ball class file and compilation Compiling the file Recap

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

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

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

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

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

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

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

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

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

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

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

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.