Slide 1 - Go to people.clarkson.edu

farrightSoftware and s/w Development

Aug 15, 2012 (4 years and 11 months ago)

254 views

CS242

Advanced Programming
Concepts in Java

9/11

Abstract Class

Introduction to GUIs

Prof. Searleman

jets@clarkson.edu

Outline


Abstract Classes


Abstract Class vs. Interface


Graphical User Interfaces (GUIs)



Java Foundation Classes (JFC): Swing



Basics of creating and launching a GUI


Read: EJ2, Chapter 4, Item 18


HW#2

due today

HW#3

due Monday, 9/21/09, in class:w


Reading assignment: GUIs


The Java Tutorials on GUIs and Swing are a
good place to start


Trail: Graphical User Interfaces


Trail: Creating a GUI with JFC/Swing


Getting Started with Swing


Learning Swing with the NetBeans IDE



NetBeans has good tutorials on the GUI editor


Introduction to GUI Building


Designing a Swing GUI in NetBeans


Abstract Class

Some classes exist only so their methods
can be inherited (guarantees that all
descendants share a common set of
operations on their public interface)

cannot instantiate an abstract class


examples:



Number


Integer, Float, BigDecimal, …



Shape


Circle, Line, Rectangle, …

Integer


BigDecimal

Float

<<abstract>>


Number

isa


Byte

methods:
byteValue(),
intValue(), …

Circle


Rectangle

Line

<<abstract>>


Shape

isa


Arrow

methods:
draw(),
computeArea()

public abstract class Shape

{


// can define constants


public static final double TWO_PI = 2*Math.PI;



// can declare abstract methods


public abstract double computeArea();



public abstract void draw();



// can implement methods


public String aka() { return “euclidean”; }

}

public class Circle


extends Shape


implements Comparable, Cloneable

{


// override draw() & computeArea()


// add code for compareTo()

}


public class Rectangle


extends Shape


implements Comparable, Cloneable

{


// override draw() & computeArea()


// add code for compareTo()

}

Concrete subclass of the
Abstract class Shape

Concrete subclass of the
Abstract class Shape

Circle

FilledCircle

Object

methods: toString(),
equals(), getClass(), …

methods: constructors,
get/setCenter(), get/setRadius(),

computeArea(), toString(),
equals(), compareTo()

methods: constructors,
get/setColor(), toString(),

equals()

<<abstract>>


Shape

methods:

abstract computeArea(),

abstract draw(), aka()

Why use an abstract class?


Suppose that you would like an array of
circles, rectangles, lines, etc. How can you
do this?


Circle[ ] drawing; // only can contain circles


Rectangle[ ] drawing; // still a problem


But with the abstract class Shape, can have


Shape[ ] drawing;

Abstract vs. concrete


Shape thing = new Shape();


Shape thing = new Circle(100,200);


Shape thing = new Rectangle(50,25,75,40);


Shape[ ] drawing = new Shape[5];


Shape[0] = new Rectangle(50,25,75,40);


Circle c1 = new Circle(100,200);


Shape[1] = c1;

// syntax error

// OK

// cannot instantiate an abstract class

// every Circle “is a” Shape (substitutability)

// OK

// creates an array of references, not of objects

// every Rectangle “is a” Shape (substitutability)

// OK

// OK

// every Rectangle “is a” Shape (substitutability)

// OK

Abstract class

“template for a collection of related subclasses”


may contain instance variables, constants,
concretely implemented methods


when a class extends an abstract class, it
may implement all or some of the methods; if
it does not implement all abstract methods,
then it must be declared to be abstract itself


cannot instantiate an abstract class


can declare a reference to one


Interface

“specification of an interface or protocol for a class (may
be implemented in unrelated classes)”


may not contain instance variables or any code, but may
contain (static final) constants


when a class states that it implements an interface, it
MUST implement ALL methods in the interface


a class can implement more than one interface


all methods in an interface are abstract and public, with
or without the modifiers “public” and/or “abstract”


an interface can be empty, in which case it is a marker
for a special property (e.g. Cloneable, Serializable)


