• No results found

GUI_Applications.pdf

N/A
N/A
Protected

Academic year: 2020

Share "GUI_Applications.pdf"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter 14, 15, 16

(2)

Agenda

Graphics

Coordinate system

Graphics Class

paintComponent Method

Events

Event-Driven Programming

Inner Classes

GUI Applications

Buttons, Checkboxes, Radiobuttons, and Textfields

(3)

Java Coordinate System

(0, 0)

X Axis

Y Axis

(x, y)

x

y

Java Coordinate

System

X Axis

Conventional

Coordinate

System

(4)

Each GUI Component Has its

Own Coordinate System

(0, 0)

Component c2

Component c1

(0, 0)

(0, 0)

(x1, y1)

(x2, y2)

(x3, y3)

Component c3

c3’s coordinate

system

c2’s coordinate

system

(5)

The Graphics Class

You can draw strings,

lines, rectangles,

ovals, arcs, polygons,

and polylines, using

the methods in the

Graphics class.

java.awt.Graphics

+setColor(color: Color): void +setFont(font: Font): void

+drawString(s: String, x: int, y: int): void +drawLine(x1: int, y1: int, x2: int, y2: int): void +drawRect(x: int, y: int, w: int, h: int): void

+fillRect(x: int, y: int, w: int, h: int): void

+drawRoundRect(x: int, y: int, w: int, h: int, aw: int, ah: int): void

+fillRoundRect(x: int, y: int, w: int, h: int, aw: int, ah: int): void

+draw3DRect(x: int, y: int, w: int, h: int, raised: boolean): void

+fill3DRect(x: int, y: int, w: int, h: int, raised: boolean): void

+drawOval(x: int, y: int, w: int, h: int): void

+fillOval(x: int, y: int, w: int, h: int): void

+drawArc(x: int, y: int, w: int, h: int, startAngle: int, arcAngle: int): void

+fillArc(x: int, y: int, w: int, h: int, startAngle: int, arcAngle: int): void

+drawPolygon(xPoints: int[], yPoints: int[], nPoints: int): void

+fillPolygon(xPoints: int[], yPoints: int[], nPoints: int): void

+drawPolygon(g: Polygon): void +fillPolygon(g: Polygon): void

Sets a new color for subsequent drawings. Sets a new font for subsequent drwings. Draws a string starting at point (x, y). Draws a line from (x1, y1) to (x2, y2).

Draws a rectangle with specified upper-left corner point at (x, y) and width w and height h.

Draws a filled rectangle with specified upper-left corner point at (x, y) and width w and height h.

Draws a round-cornered rectangle with specified arc width aw and arc height ah.

Draws a filled round-cornered rectangle with specified arc width aw and arc height ah.

Draws a 3-D rectangle raised above the surface or sunk into the surface.

Draws a filled 3-D rectangle raised above the surface or sunk into the surface.

Draws an oval bounded by the rectangle specified by the parameters x, y, w, and h.

Draws a filled oval bounded by the rectangle specified by the parameters x, y, w, and h.

Draws an arc conceived as part of an oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws a filled arc conceived as part of an oval bounded by the

rectangle specified by the parameters x, y, w, and h. Draws a closed polygon defined by arrays of x and y

coordinates. Each pair of (x[i], y[i]) coordinates is a point. Draws a filled polygon defined by arrays of x and y

(6)

The paintComponent Method

The paintComponent method is defined in JComponent,

and its header is as follows:

protected void paintComponent(Graphics g)

This method, defined in the JComponent class, is invoked

whenever the component is first displayed or

redisplayed. The Graphics object g is created

automatically by the JVM for every visible GUI

(7)

Drawing on Panels (p. 478)

JPanel can be used to draw graphics (including text) and

enable user interaction.

To draw in a panel, you create a new class that extends

JPanel and override the paintComponent method to tell

the panel how to draw things. You can then display

strings, draw geometric shapes, and view images on

the panel.

(8)

Drawing Geometric Figures

Drawing Strings

Drawing Lines

Drawing Rectangles

Drawing Ovals

Drawing Arcs

(9)

Drawing Arcs

drawArc(int x, int y, int w, int h, int angle1, int angle2);

fillArc(int x, int y, int w, int h, int angle1, int angle2);

(10)

Drawing Arcs Example (p. 484)

(11)

Case Study: MessagePanel

MessagePanel

-xCoordinate: int

-yCoordinate: int

-centered: boolean

-message: String

-interval: int

+MessagePanel()

+MessagePanel(message: String)

+moveLeft(): void

+moveRight(): void

+moveUp(): void

+moveDown(): void

javax.swing.JPanel

-char token +getToken +setToken +paintComponet +mouseClicked

The get and set methods for these data

fields are provided in the class, but

omitted in the UML diagram

The x-Coordinate for the message (default 20).

The y-Coordinate for the message (default 20).

Specifies whether the message is displayed centered.

The message to be displayed.

The interval to move the message in the panel.

Constructs a default message panel.

Constructs a message panel with a specified string.

Moves the message to the left.

Moves the message to the right.

Moves the message up.

Moves the message down.

MessagePanel

Run

This case study

develops a useful class

that displays a message

in a panel. The class

enables the user to set

the location of the

message, center the

message, and move the

message with the

specified interval.

(12)

Procedural vs. Event-Driven

Programming

Procedural programming

