ISE 582: Information Technology for Industrial Engineering

advertisement
ISE 582: Information Technology
for Industrial Engineering
Instructor: Elaine Chew
University of Southern California
Department of Industrial and Systems Engineering
Lecture 7
JAVA Cup Seven: It’s All in the Packaging
Winston & Narasimhan: Chapt 33-40
JAVA Cup 7
• How to Read/Write Objects
• How to Modularize Programs
• Creating a Window
• Drawing Lines in Windows
• Writing Text in Windows
8 October 2003
Web Technology for IE
2
Review
8 October 2003
stream
reader
buffer
stream
reader
tokens
stream
writer
Web Technology for IE
3
Reading / Writing Objects
• Serialized input-output operations
– <obj.o/p.stream>.writeObject(<vector>)
– <obj.i/p.stream>.readObject()
• Indicate intentions to use operations by
– implements Serializable
– throws IOException, ClassNotFoundException
• Each instance must belong a class that implements the
Serializable Interface
8 October 2003
Web Technology for IE
4
Reading Objects from File
FileInputStream fileInputStream
= new FileInputStream(“in.data”);
ObjectInputStream objectInputStream
= new ObjectInputStream(fileInputStream);
Vector <vector>
= (Vector) objectInputStream.readObject();
8 October 2003
Web Technology for IE
5
To Write Objects to File
FileOutputStream fileOutputStream = new
FileOutputStream(“out.data”);
ObjectOutputStream objectOutputStream = new
ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(<vector>);
objectOutputStream.close();
8 October 2003
Web Technology for IE
6
Modularing Your Program
•
•
•
•
•
Compilation Units
Packages
Setting CLASSPATH
Calling Methods from a Module
Access to Var/Methods in Package
8 October 2003
Web Technology for IE
7
Compilation Units
• Define all classes in same file
• Only one class can be public
• Only one class is universally
accessible
• Convention: each class has its own
compilation unit
8 October 2003
Web Technology for IE
8
Packages
• Use single-class compilation units
• Indicate module using package
statement at beginning of each c.u
– package onto.java.entertainment;
• Convention: package names consist of
components separated by dots
8 October 2003
Web Technology for IE
9
CLASSPATH
• Package names correspond to paths
– package onto.java.entertainment;
– Files stores in ./onto/java
• Setting the path
UNIX
– setenv CLASSPATH <path1>:<path2>:.
– setenv CLASSPATH=<path1>;<path2>;.
PC
8 October 2003
Web Technology for IE
10
Using Methods from a Package
• First import package
– import onto.java.entertainment.*
– Auxiliaries.readMovieFile(“input.data”)
– ((Movie) vectorElement).rating()
• Or, refer to entire name
– onto.java.entertainment.Auxiliaries.readMovieFile
(“input.data”)
– ((onto.java.entertainment.Movie)
vectorElement).rating()
8 October 2003
Web Technology for IE
11
Access to Variables/Methods
• private :
– available only to methods inside own class
• protected :
– available to methods in same class, c.u. or package, subclasses
• No keyword :
– available only to c.u. or package
8 October 2003
Web Technology for IE
12
How to Create Windows
•
•
•
•
•
•
•
Nomenclature
Intro to Swing classes
Hierarchy of Swing classes
Listener Classes
Window Creation Pattern
Drawing Lines in Windows
Writing Text in Windows
8 October 2003
Web Technology for IE
13
Nomenclature
• GUI (pronounced goo-ey) :
– Graphical User Interface
• Components :
– class instances that have a graphical representation
• Containers :
– components that allow nesting of other containers inside
their boundaries
• Window :
– a container’s graphical representation
8 October 2003
Web Technology for IE
14
Introduction to Swing (J) Classes
• API = Application Programmer’s Interface
• The AWT Package: java.awt.*
– AWT = Abstract Window Toolkit
– contains Component, Container classes
• The Swing Package: java.swing.*
– contains JFrame, JApplet, JComponent, JPanel classes
• Try to restrict display work to Swing classes
8 October 2003
Web Technology for IE
15
Hierarchy of Swing Classes
Component
Container
Window
Panel
Frame
Applet
JFrame
JComponent
JPanel
JList
JLabel
JTextField
JApplet
JButton
8 October 2003
Web Technology for IE
16
Example: A Window
import javax.swing.*;
public class Demonstrate {
public static void main (String argv []) {
JFrame frame = new JFrame (“I am a
window, square and proud”);
frame.setSize(200,200);
frame.show();
}
}
8 October 2003
Web Technology for IE
17
Listener Classes
• Event as a state change
– Mouse clicks and key presses
– Variable-value changes
• Event as instance of EventObject class
– Describes state change
• Java deals with events through Listener Classes
• Listener classes are defined by
– Extending existing listener classes
– Implementing listener interfaces
8 October 2003
Web Technology for IE
18
Example: Link listener to frame
• When mouse clicks:
– a WindowEvent instance is created
– the windowClosing method is called with
connected listener as target and
WindowEvent instance as arg
8 October 2003
Web Technology for IE
19
Adapter Classes
• Adapter classes implement all
interface-required methods as donothing methods.
• You can define shadowing methods in
your subclass for whatever subset of
methods you need
8 October 2003
Web Technology for IE
20
Nesting Class Definitions
• Advantage of nesting classes:
– Method definition (windowClosing)
appears in proximity to window definition
– Can reuse names of private classes such
as LocalWindowListener
8 October 2003
Web Technology for IE
21
Window Creation Pattern
• Define a subclass of JFrame class
• Main method calls constructor
• Constructor:
– assigns title, size, and displays a window
– creates required class instances
– connects class instances
8 October 2003
Web Technology for IE
22
How to Draw Lines in Windows
•
•
•
•
•
•
The Paint Method
Creating a Meter
Putting the Meter in a Frame
Layout Managers
Component Size
Line Color
8 October 2003
Web Technology for IE
23
JComponent Class: Paint Method
• paint method is called whenever
repaint is called with a component as
target
• paint method is called on every
component attached to a frame
whenever you iconify, deiconify, or
expose a frame’s window.
8 October 2003
Web Technology for IE
24
Example: Creating a Meter
Meter
Class
extends
Shadowing
paint method
8 October 2003
Web Technology for IE
JComponent
Class
Java’s basic
paint method
25
Meter Interface
• Require the following methods:
– setValue
– setTitle
– getValueAtCoordinates
8 October 2003
Web Technology for IE
26
Beginnings of a Meter
import java.awt.*; import javax.swing.*;
public class Meter extends JComponent implements MeterInterface {
String title = "Title to Be Supplied"; // Title string for the
meter
int minimum, maximum; // Minimum and maximum values displayed
int value;
// Currently displayed value
// Two-parameter constructor:
public Meter (int x, int y) {
minimum = x;
maximum = y;
value = (x + y) / 2; }
// Setters:
public void setTitle(String s) {title = s; repaint();}
public void setValue(int v) { value = v; repaint(); }
// getValueAtCoordinates to be defined...
// Paint to be defined...}
8 October 2003
Web Technology for IE
27
The Paint Method
x
0,0
Java’s Coordinate System
y
8 October 2003
public void paint(Graphics g) {
g.drawLine( 0, 50,100, 50);
g.drawLine(50, 50, 50, 40);
}
Web Technology for IE
28
Attaching Meter to a Frame
import javax.swing.*;
import java.awt.event.*;
public class MovieApplication extends JFrame {
public static void main (String argv []) {
new MovieApplication("Movie Application");
}
// Declare instance variables:
private Meter meter;
// Define constructor:
public MovieApplication(String title) {
super(title);
meter = new Meter(0,30);
getContentPane().add("Center",meter);
… } etc…
8 October 2003
Web Technology for IE
29
Layout Manager
• Every frame’s content pane has an
associated Layout Manager
• Default: Border Layout
– Allows 5 components to be added at
positions “Center”, “North”, “East”,
“South” and “West”
– Can be specified explicitly with
getContentPane().setLayout(new BorderLayout());
8 October 2003
Web Technology for IE
30
Setting Sizes
You may have to define getMinimumSize,
getMaximumSize or getPreferredSize to get the
desired effect.
import java.awt.*;
import javax.swing.*;
public class Meter extends JComponent implements MeterInterface {
// variables ...
// constructor ...
height
// Setters: setTitle, setValue ...
width
// getValueAtCoordinates ...
// Paint ...
public Dimension getMinimumSize() {return new Dimension(150, 150);}
public Dimension getPreferredSize() {return new Dimension(150, 150);}
}
8 October 2003
Web Technology for IE
31
Accounting for Component Size
public void paint(Graphics g) {
Dimension d = getSize();
int meterWidth = d.width * 3 / 4;
int meterHeight = meterWidth / 20;
int dialPosition
= meterWidth * (value-minValue)/(maxValue-minValue);
int xOffset = (d.width-meterWidth) / 2;
int yOffset = d.height / 2;
g.drawLine(xOffset, yOffset, xOffset+meterWidth, yOffset);
g.drawLine(xOffset+dialPosition, yOffset,
xOffset+dialPosition, yOffset-meterHeight);
}
8 October 2003
Web Technology for IE
32
Finding closest gridline
public void getValueAtCoordinates(int x, int y) {
Dimension d = getSize();
int meterWidth = d.width * 3 / 4;
int xOffset = (d.width-meterWidth) / 2;
float fraction = (float)(x-xOffset) / meterWidth;
return
(int)Math.round(fraction*(maxValue-minValue)+minValue);
}
8 October 2003
Web Technology for IE
33
Graphics Context Variable: Color
public void paint(Graphics g) {
Color colorHandle = g.getColor();
g.setColor(Color.blue);
...
g.setColor(colorHandle);
}
• The color class provides class variables for common
colors: black, blue, cyan, darkGray, gray, green,
lightGray, magenta, orange, pink, red, white and
yellow.
• You can create your own Color using the 3-parameter
constructor: e.g. new Color (0,0,255)
8 October 2003
Web Technology for IE
34
Writing Text in Windows
• Text Methods
• Font Metrics Class
8 October 2003
Web Technology for IE
35
Text Methods
public void paint(Graphics g) {
g.setFont(new Font(“Helvetica”, Font.BOLD, 12));
g.drawString(title,100,50);
}
• Font Families: Roman, Helvetica.
• Font Styles: PLAIN, BOLD, ITALICS.
• Font Sizes: in points.
8 October 2003
Web Technology for IE
36
FontMetrics Class
public void paint(Graphics g) {
Dimension d = getSize();
int fontSize = d.width / 30;
g.setFont(new Font(“Helvetica”, Font.BOLD, fontSize));
FontMetrics f = g.getFontMetrics();
int stringWidth = f.stringWidth(title);
int xOffset = (d.width - stringWidth) / 2;
int yOffset = d.height / 2;
g.drawString(title,xOffset,yOffset);
}
f.getHeight()
f.getDescent()
8 October 2003
Web Technology for IE
height
descent
q
37
Download