Académique Documents
Professionnel Documents
Culture Documents
COMP 103 L2
Lecture Program:
• Bridging lecture – 102 to 103
transition.
• Course Overview
• GUI Programs
• Recurring themes:
• Testing: Does it work right?
• Complexity How fast is it?
How much memory does it take?
Analysis and benchmarking.
• Good Design How do you get it right first time?
COMP103 2006/T3 3
Programming with libraries
Programming with Libraries
• Java libraries to build GUI programs (Graphical User
Interface)
• Programs with buttons
• Drawing graphics on the screen (DrawingCanvas)
• Outputting text to screen
• Responding to the mouse
• Other useful libraries
• Let user select files and choose colours.
• Reading data from files or standard input
• The Java Collections library
• New and improved in Java 1.5
• Easier to manipulate large collections of data
• Iterators, and the new “for each” loop
COMP103 2006/T3 4
Programming with linear collections
• Kinds of collections:
• Lists, Sets, Bags, Maps, Stacks, Queues, Priority Queues
COMP103 2006/T3 5
Programming with hierarchical collections
• Kinds of collections:
• Trees, binary trees, general trees, [? graphs]
COMP103 2006/T3 6
GUI Programs
Doit Save Open Quit
• GUI programs are:
• easier to use, and
• visually pleasing.
• user interface design = COMP
311
• We will use a “basic” design Debugging messages here
COMP103 2006/T3 8
Libraries for COMP 103
COMP103 2006/T3 10
The “Question” Program
import comp100.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.*;
COMP103 2006/T3 11
Structure of a GUI program
• Comment, including name
• List of libraries to import
• Javadoc comment describing the Class
• Class header
• public class Xxxx implements ActionListener,
MouseListener
5. Fields of the class, including the main GUI components
• frame, canvas, messageArea, [ textArea ]
6. The constructor
• Creates the frame, the drawing canvas, the message
area
• Creates the panel for the buttons and the buttons
7. Methods to respond to buttons and mouse
8. Other methods to do all the work
9. The main
COMP103 2006/T3method, 12
The DemoGraphics Program
/* Code for Assignment 1, Nov 2005*/
import java.util.*;
import comp100.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.*;
COMP103 2006/T3 14
The DemoGraphics Program
/** Constructs a DemoFiles object and sets up the user
interface */
public DemoGraphics() {
frame = new JFrame("Drawing shapes");
frame.setSize(600, 400);
//The buttons
JPanel buttonPanel = new JPanel();
frame.getContentPane().add(buttonPanel,
COMP103 2006/T3 BorderLayout.NORTH);
15
Listeners & Events
Yes No
• User
clicks/presses/releases/drags/
types.….on some GUI object
(window, button, panel,
canvas…) button1 Button 2 canvas1
objects
Object 15
• If so, it calls the xxxxPerformed mousePressed
method on the listener object, Object 10 mouseReleased
:
passing information about the actionPerformed
event. :
:
frame.setVisible(true);
}
COMP103 2006/T3 17
Listeners
The listener object must:
2. implement the ActionListener or MouseListener interface
3. Define the appropriate methods that the JVM can call.
COMP103 2006/T3 18
The DemoGraphics Program
/** Respond to button presses */
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Draw Rectangle"))
drawRectangle();
if (e.getActionCommand().equals("Draw Faces"))
drawFaces();
else if (e.getActionCommand().equals("Clear"))
canvas.clear();
else if (e.getActionCommand().equals("Quit"))
frame.dispose();
}
COMP103 2006/T3 19
The DemoGraphics Program
/** Draw a green rectangle on the screen */
public void drawRectangle() {
messageArea.setText("drawRectangle: 100x50 rect at (10,20)");
canvas.setForeground(Color.green); .
canvas.fillRect(10, 20, 100, 50);
}
COMP103 2006/T3 20
The DemoGraphics Program
COMP103 2006/T3 22
CrowdRenderer
• MakeCrowd
• Java API – ArrayList, List
// Construct new ArrayList
private List<Face> crowd = new ArrayList<Face>();