is executed in

procedural order.

In event-driven programming, code is executed

(13)

Events

An

event

can be defined as a type of signal to the

program that something has happened.

The event is generated by external user actions such

as mouse movements, mouse clicks, and keystrokes,

or by the operating system, such as a timer.

Listeners handle events

(14)
(15)

Inner Class Listeners

A listener class is designed specifically to

create a listener object for a GUI

component (e.g., a button). It will not be

shared by other applications. So, it is

(16)

Inner Classes

Inner class: A class is a member of another class.

Advantages: In some applications, you can use an

inner class to make programs simple.

An inner class can reference the data and

methods defined in the outer class in which it

nests, so you do not need to pass the reference

of the outer class to the constructor of the inner

class.

(17)

Inner Classes, cont.

public class

Test {

...

}

public class

A {

...

}

public class

Test {

...

// Inner class

public class

A {

...

}

}

(a)

(b)

// OuterClass.java: inner class demo

public class

OuterClass {

private int

data;

/** A method in the outer class */

public

void

m() {

// Do something

}

// An inner class

class

InnerClass {

/** A method in the inner class */

public void

mi() {

// Directly reference data and method

// defined in its outer class

(18)

Inner Classes (cont.)

Inner classes can make programs simple

and concise.

An inner class supports the work of its

containing outer class and is compiled

into a class named

OuterClassName

$

InnerClassName

.class.

For example, the inner class InnerClass in

OuterClass is compiled into

(19)

Inner Classes (cont.)

An inner class can be declared public,

protected, or private subject to the same

visibility rules applied to a member of the

class.

An inner class can be declared static. A

static inner class can be accessed using the

outer class name. A static inner class

(20)

Anonymous Inner Classes

An anonymous inner class must always extend a superclass or

implement an interface, but it cannot have an explicit extends or

implements clause.

An anonymous inner class must implement all the abstract

methods in the superclass or in the interface.

An anonymous inner class always uses the no-arg constructor

from its superclass to create an instance. If an anonymous inner

class implements an interface, the constructor is Object().

An anonymous inner class is compiled into a class named

OuterClassName$n.class. For example, if the outer class Test

has two anonymous inner classes, these two classes are

(21)

Anonymous Inner Classes (cont.)

Inner class listeners can be shortened using anonymous

inner classes. An

anonymous inner class

is an inner

class without a name. It combines declaring an inner

class and creating an instance of the class in one step.

An anonymous inner class is declared as follows:

new

SuperClassName/InterfaceName() {

// Implement or override methods in superclass or interface

// Other methods if necessary

(22)

Example: Controlling Balls (p. 522)

ControlBall

Run

(23)

Example: Moving Message Using

Mouse (p. 528)

Objective: Create a

program to display

a message in a

panel. You can use

the mouse to move

the message. The

message moves as

the mouse drags

and is always

displayed at the

mouse point.

(24)

Example: Using Buttons (p. 546)

Write a program that displays a

message on a panel and uses

two buttons, <= and =>, to move

the message on the panel to the

left or right.

Run

ButtonDemo

(25)

Example: Using Check Boxes

(p. 549)

Add three check boxes named

Centered

,

Bold

, and

Italic

into Example 16.1 to let

the user specify whether

the message is centered,

bold, or italic.

CheckBoxDemo

Run

ButtonDemo

(26)

Example: Using Radio Buttons

(p. 552)

Add three radio buttons

named

Red

,

Green

,

and

Blue

into the

preceding example to

let the user choose the

color of the message.

Run

RadioButtonDemo

ButtonDemo

CheckBoxDemo

(27)

Example: Using Text Fields

(p. 555)

Add a text field to the

preceding example

to let the user set a

new message.

Run

TextFieldDemo

(28)

Summary of Lecture

Graphics

Coordinate system

Graphics Class

paintComponent Method

Events

Event-Driven Programming

Inner Classes

GUI Applications

Buttons, Checkboxes, Radiobuttons, and Textfields

(29)

Course Conclusion

Java Fundamentals

– statements (sequential processing)

– flow control (branches and loops)

Problem Solving

Object-Oriented Technology

– encapsulation

– inheritance

– polymorphism

(30)

Post Mortem

What did you learn?

What did you like?

What did you not like?

How can we make the course better?

Do we have the proper focus?

Evaluate the materials: book, slides, sites

We will certainly program again so how can

References

Related documents

To minimize physical interaction between dealers and VAT Officials and for efficient administration of the Act, it is considered necessary that the details of

In vivo antimicrobial assay revealed that the mice treated with the crude methanolic and aqueous extracts after being infected with the various test organisms,

The cover image is partitioned into two equal parts and the dif- ference between the maximum and minimum value of secret data determines if we use four LSBs or five LSBs of each

Tua Jakarta area, namely the local community form communities and outsiders who contribute in the form of unique activities and attract visitors. The community, is

b) Derive test cases regarding invalid equivalence classes by combining test data from exactly one invalid equivalence class with test data from valid equivalence classes?. When

Aktivate the calibration mode by pressing the central lateral keys between 6 and 7 + OK-button (10) at the same time. Replace the two flashing 00 with the number 20 by pressing

University of Santa Maria, Brazil, Fernando Luís Herrmann, Federal University of Santa Maria, Brazil, Leandro Zafalon Pieper, Federal University of Santa Maria,