Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.

Slides:



Advertisements
Similar presentations
Web Design & Development Lecture 19. Java Graphics 2.
Advertisements

1 CS1001 Lecture Overview Java Programming Java Programming.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
A Simple Applet.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Programming Task: Task 1 Controlled Assessment Practice.
Io package as Java’s basic I/O system continue’d.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Layout Managers Arranges and lays out the GUI components on a container.
Data Types, Variables, and Arithmetic JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
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.
Applets Yong Choi School of Business CSU, Bakersfield.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Inheritance A Review of Objects, Classes, and Subclasses.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Creating a Java Application and Applet
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.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Java Classes, Objects, and Events: A Preview
A First Look at GUI Applications
Lecture 09 Applets.
“Form Ever Follows Function” Louis Henri Sullivan
Ellen Walker Hiram College
Java Methods Objects and Classes Start on Slide 30 for day 2
Java Methods Objects and Classes Start on Slide 30 for day 2
Presentation transcript:

Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM

4-2 Objectives: l Get an introduction to classes, objects, fields, constructors, and methods; get a general idea of how a small program is put together l Explore how library classes are used in Java programs l Get a feel for how methods call each other; learn about private and public methods l Learn a little about event-driven applications and the event-handling mechanism in Java

4-3 Objects in the Ramblecs Applet Ramblecs, the applet itself LetterPanel whiteboard FallingCube cube JButton go

4-4 Classes and Source Files l A class defines a class of objects. Class name:File name: SomeClass Ramblecs FallingCube SomeClass.java Ramblecs.java FallingCube.java Convention: a class name starts with a capital letter Same upper / lower case letters

4-5 Programmers write classes l And extensively use library classes –either directly: JButton go = new JButton("Click here"); –or through inheritance: public class LetterPanel extends JPanel

4-6 Classes in the Ramblecs Applet Written by us From the library package javax.swing

4-7 Files and Folders javac automatically looks for classes (.java or.class files) in the current folder, or, if classpath is set, in folders listed in the classpath string. l A missing file may be reported as a syntax error when compiling another file. If you set classpath, include the current folder. It is denoted by a dot. For example:.; C:\javamethods\EasyIO l IDE helps take care of the file locations.

4-8 Libraries l Java programs are usually not written from scratch. l There are hundreds of library classes for all occasions. l Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — windowing and graphics toolkit javax.swing — newer GUI package Swing

4-9 import l Full library class names include the package name. For example: java.awt.Color javax.swing.JButton import statements at the top of your program let you refer to library classes by their short names: import javax.swing. JButton ;... JButton go = new JButton ("Click here"); Fully-qualified name

4-10 import (cont’d) You can import names for all the classes in a package by using a wildcard. * : import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes. Imports all classes from awt, awt.event, and swing packages

4-11 public class SomeClass { l Fields l Constructors l Methods } private: visible only inside this class public: visible in other classes Attributes / variables that define the object’s state. Can hold numbers, characters, strings, other objects. Usually private. Code for constructing a new object and initializing its fields. Usually public. Actions that an object can take. Can be public or private.

4-12 public class FallingCube { private final int cubeSize; private int cubeX, cubeY; // Cube coordinates... private char randomLetter; // Cube letter public FallingCube(int size) { cubeSize = size;... } public void start() { cubeX = 0; cubeY = -cubeSize;... }... } Fields Constructor Methods The name of a constructor is always the same as the name of the class.

4-13 private (or public) [static] [final] datatype name ; Fields Usually private May be present: means the field is shared by all objects in the class May be present: means the field is a constant int, double, etc., or an object: String, JButton, FallingCube, Timer You name it!

4-14 Fields (cont’d) l May have primitive data types: int, char, double, etc. private int cubeX, cubeY; // cube coordinates... private char randomLetter; // cube letter

4-15 Fields (cont’d) l May be objects of different types: private FallingCube cube ; private Timer t ; private static final String letters ;

4-16 Constructors l Constructors are like methods for creating objects of a class. l Most constructors initialize the object’s fields. l Constructors may take parameters. l A class may have several constructors that differ in the number or types of their parameters. l All of a class’s constructors have the same name as the class.

4-17 Constructors (cont’d) go = new JButton("Go");

4-18 Constructors (cont’d) l Call them using the new operator: cube = new FallingCube (CUBESIZE);... t = new Timer (delay, this) Calls FallingCube ’s constructor with CUBESIZE as the parameter Calls Timer ’s constructor with delay and this (i.e. this object) as the parameters (see Java docs for javax.swing.Timer )

4-19 Methods l Call them for a particular object: cube.start(); whiteboard.dropCube(); randomLetter = letters.charAt(i); l But call static (“class”) methods for the whole class, not a specific object: y = Math.sqrt (x);

4-20 Methods (cont’d) l Constructors and methods can call other public and private methods of the same class. l Constructors and methods can call only public methods of another class. Class X private method Class Y public method

4-21 Methods (cont’d) l You can call methods with specific arguments: g.drawRect (75, 25, 150, 50); g.drawString ("Welcome", 120, 50); l The number and types of arguments must match the method’s parameters: public void drawRect ( int x, int y, int width, int height ) {...} public void drawString ( String msg, int x, int y ) {...}

4-22 Events l Can originate in the real world (mouse clicked, keyboard key pressed, cable gets connected, etc.) l Can come from the operating system (window resized or closed, message received, etc.) l Can originate in your program (a timer fires, a panel needs to be repainted, etc.)

4-23 Events (cont’d) l An object that generates events may have one or several listeners attached to it. l A listener is an object. l A listener’s method is called for each event. ActionListener object (ActionEvent e) { whiteboard.dropCube(); } public void actionPerformed Click!

4-24 Events (cont’d) public class Ramblecs extends JApplet implements ActionListener {... private JButton go; public void init() { go = new JButton("Go"); go.addActionListener(this);... } public void actionPerformed(AcionEvent e) { whiteboard.dropCube(); } Add a listener to the button. In this case, the listener object is the applet itself. Describes the details of this event. Not used here.

4-25 Ramblecs Events Ramblecs class applet object creates the whiteboard panel and the “Go” button calls whiteboard’s dropCube Applet starts init method actionPerformed method  “Go” clicked

4-26 Ramblecs Events (cont’d) LetterPanel class whiteboard object starts the timer and the cube moves the cube down; generates a repaint request restores the background; calls cube’s draw method Repaint request dropCube method actionPerformed method  Timer fires paintComponent method

4-27 Ramblecs Events (cont’d) FallingCube class cube object picks a random letter; resets cube’s position to the top checks whether cube reached the bottom; moves the cube down draws the cube start method moveDown method draw method

4-28 Review: l How many classes did we write for Ramblecs? l Name a few library classes that we used. l What are import statements used for? l What is a field? A constructor? A method? l Which operator is used to construct an object? l What is the difference between private and public methods? l Why are fields usually private?

4-29 Review (cont’d): l Define an event-driven application. l Why are GUI applications event-driven? l Is an event listener a class, an object, or a method? l How many action listeners are used in Ramblecs? l What does the following statement do? w.addWindowListener (new ExitButtonListener ( ) );