Download Lecture_20_the_Ballworld

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
BallWorld.java
A structured walkthrough
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.
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() < 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 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.
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
The variable declerations
(note the final and the
private designations)
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
“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.)
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() < 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 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.
import java.awt.*;
import java.awt.event.*;
Constructors
public class BallWorld extends Frame {
public static void main (String [ ] args)
{
BallWorld world = new BallWorld (Color.red);
world.show ();
}
Creates an instance of the
class “Ballworld” (window)
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
Creates an instance of the ball and
the behaviors of the ball
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)
import java.awt.*;
import java.awt.event.*;
public class BallWorld extends Frame {
Import from Java Library
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
Sets the size of the window
Gives a name to this instance of the
window (frame)
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() < 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 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.
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
Sets ball color and motion
paramenters (direction)
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 ();
}
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.
private static final int FrameWidth = 600;
private static final int FrameHeight = 400;
private Ball aBall;
private int counter = 0;
Pixels
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
Fuctions given to us by the
Java provided frame
import java.awt.*;
import java.awt.event.*;
“awt” stands for the Abstract
Windowing Toolkit. Which gives us
access to a large portion of code
we do not have to write.
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;
Fuctions given to us by the
Java provided frame
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
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;
The show method invokes
a function called paint
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
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() < 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 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
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() < 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 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
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() < 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 frame
counter = counter + 1;
if (counter < 2000) repaint();
else System.exit(0);
}
}
Related documents