BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
Java exercise review Lesson 25: Exercise 1, 2 and 3.
Object Oriented Programming Lecture 5: BallWorld.
School of Computer Science & Information Technology G6DICP - Lecture 17 GUI (Graphical User Interface) Programming.
© A+ Computer Science - Chicken yeller = new Chicken();
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Even-Driven Programming and basic Graphical User Interface.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
© 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.
Inheritance A Review of Objects, Classes, and Subclasses.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Swing One of the most important features of Java is its ability to draw graphics.
Creating a Java Application and Applet
© 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.
Lesson 28: More on the GUI button, frame and actions.
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.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 1 CS100A, Lecture 7, 22 Sept Static fields and methods. Suppose that we would like to main-
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
A structured walkthrough
Introduction to Graphics
Topic: Classes and Objects
A First Look at GUI Applications
CS100A, Lecture 7, 22 Sept Static fields and methods
Interface.
Constructors, GUI’s(Using Swing) and ActionListner
A+ Computer Science METHODS.
Expanding the PinBallGame
Presentation 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() 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); } 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 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() 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); } The variable declerations (note the final and the private designations) “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() 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); } 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.*; 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() 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); } Creates an instance of the class “Ballworld” (window) 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) Constructors Creates an instance of the ball and the behaviors of the 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() 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); } Import from Java Library 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() 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); } 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() 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); } 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 (); } 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() 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); } 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. Fuctions given to us by the Java provided frame Pixels

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() 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); } “awt” stands for the Abstract Windowing Toolkit. Which gives us access to a large portion of code we do not have to write. Fuctions given to us by the Java provided 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() 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); } The show method invokes a function called paint

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() 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); } 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() 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); } 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() 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); }