25 GUI Example.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 29 Organising Code: Packages & Archives.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
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.
Java Programming Chapter 10 Graphical User Interfaces.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Field Trip #19 Animations with Java By Keith Lynn.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
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.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
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.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Review_6 AWT, Swing, ActionListener, and Graphics.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
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
A First Look at GUI Applications
Java GUI.
GUIs Model/View/Controller Layouts
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
GUI AND GRAPHICS.
Ellen Walker Hiram College
GUI Programming III: Events
Chapter 13: Advanced GUIs and Graphics
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Timer class and inner classes
Graphical user interface-based programming
Java External Libraries & Case Study
Steps to Creating a GUI Interface
GUI Test Information.
Constructors, GUI’s(Using Swing) and ActionListner
Java Tutorial – Application Building
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Advanced GUIs and Graphics
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

25 GUI Example

Previously GUI Programming III: Events Delegation Event Model Event Object Event Listener Event Control Flow Registration Event Listener Interfaces Listener Methods Process Action Event Example

Overview Build GUI application than displays a switch and a light bulb. By clicking the switch we can turn the light bulb on and off

States Valid states The light build may be on or off The switch may be on or off To be able to see the switch in the dark (light off) the switch will be lighted when switch is off Switch without light when on as it can already be seen because the light is also on When the switch is on the light is also on When the switch is off the light is also off

Sketch Just draw a simple view of what will be the general GUI look Draw it in a paper Switch by Amadeo Ascó I O

States Valid states The light build may be off or on lightOn.gif lightOff.gif Valid states The light build may be off or on The switch may be off or on To be able to see the switch in the dark (light off) then the switch will be lighted when switch is off Switch without light when on as it can already be seen because the light is also on When the switch is on the light is also on When the switch is off the light is also off switchOff.gif switchOn.gif

Considerations The display can be obtained by Application draw the pictures from basic drawing operation Too much work Quality may not be very good Application uses external images The quality of the display depends of the quality of the images. Normally better than previous approach Can change the images without the need of changing the code Can use already existing functionality in swing

Considerations Switch The switch IS a button Can be clicked, like a JButton On click executes an operation, like a JButton Changes the image on click, like a JButton We may need specific functionality: The look will be just the image no borders as normal buttons so override it Because IS a button we extend JButton; use extends

Considerations Switch These mean a new class that inherits (extends) JButton SwitchImg

Considerations Light The light does not have click functionality, it is not a button Need to draw the light at the middle of the rest of the area (area left by the switch) JPanel allows to create an area where to draw JPanel draws a rectangular area with an specified colour, the background colour The light IS NOT a panel but it is contained by our special panel

Considerations Light Our panel HAS an image (on the centre); image(s) are members of our panel JPanel Does not provide capabilities to hold image(s) so we need to add it Does not draw image(s) so we have to provide it Override method void paintComponent(Graphics g) Graphics has capabilities to draw images; drawImage(...) These mean a new class that inherits (extends) JPanel ImgPanel

Considerations Base GUI We have the content of our GUI We need a base with all standard GUI functionality; new class Logo Minimise, maximise and close buttons Display area Our GUI HAS a switch and a light Witch means member variables

Considerations Base GUI A new class It IS a JFrame which means inherit (extends) from JFrame SwitchGUI To set the size of the GUI use the method void setPreferredSize(Dimension dim) The content of the GUI is in the content pane; obtainable by calling method Container getContentPane() Switch by Amadeo Ascó Content pane

setResizable(false); Considerations Base GUI No too much point to allow the user to change the size of the application GUI: setResizable(false); Change colour using void setBackground(Color) on the component of interest Need to synchronise switch with light by implementing the ActionListener

Considerations Entry Point We need a place to process the entered data from commend prompt, if any; we are writing an application Use a new class to contain all the functionality used to start the application SwitchMain

Position Layout managers are responsible to position each element in its parent control In this case useful ones maybe BorderLayout in package java.awt; position components in five regions - here more CardLayout in package java.awt; it treats each component in the container as a card - here more GridBagLayout in package java.awt; can be complex but very powerful - here more Many more than can be used - here more

We have not written any code yet!

Organize The project will contain the following directories (folders) bin contains the .classes of the project and .jar file src contains the .java of the project; the source code files doc contains the documentation like readme.txt file, other help file(s) doc\javadoc contains the javadoc documentation for the project images contains all the images to use - get them from here

Organize The project needs a meaningful name Project refers to switching on/off a light so several potential names Light Switch ... The source files package structure of the project should identify you (or company you work for) and identify the project (you cannot use the word switch): com.aasco.switches

Organize The classes It seem logic that the package to be called gui SwitchImg ImgPanel SwitchGUI are part of the GUI of the application so put them all together in the same package It seem logic that the package to be called gui com.aasco.switches.gui

Organize The class SwitchMain refers to the entry point for the application so create it in the base package; com.aasco.switches

Write Code Now we can start by writing the code Create the classes Make sure that their inheritance is correct (extends) - IS Add member variables – HAS Add functionality; new or override package com.aasco.switches; public class SwitchMain { public static void main(String[] astrArgs) { new SwitchGUI(); } // main() } // end class SwitchMain

Try to implement it in the labs! What we expect Try to implement it in the labs!

Remember Remember to add Comments to your code Documentation comments Build the javadoc documentation When creating the JAR file you must also select the images directory as the images must be contained in the final application To get the javadoc run the steps as specified in the website To build the final ZIP file follow the steps shown in the website