MSc IT Programming Methodology (2). Which printer [a] LPT1 or [b] LPT2?: How many copies do you wish to print?: [P]rint now or [C]ancel?: Print all pages.

Slides:



Advertisements
Similar presentations
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Advertisements

Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS Lecture 01 Frames and Components and events Lynda Thomas
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
Java Applet Basics (2). The Body Mass Index Calculator.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Review_6 AWT, Swing, ActionListener, and Graphics.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
Java Applet What is a Java Applet? How is applet compiled?
A First Look at GUI Applications
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical user interface-based programming
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

MSc IT Programming Methodology (2)

Which printer [a] LPT1 or [b] LPT2?: How many copies do you wish to print?: [P]rint now or [C]ancel?: Print all pages (y/n)?: a 2 y C

Graphics and event-driven programs Learning objectives identify and use some of the common components of the Java Swing package; program graphics components to handle mouse- click events; describe the role of layout managers; use the FlowLayout and BorderLayout managers; make use of compound containers.

Swing vs AWT

AWT Swing heavyweight lightweight

Swing Components..

The SmileyFace program…

public class RunSmileyFace { public static void main(String[ ] args) { new SmileyFace(); } }

JFrame SmileyFace is a kind of JFrame JFrame SmileyFace SmileyFace( ) paint( )

