Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
User Interfaces II GUI – Awt, Swing, Web
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
OLD BUSINESS : Lets talk about the next exam Project proposals – Functional Definitions Go over homework – NEW BUSINESS: Chapter 4 today GUIs Assignment.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Graphical User Interfaces
JAVA1 1. JFrame. JAVA2 JApplet import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HerhalingAppletH10 extends JApplet implements.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
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.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces Java’s AWT and Swing APIs.
Graphical User Interfaces (Part IV)
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Drawing in a frame – Java GUI
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
F27SB2 Programming Languages
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Graphical User Interfaces, 2D Graphics & Game Programming.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
JAVA 程式語言入門 (II).  版面配置  事件驅動  Ch14_01.java 1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_01 4. { 5. public static void main(String.
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.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Övning 5. Repetition klasser class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
© 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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
עקרונות תכנות מונחה עצמים תרגול 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,
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
A simple swing example GETTING STARTED WITH WIND CHILL.
Review_6 AWT, Swing, ActionListener, and Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event-driven programming for GUI
Introduction to Event Handling
Chapter 5 Processing Input with Applets
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071

Constructing a GUI Consider the following problem: Construct a GUI as follows: The purpose of the GUI is to calculate the body mass index (BMI) of a person. The formula to calculate the BMI is BMI = weight(in kg)/height 2 (in metres)

Solution – Components in the GUI Here we find that the GUI has the following components: The layout of the components is Flow Layout LabelsText fields Button

Solution – Code for the GUI 1. import java.awt.*; 2. import java.awt.event.*; 3. import javax.swing.*; 4. public class BMICalc extends JFrame implements ActionListener 5. { 6. private JTextField wtt, htt; 7. private JButton convert; 8. private JLabel wtl, htl, rsll; 9. public BMICalc() 10. { 11. super("Body Mass Index Calculator"); 12. setSize(500, 70); 13. Container cp = getContentPane(); 14. cp.setLayout(new FlowLayout()); 15.

Solution – Code for the GUI (contd) 16. wtt = new JTextField(5); 17. htt = new JTextField(5); 18. wtl = new JLabel("Weight (kg)"); 19. htl = new JLabel("Height (m)"); 20. rsll = new JLabel("Your BMI is:"); 21. convert = new JButton("Calculate"); 22. cp.add(wtl); 23. cp.add(wtt); 24. cp.add(htl); 25. cp.add(htt); 26. cp.add(convert); 27. cp.add(rsll); 28. convert.addActionListener(this); 29. show(); 30. }

Solution – Action Listener 31. public void actionPerformed(ActionEvent e) 32. { 33. double weight = Double.parseDouble(wtt.getText()); 34. double height = Double.parseDouble(htt.getText()); 35. double bmi = weight/(height*height); 36. double bmirounded = ((int) (bmi*1000))/1000.0; 37. rsll.setText("Your BMI is: " + bmirounded); 38. wtt.setText(""); 39. htt.setText(""); 40. } public static void main(String[] args) 43. { 44. new BMICalc(); 45. } 46. }

Problem – Using Layout Managers Construct a GUI that divides a frame into appropriate areas as shown in the following GUI. Use Border and Grid Layouts.

Solution Solution in attached zip file. Note in the solution that: Grid Layout Flow Layout Border Layout

Problem – Drawing Shapes Write a program that constructs the following figure along with associated labels.

Solution public class PieChart extends Frame { public PieChart() { super("Simple Pie Chart Drawing"); setSize(300, 300); setResizable(false); show(); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setFont(new Font("Arial", Font.ITALIC, 12)); Arc2D.Double slice1 = new Arc2D.Double(50, 50, 150, 150, 0, 90, 2); g2.setColor(Color.red); g2.draw(slice1); g2.fill(slice1); g2.drawString("25% Revenue", 170, 60);

Solution – contd. Arc2D.Double slice2 = new Arc2D.Double(50, 50, 150, 150, 90, 45, 2); g2.setColor(Color.blue); g2.draw(slice2); g2.fill(slice2); g2.drawString("12.5% Sales", 40, 50); Arc2D.Double slice3 = new Arc2D.Double(50, 50, 150, 150, 135, 135, 2); g2.setColor(Color.magenta); g2.draw(slice3); g2.fill(slice3); g2.drawString("37.5% Profits", 30, 220); Arc2D.Double slice4 = new Arc2D.Double(50, 50, 150, 150, 270, 90, 2); g2.setColor(Color.green); g2.draw(slice4); g2.fill(slice4); g2.drawString("25% Expenditures", 180, 200); } public static void main(String[] args) { PieChart p = new PieChart(); }