Lecture 1 and 2

clumpsmackoverΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 11 μήνες)

96 εμφανίσεις

1

SE204
Object
-
Oriented
Development

Jacqueline McQuillan B.Sc.

NUI Maynooth

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
2

Overview


Java Graphics


Threads


I/O in Java


Object Serialization


Remote Method Invocation (RMI)


More …


3

Java Graphics

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
4

Basic Java Graphics


We can use Java to develop computer
graphics based applications, both for 2
-
dimensional drawing and for Graphical
User Interfaces(GUIs).



We will begin by looking at GUI
programming in Java

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
5

A Simple Example

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
6

Another Example

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
7

Graphical User Interface


Use components to build our GUI



It is possible for a component to have a peer



The peer is a native implementation of that
component


for instance, a button object in an application will
correspond to the native button implemented by the
operating system


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
8

AWT

The Abstract Windows Toolkit


java.awt



components are heavyweight


appearance of awt component is determined by the peer
component


l
ook is platform dependent


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
9

Swing


javax.swing



most of its components are lightweight


component has no peer


look is determined by the java runtime environment, so
will look the same regardless of the platform being used



i
t is possible to make the swing components
look like anything you want. This is referred
to as pluggable look and feel(PLAF)


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
10

Main categories of Java GUI
F
ramework


Components:

These

are the building blocks
of GUI based applications.

Examples: JFrame, JButton, J
L
abel



Containers:

A container is a component that
holds other components.

Examples: JFrame, JPanel, J
W
indow



Layout managers:

These are u
sed for
laying out components in a container.

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
11

Main categories of Java GUI
F
ramework


Events:

An action is translated into an
EventObject. This object contains details
about the event including type of action and
where it occurred.

Examples: key presses, button presses, mouse moves



Event Listeners:

A class indicates which
events it would like to receive. This is done
by installing event listener objects.




J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
12

Main categories of Java GUI
F
ramework


Graphics and Imaging classes:

These are
u
sed for drawing and displaying images.

Examples: Graphics, Color, Font, Rectangle, Image


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
13

Swing classes in UML notation

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
14

JFrame


A Frame object is an (optionally) resizable
top
-
level window with


a title


a minimize box


a maximize box


a close box



JFrame is a direct extension of the AWT
Frame class. The AWT Frame class extends
the Window class

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
15

An Example of a JFrame

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
16

Using JFrame

Let’s look at an example





import javax.swing.*;

class
F
rame1 {


public static void main(String[] args){



JFrame f = new JFrame(“
F
rame 1”);



f.setSize(250, 100);



f.setVisible(true);


}

}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
17

Using JFrame

Let’s look at another example

import javax.swing.*;

class Frame
2

extends JFrame {


Frame
2
(String s){



super(s);



setSize(250,100);



setVisible(true);


}



public static void main(String[] args){



new Frame
2
(“Frame

1
”);


}

}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
18

Both of the above programs give the same

output


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
19

Another JFrame Example

import javax.swing.*;

Import java.awt.*;

class MyFrame extends JFrame {


MyFrame(String s){



super(s);



getContentPane().
setBackground(
C
olor.blue);



setSize(250,100);



setLocation(0,0);



setVisible(true);


}


public static void main(String[] args){



new MyFrame(“My Frame”);


}

}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
20

Let’s look at the output of the previous program

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
21

Now, lets look at the code


super(s);



this invokes the constructor of the parent superclass …
the positioning of this is vital (it can/should
only

be done
on the first line of the constructor method of the new
subclass)



getContentPane().
setBackground(Col
or.blue);



here, we use the
Color blue

to set the background
colour
,
note the use of getContentPane(), this
gives a reference to the JFrame’s container.



J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
22


setSize(250, 100);


specifies the width and height of the

frame in pixels



setLocation(0,0);


specifies where to place the frame on the desktop



setVisible(true);


used to tell
the screen

to display the frame


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
23

The Toolkit Class


This is an abstract class that provides an
interface to platform specific

details like
window size, available fonts and images.



We never directly instantiate an object of
type Toolkit. We obtain a toolkit

object by
invoking the static getDefaultToolkit()
method. This will give us

an object that is
appropriate for our system.

Toolkit tk = Toolkit.getDefaultToolkit();


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
24

The Toolkit Class


Situations where you might need to use a
Toolkit


want to load an image file, use
getImage()


want to obtain information about the screen, use
getScreenSize()


want information about available fonts, use
getFontList()



J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
25

Construct a frame that fills the entire window

import java.awt.*;

import javax.swing.*;

public class tkFrame extends JFrame{


tkF
rame(){



super(“Frame to fill entire window”);



Toolkit tk = Toolkit.getDefaultToolkit();



Dimension d = tk.getScreenSize();



setSize(d.width, d.height);



setVisible(true);


}

}

public class test{


public static void main(String args[]){



new tkFrame();


}

}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
26

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
27

JPanel


It is a component



It can contain other components



It can be structured using the layout
managers



Constructors


JPanel()
--

Creates a new JPanel with a
FlowLayout


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
28

Using JPanel

import java.awt.*;

import javax.swing.*;


class MyPanel extends JPanel{






MyPanel(){



setPreferredSize(new Dimension(300, 200));



setBackground(Color.white);


}



public void paintComponent(Graphics g){



super.paintComponent(g);



g.drawString("Hello World", 100,100);


}


}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
29

public class panelFrame extends JFrame {




panelFrame(){



super("Panel Frame");



MyPanel
p1 = new MyPanel();



getContentPane().add(p1);



pack();



setVisible(true);


}



public static void main(String args[]){



new panelFrame();


}

}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
30

Let’s look at the output of the previous program

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
31

Adding Components


We will not try to learn everything about a
particular user interface

component.



It will be better if we try to understand the
concepts and then we will be

able to search
the
J
ava documentation for the details.



Adding a component to a container:

1.

Construct the component object

2.

Add it to the container


J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
32

Using JButton

import java.awt.*;

import javax.swing.*;


class buttonPanel extends JPanel{


private JButton b1;


private JButton b2;



buttonPanel(){



setBackground(Color.white);



b1 = new JButton("Button 1");



b2 = new JButton("Button 2");



add(b1);



add(b2);


}

}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
33

public class buttonFrame extends JFrame {



private buttonPanel p1;



buttonFrame(){



super("Button Frame");



setSize(200, 200);





p1 = new buttonPanel();



getContentPane().add(p1);



setVisible(true);


}



public static void main(String args[]){



new buttonFrame();


}

}

J McQuillan


200
4
/
5
: SE204:
L
ecture 1
&2

Slide
34

Now, lets look at the code



b1 = new JButton(“Button 1”);


creates an instance of JButton with the label “Button 1”



add(b1);


adds the JButton b1 to the container buttonPanel



getContentPane().add(p1);


adds the component p1 to the frame
,
note the use of
getContentPane(), this gives a reference to the JFrame’s
container.