Presentation is loading. Please wait.

Presentation is loading. Please wait.

A structured walkthrough

Similar presentations


Presentation on theme: "A structured walkthrough"— Presentation transcript:

1 A structured walkthrough
BallWorld.java A structured walkthrough -part 2 the ball class

2 The BallWorld program Recap
Two classes are created (last time we looked at the frame and some behaviors, now we will look at the ball class we used in the program. It is located in a separate file called Ball.java) The BallWorld features (from the previous lecture) : Execution is done through the procedure called “main” which are declared 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 variables, 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 declared as an extension 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. Recap

3 Recap

4 The BallWorld class file and compilation
Recap

5 The Ball class file and compilation
Compiling the file

6 The bytecode file 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)

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) // functions that change attributes of ball { 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); }} “awt” stands for the Abstract Windowing Toolkit. Which gives us access to a large portion of code we do not have to write. Behaviors of ball should be (and are) written in generalities so it can be used in many ways) This is a stored in a separate file so that together with “BallWorld” it can be executed.

8 The ball has four data fields that are “protected”
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) // functions that change attributes of ball { 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); }} The ball has four data fields that are “protected” “Rectangle” is representing the location of the ball. It is also a general purpose class provided by the Java run-time library “dx” and “dy” are horizontal and vertical direction of the movement of the ball. “Color” is a Java library class (not “run-time”). “protected” fields allows child classes to have access to the fields without allowing other objects to modify them. In general some developers prefer “protected” instead of “private”

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) // functions that change attributes of ball { 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); }} Three arguments are needed to call the public class “Ball”. They all need to be integers. The first two “x” and “y” are the initial location of the ball, while “r” is the radius of the ball. The constructor is made by recording the location of the new ball (represented by the rectangle).

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) // functions that change attributes of ball { 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); }} A simple calculation is used to determine the four corners of the rectangle, since this Java general run-time class requires four arguments (one for each corner in pixels). Default values are set for motion (dx and dy) and color. These values can be modified by an object through messages (as we saw in setColor and setMotion in the BallWorld class) The notation “Color.blue” is used since Color is a Java provided library class and “blue” is a member of this class.

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) // functions that change attributes of ball { 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); }} This allows users of the class to pass a message to this object to change the color through the call “setColor”. We used this in BallWorld.java:

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) // functions that change attributes of ball { 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); }} This allows users of the class to pass a message to this object to change the motion through the call “setMotion”. We used this in BallWorld.java:

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) // functions that change attributes of ball { 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); }} Behaviors that return the radius, the x and y location (in pixels), the dx and dy directional movement of the ball and the region occupied by the ball to anyone accessing the ball class Functions such as these that allows access to the data in a class is called “accessor functions”

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) // functions that change attributes of ball { 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); }} “accessor functions” are preferred instead of making these data fields public. This is due to the fact that accessor functions only gives read-only access to these values and thereby protect them from updates. However the use of the functions above can modify the ball (setMotion and setColor)

15 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) // functions that change attributes of ball { 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); }} Functions of the run-time Java class “Rectangle”. It provides the width and the “anchor” of the upper corner in terms of x,y coordinates Function of the run-time Java class “Rectangle” that allows you to move the rectangle to another location in terms of pixels (as we represents as allowable arguments “x” and “y” that must be integers when we use this function).

16 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); }} Function of the run-time Java class “Rectangle” that allows you to “transliterate” the rectangle from one location to the next. The movement-to location is represented by the dx and the dy variables. The function “paint” use two operators provided by the Java library class “Graphics”: Setting color for the graphics to be drawn Painting an oval at a given location (need 4 parameters) We can print an oval anywhere by this simple code: Public void paint (Graphics xyz){ xyz.fillOval(1,1,10,10);} And call it through: paint(xzy);

17 How is BallWorld using the Ball class?
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?

18 Some observations: The following classes are extensively used in Ball and BallWorld: The Frame class (the window) The run-time class Rectangle The Graphics class (the display) The only way to use these classes are through knowing what operators can be used with them. We have used some of the most common, but each time you develop a system, you will frequently need to consult the object library to see what is available to you and how you can use the classes provided by Java as well as those already created by vendors such as Microsoft or the company you work for.

19 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 "A structured walkthrough"

Similar presentations


Ads by Google