agenda –interfaces and realization –type hierarchy –introduction to graphics and event...

29
Agenda interfaces and realization type hierarchy introduction to graphics and event handling

Post on 19-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Agenda

– interfaces and realization– type hierarchy– introduction to graphics and event handling

Interfaces and Realization

• the form of an interface definition• the function of an interface• the realization (“implements”) relationship

Form of an interface• header + body

– header• access control modifier• keyword ‘interface’• name (generally an adjective, following class name

conventions, but prefixed with an upper-case ‘I’)

– body• method specifications (method headers followed by ‘;’,

also called method declarations, as opposed to method definition)

CSE 115/503 Introduction to Computer Science for Majors I 4

Interfaces – no instantiation

• While classes can be instantiated, interfaces cannot be instantiated.

Examples

1) Goofy example, tying in with Dog example

public interface ICollarable { public void addCollar(Collar collar); public Collar removeCollar();}

2) Example from Java’s libraries (one detail omitted)

public interface ActionListener { public void actionPerformed(ActionEvent e);}

CSE 115/503 Introduction to Computer Science for Majors I 6

Implementation

• A class can implement an interface:

public class Dog implements ICollarable {

...

}

CSE 115/503 Introduction to Computer Science for Majors I 7

Implementation as contract

• A class which implements an interface is obligated to provide full definitions of all the methods specified in the interface.

ExampleProblem – methods of interface are not defined

(incorrect name)

public class Dog implements ICollarable { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; }}

public class Dog implements ICollarable { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void addCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; }}

ExampleSolution – methods of interface defined

(correct name)

CSE 115/503 Introduction to Computer Science for Majors I 10

Types

• When you define a class, you are defining a type.

• When you define an interface, you are also defining a type.

• A class which implements an interface is a SUBTYPE of the interface type.

CSE 115/503 Introduction to Computer Science for Majors I 11

Assignment

• If a variable is declared to be of an interface type, it can be assigned an instance of a subtype class.

Examples

ICollarable x;

x = new ICollarable(); NO! Cannot instantiate.

x = new Dog(); OK if Dog implements ICollarable

x = new Cat(); OK if Cat implements ICollarable

CSE 115/503 Introduction to Computer Science for Majors I 13

The point?

• Subtypes of a common supertype can be treated uniformly when treated as members of the supertype: see next slide.

Calling methods

ICollarable x;

x = new ICollarable(); NO! Cannot instantiate.

x = new Dog(); OK if Dog implements ICollarable

x = new Cat(); OK if Cat implements ICollarable

x.addCollar(new Collar()); OK

x.removeCollar(); OK

x.walk(); NO! Not all ICollarable objects define this method!

CSE 115/503 Introduction to Computer Science for Majors I 15

Things to mention

• public static void main(String [] args)– The starting point for a free-standing Java application (i.e. one not

run from the DrJava interactions pane)

• imports– Allow us to use, with unqualified names, things defined in other

packages

• comments – ignored by the compiler– block comments /* … */– single-line comments //– javadoc comments and tags (?) /** … */

• String class and String literals (e.g. “Fred”)• true as a boolean literal (for setVisible method call)

CSE 115/503 Introduction to Computer Science for Majors I 16

Using the Java graphics classes

• In this slide set we will explain the basics of how to create graphical programs.

• Some advanced issues will be glossed over (e.g. thread safety, graphics library design).

• In CSE116 we will revisit these and other more advanced topics.

CSE 115/503 Introduction to Computer Science for Majors I 17

Graphical elements

• There are two basic types of graphical elements:– Containers

• able to hold graphical objects, such as containers and components

– Components• must be put into containers• able to generate events when manipulated

CSE 115/503 Introduction to Computer Science for Majors I 18

Containers• Top-level containers

– some containers are called “top-level” because they do not need to be place inside any other containers

– javax.swing.JFrame is an example (JDialog and JApplet are others)

• Other containers (not top-level)– most containers must be placed inside some other

container– javax.swing.JPanel is an example

CSE 115/503 Introduction to Computer Science for Majors I 19

Adding elements to a JFrame• Top-level containers have multiple panes• Content pane is the one which holds components• With javax.swing.JFrame, two ways:

– call getContentPane() on frame to get frame’s content pane, then call add(…) on content pane to add a component

– call add(…) directly on the JFrame object

• Second approach is just a convenience method, does the same thing the first approach

CSE 115/503 Introduction to Computer Science for Majors I 20

Example• Creating just a frame

– new javax.swing.JFrame()

• Creating a frame with a title– new javax.swing.JFrame(“My title”)

• Making the frame visible– call setVisible(true) on the frame

• Making application close when window is closed:– call setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) on the frame

• See code in:– the BasicExample class in the graphics package of the GraphicsExamples

project in the repository, as well as,– the ex_1_JFrame_visible package in the FA10-CSE115-SwingExamples

project in the repository.

CSE 115/503 Introduction to Computer Science for Majors I 21

A simple component

• A JLabel is a component that can display text or an image.

• It can also have a “tooltip”, a note that appears when you hover the mouse pointer over it, without clicking.

• Look at ex_2_JLabel_tooltip

CSE 115/503 Introduction to Computer Science for Majors I 22

Layout managers

• First, let’s consider what happens if we add another JLabel to the content pane of the JFrame.

• Look at ex_3_JLabels_noLayout• This will show us the need to manage the

layout of components added to a container.• See also code in GraphicsExamples project

for this and the rest of the slides.

CSE 115/503 Introduction to Computer Science for Majors I 23

Applying a layout management strategy

• Look at ex_4_staticLayout

• In this example we can apply one of four different layout management strategies to a set of seven JLabels that are added to the JFrame’s content pane.

CSE 115/503 Introduction to Computer Science for Majors I 24

Another component: a JButton• A JButton is a component which is typically

set up to react to clicks.• Look at ex_5_JButton_noEventHandler,

this example just shows a JButton.

CSE 115/503 Introduction to Computer Science for Majors I 25

Events

• Clicks on buttons, mouse movements, etc. are all considered events.

• A program can react to events by setting up event handlers.

• An event handler defines what should happen when a particular event occurs.

CSE 115/503 Introduction to Computer Science for Majors I 26

Event handling – 1• The component which gives rise to an event is

decoupled from the part of the code that handles the event.

• This is called the observer pattern.• General form:

– www.research.ibm.com/designpatterns/example.htm

CSE 115/503 Introduction to Computer Science for Majors I 27

Event handling – 2• Observer pattern in Java

– An observer is called a listener in Java– Button clicks are “ActionEvents”.– Handlers for ActionEvents are ActionListeners.– An event-generator can have many listeners– Use “addActionListener” method to register a listener

with a component

CSE 115/503 Introduction to Computer Science for Majors I 28

Event handling for a JButton

• Look at ex_6_JButton_eventHandler• In this example an event handler is defined

and attached to the JButton.

CSE 115/503 Introduction to Computer Science for Majors I 29

Putting it all together

• Look at ex_7_dynamicLayoutEventHandling

• In this example we put everything together:– different components are put into different containers– multiple components are put into each container– different layout managers can be applied dynamically– JButtons’ event handlers select and apply different

layout managers to the container containing JLabels