an interface can be used to implement “callbacks”


interfaces can extend other interfaces


cannot instantiate an interface


can declare a reference to one

Interface vs. Abstract Class

An interface is simply a list of unimplemented, and therefore
abstract, methods. So how does an interface differ from an
abstract class? The differences are significant.




An interface cannot implement any methods, whereas an
abstract class can.



A class can implement many interfaces but can have only
one superclass.



An interface is not part of the class hierarchy. Unrelated
classes can implement the same interface.


Use an abstract class when you want a template for
a collection of subclasses


Subclass when you want to extend a class and add
some functionality, whether or not the parent class is
abstract


Subclass when you must (e.g. Applets)


Define an interface when there is no common parent
class with the desired functionality, and when you
want only certain unrelated classes to have that
functionality


Use interfaces as “markers” to indicate something
about a class (e.g. Cloneable


can make a copy)

A common design pattern is to use both an
interface and an abstract class




Circle

FilledCircle

Object

<<abstract>>


Shape


<<interface>>


Comparable

Point

IS
-
A

IS
-
A

IS
-
A

implements

implements

IS
-
A

Graphical User
Interfaces: Swing

JFC


AWT (Abstract Window Toolkit)


java.awt.* and java.awt.event.*


Swing


javax.swing.* and javax.swing.event.*


Accessibility, 2D API, Drag & Drop

Swing


consistent user experience across
platforms (written in Java, peerless)


pluggable look & feel (PLAF)


(Windows, Metal, Motif L&Fs builtin)


Model
-
View
-
Controller (MVC) Framework

Swing API


javax.swing


javax.swing.event


other Swing packages include:

javax.accessibility, javax.swing.border

javax.swing.colorchooser,

javax.swing.filechooser, javax.swing.plaf

javax.swing.table, javax.swing.text

javax.swing.text.html, javax.swing.text.rtf

javax.swing.tree, javax.swing.undo

Object

Component

JComponent

Window

JPanel

JFrame

Frame

Container

AbstractButton

JButton

Look & Feel

Java L&F (Metal)

GTK+ L&F (Motif)

Windows L&F

Mac OS L&F

HelloWorldSwing

Basic Swing code

1.
Import the relevant packages.

2.
Set up a top
-
level container.

3.
Display the container.

4.
Be thread
-
safe.


Basic Swing code

1.
Import the relevant packages:



import javax.swing.*;



import java.awt.*;



import java.awt.event.*;




2.
Set up a top
-
level container.

3.
Display the container.

4.
Be thread
-
safe.


Basic Swing code

1.
Import the pertinent packages



2.
Set up a top
-
level container.

JFrame frame = new JFrame("HelloWorldSwing");

frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);

//Add a label to the content pane

JLabel label = new JLabel("Hello World");

frame.getContentPane().add(label);

1.
Display the container.

2.
Be thread
-
safe.


Basic Swing code

1.
Import the pertinent packages



2.
Set up a top
-
level container.

JFrame frame = new
JFrame("HelloWorldSwing”);


3.
Display the container.

frame.pack();

frame.setVisible(true);


4.
Be thread
-
safe.


Basic Swing code

1.
Import the pertinent packages



2.
Set up a top
-
level container.

3.
Display the container.

4.
Be thread
-
safe.

javax.swing.SwingUtilities.invokeLater(


new Runnable() {


public void run() {


/* create and show the GUI */


} // end run


} // end Runnable

); // create and run a thread

import javax.swing.*;

public class HelloWorldSwing {

private static void createAndShowGUI() {


JFrame.setDefaultLookAndFeelDecorated(true);


JFrame frame = new JFrame("HelloWorldSwing");


frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);


JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);


frame.pack();


frame.setVisible(true);

}

public static void main(String[] args) {


//Schedule a job for the event
-
dispatching thread:


//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(



new Runnable() {



public void run()



{ createAndShowGUI(); }


});


} // end main

} // end class HelloWorldSwing


Show Demo of HelloWorldSwing