340 likes | 891 Views
SE204 Object-Oriented Development Jacqueline McQuillan B.Sc. NUI Maynooth Overview Java Graphics Threads I/O in Java Object Serialization Remote Method Invocation (RMI) More … Java Graphics Basic Java Graphics
E N D
SE204 Object-Oriented Development Jacqueline McQuillan B.Sc. NUI Maynooth
Overview • Java Graphics • Threads • I/O in Java • Object Serialization • Remote Method Invocation (RMI) • More … 2004/5: SE204: Lecture 1&2
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 2004/5: SE204: Lecture 1&2
A Simple Example 2004/5: SE204: Lecture 1&2
Another Example 2004/5: SE204: Lecture 1&2
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 2004/5: SE204: Lecture 1&2
AWTThe Abstract Windows Toolkit • java.awt • components are heavyweight • appearance of awt component is determined by the peer component • look is platform dependent 2004/5: SE204: Lecture 1&2
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 • it is possible to make the swing components look like anything you want. This is referred to as pluggable look and feel(PLAF) 2004/5: SE204: Lecture 1&2
Main categories of Java GUI Framework • Components:These are the building blocks of GUI based applications. Examples: JFrame, JButton, JLabel • Containers: A container is a component that holds other components. Examples: JFrame, JPanel, JWindow • Layout managers:These are used for laying out components in a container. 2004/5: SE204: Lecture 1&2
Main categories of Java GUI Framework • 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. 2004/5: SE204: Lecture 1&2
Main categories of Java GUI Framework • Graphics and Imaging classes:These are used for drawing and displaying images. Examples: Graphics, Color, Font, Rectangle, Image 2004/5: SE204: Lecture 1&2
Swing classes in UML notation 2004/5: SE204: Lecture 1&2
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 2004/5: SE204: Lecture 1&2
An Example of a JFrame 2004/5: SE204: Lecture 1&2
Using JFrame Let’s look at an example 2004/5: SE204: Lecture 1&2
Using JFrame Let’s look at another example 2004/5: SE204: Lecture 1&2
Both of the above programs give the same output 2004/5: SE204: Lecture 1&2
Another JFrame Example 2004/5: SE204: Lecture 1&2
Let’s look at the output of the previous program 2004/5: SE204: Lecture 1&2
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(Color.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. 2004/5: SE204: Lecture 1&2
setSize(250, 100); • specifies the width and height of theframe 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 2004/5: SE204: Lecture 1&2
The Toolkit Class • This is an abstract class that provides an interface to platform specificdetails like window size, available fonts and images. • We never directly instantiate an object of type Toolkit. We obtain a toolkitobject by invoking the static getDefaultToolkit() method. This will give usan object that is appropriate for our system. Toolkit tk = Toolkit.getDefaultToolkit(); 2004/5: SE204: Lecture 1&2
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() 2004/5: SE204: Lecture 1&2
Construct a frame that fills the entire window 2004/5: SE204: Lecture 1&2
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 2004/5: SE204: Lecture 1&2
Using JPanel 2004/5: SE204: Lecture 1&2
Let’s look at the output of the previous program 2004/5: SE204: Lecture 1&2
Adding Components • We will not try to learn everything about a particular user interfacecomponent. • It will be better if we try to understand the concepts and then we will beable to search the Java documentation for the details. • Adding a component to a container: 1. Construct the component object 2. Add it to the container 2004/5: SE204: Lecture 1&2
Using JButton 2004/5: SE204: Lecture 1&2
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. 2004/5: SE204: Lecture 1&2