public class SmileyFace import java.awt.*; import javax.swing.*; { public SmileyFace() { // initialise the screen } public void paint(Graphics g) { // draw onto the screen } } extends JFrame

The SmileyFace constructor..

public SmileyFace() { setTitle("Smiley Face"); }

public SmileyFace() { setTitle("Smiley Face"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,220); setLocation(300,300); getContentPane().setBackground(Color.yellow); }

public SmileyFace() { setTitle("Smiley Face"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,220); setLocation(300,300); getContentPane().setBackground(Color.yellow); setVisible(true); }

The paint method..

public void paint(Graphics g) { super.paint(g); } g.drawOval(85,75,75,75); g.setColor(Color.blue); g.drawOval(100,95,10,10); g.drawOval(135,95,10,10); g.drawArc(102,115,40,25,0,-180); g.drawString("Smiley Face", 90,175); g.setColor(Color.red); Calls the paint method of the superclass - JFrame

public class RunSmileyFace { public static void main(String[ ] args) { new SmileyFace(); }

The ChangingFace class…

JButton boolean

import javax.swing.*; import java.awt.*; public class ChangingFace extends JFrame { public ChangingFace() { // code goes here } public void paint(Graphics g) { // code goes here } } private JButton happyButton = new JButton("Smile"); private JButton sadButton = new JButton("Frown"); private boolean isHappy = true;

The ChangingFace constructor..

public ChangingFace() { setTitle("Changing Face"); getContentPane().setBackground(Color.yellow); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250, 200); setLocation(300,300); setVisible(true); } setLayout(new FlowLayout()); add(happyButton); add(sadButton);

The paint method..

public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.drawOval(85,45,75,75); g.setColor(Color.blue); g.drawOval(100,65,10,10); g.drawOval(135,65,10,10); g.drawString("Changing Face", 80,155); } if(isHappy == true) { g.drawArc(102,85,40,25,0,-180); } else { g.drawArc(102,85,40,25,0,180); }

How do we get the program to monitor our buttons for mouse- click events ?

Give each button a listener!

public ChangingFace() { setTitle("Changing Face"); setLayout(new FlowLayout()); add(happyButton); add(sadButton); getContentPane().setBackground(Color.yellow); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250, 200); setLocation(300,300); setVisible(true); } happyButton sadButton.addActionListener( ); this.addActionListener( ); this code for responding to mouse clicks is in this class

import javax.swing.*; import java.awt.*; public class ChangingFace extends JFrame { public ChangingFace() { // code goes here } public void paint(Graphics g) { // code goes here } } private JButton happyButton = new JButton("Smile"); private JButton sadButton = new JButton("Frown"); private boolean isHappy = true; public void actionPerformed(ActionEvent e) { // code goes here } This method is where we write the code for responding to button clicks

import javax.swing.*; import java.awt.*; public class ChangingFace extends JFrame { public ChangingFace() { // code goes here } public void paint(Graphics g) { // code goes here } } private JButton happyButton = new JButton("Smile"); private JButton sadButton = new JButton("Frown"); private boolean isHappy = true; public void actionPerformed(ActionEvent e) { // code goes here } implements ActionListener import java.awt.event.*; This special kind of Class acts a bit like a contract.

import javax.swing.*; import java.awt.*; public class ChangingFace extends JFrame { public ChangingFace() { // code goes here } public void paint(Graphics g) { // code goes here } } private JButton happyButton = new JButton("Smile"); private JButton sadButton = new JButton("Frown"); private boolean isHappy = true; public void actionPerformed(ActionEvent e) { // code goes here } implements ActionListener import java.awt.event.*; This key word indicates that we have met that contract.

import javax.swing.*; import java.awt.*; public class ChangingFace extends JFrame { public ChangingFace() { // code goes here } public void paint(Graphics g) { // code goes here } } private JButton happyButton = new JButton("Smile"); private JButton sadButton = new JButton("Frown"); private boolean isHappy = true; public void actionPerformed(ActionEvent e) { // code goes here } implements ActionListener import java.awt.event.*;

public void actionPerformed(ActionEvent e) { if(e.getSource() == happyButton) { isHappy = true; repaint(); } if(e.getSource() == sadButton) { isHappy = false; repaint(); } }

public class RunChangingFace { public static void main(String[] args) { new ChangingFace(); } } Running the application

Another interactive graphics class…………

JFrame JTextField JButton JLabel

import javax.swing.*; import java.awt.event.*; import java.awt.*; public class PushMe extends JFrame implements ActionListener { private JTextField myTextField = new JTextField(15); private JButton myButton = new JButton("please push me"); private JLabel myLabel = new JLabel("Enter some text and push the button", JLabel.CENTER); public PushMe() { // code goes here } public void actionPerformed(ActionEvent e) { // code goes here } }

public PushMe() { setTitle("Push Me"); setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(220,120); setLocation(400, 300); add(myTextField); add(myButton); add(myLabel); myButton.addActionListener(this); setVisible(true); }

public void actionPerformed(ActionEvent e) { } String myText; myText = myTextField.getText(); myLabel.setText("You entered: " + myText); myTextField myLabel

A GUI for the Oblong class …

JTextArea JButton JLabel JTextField JLabel JTextField

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class OblongGUI extends JFrame implements ActionListener { private Oblong myOblong = new Oblong(0,0); private JLabel lengthLabel = new JLabel("Length"); private JTextField lengthField = new JTextField(5); private JLabel heightLabel = new JLabel("Height"); private JTextField heightField = new JTextField(5); private JButton calcButton = new JButton("Calculate"); private JTextArea displayArea = new JTextArea(2,20); // methods go here }

public OblongGUI() { setTitle("Oblong GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); setSize(240, 135); setLocation(300,300); add(lengthLabel); add(lengthField); add(heightLabel); add(heightField); add(calcButton); add(displayArea); calcButton.addActionListener(this); setVisible(true); }

public void actionPerformed(ActionEvent e) { String lengthEntered = lengthField.getText(); String heightEntered = heightField.getText(); if(lengthEntered.length() == 0 || heightEntered.length() == 0) { displayArea.setText("Length and height must be entered"); } else { myOblong.setLength( Double.parseDouble(lengthEntered) ); myOblong.setHeight( Double.parseDouble(heightEntered) ); displayArea.setText("The area of the oblong is " + myOblong.calculateArea() + "\n" + "The perimeter of the oblong is " + myOblong.calculatePerimeter()); } } }

A metric converter

JFrame JPanel inchCmPanel mileKmPanel poundKgPanel

Layout policies - FlowLayout

Layout policies - BorderLayout

Compound containers

JPanel inchCmPanel inchCmButtons inchCmButtons.setLayout(new BorderLayout()); inchCmButtons.add("North", cmToInchButton); inchCmButtons.add("South", inchToCmButton); inchCmPanel.add(cmText); inchCmPanel.add(cmLabel); inchCmPanel.add(inchCmButtons); inchCmPanel.add(inchText); inchCmPanel.add(inchLabel);

Practical Work

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ChangingFace extends JFrame implements ActionListener { private boolean isHappy = true; private JButton happyButton = new JButton("Smile"); private JButton sadButton = new JButton("Frown"); public ChangingFace() // code goes here public void paint(Graphics g) // code goes here public void actionPerformed (ActionEvent e) // code goes here } Face now has three moods declare an extra Jbutton Add extra button and give it a listener Check mood and paint frown, smile or think Process three button clicks So try changing to an int type (1 = happy, 2 = sad, 3 = frown?)