Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Graphic User Interfaces Layout Managers Event Handling.
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.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling Events and Listeners Timers and Animation.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Java Programming Chapter 10 Graphical User Interfaces.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
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.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Layout Managers Arranges and lays out the GUI components on a container.
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 Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
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.
1 Lecture 8: User Interface Components with Swing.
Lesson 33: Layout management and drawing – Java GUI.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
A First Look at GUI Applications
Computer Science 209 Graphics and GUIs.
Ellen Walker Hiram College
Introduction to Computing Using Java
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Graphic User Interface

Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you can perform on a computers ­Mouse click, buttons, menus etc.. Java has a set of built in tools that enable the creation of Graphic User Interfaces. Java uses two main packages: 1.Abstract Windows Toolkit –awt 2.Java Foundation Classes called Swing 3.The event handler from the awt

Using GUIs in Java Step 1: Import the swing and awt packages. import javax.swing.*: import java.awt.*; Import java.awt.event; The “.* “ imports all the files associated with the package. Step 2: Work with the classes and components of the package.

Swing Components When you use a Swing component, you work with objects of that component’s class. You create the component by calling its constructor and then calling methods of the component.. All Swing components are subclasses of the abstract class Jcomponent Before components can be displayed in a user interface, they must be added to a container component. The container component is a window called a frame which belongs to the Jframe class.

Creating a Swing App Make the interface a subclass of Jframe public class FirstFrame extends JFrame {... The constructor of the class should handle the following tasks: Call a superclass constructor to give the frame a title and handle other setup procedures. Set the size of the frame’s window, either by specifying the width and height in pixels or by letting Swing choose the right size. Decide what to do if a user closes the window. Display the frame The constructor of the JFrame class: JFrame()

Simple frame import javax.swing.*; import java.awt.*; class FirstFrame extends JFrame { public FirstFrame() { setTitle("Messages Please"); setSize(400,100); setVisible(true); } public static void main(String[] args) { new FirstFrame(); } Libraries Size of Frame Jframe Class Title of Frame call

Drawing Text Example 1,13.1 import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Container; import javax.swing.JComponent; import java.awt.Graphics; class GUIGreet { public static void main (String[] args) { JFrame frame = new JFrame("Message Please"); frame.setSize(400,100); JPanel pane = (JPanel) frame.getContentPane(); pane.add(new Greeting()); frame.setVisible(true); } class Greeting extends JComponent { public Greeting () { repaint(); } public void paint (Graphics g) { g.drawString("Hello, world",150,50); } Classes from Packages Jframe Class Main Method Window Size Method Content Manager Method Greeting Object Make frame visible inheritance Paint method constructor Drawing text method

What does it all mean? Importing classes from swing and awt packages Main method always must be included in this case it will set up a window to display the output of the program Constructing the top level holder or container JFrame and name in frame import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Container; import javax.swing.JComponent; import java.awt.Graphics public static void main (String[] args) JFrame frame = new JFrame("Message Please")

The setSize method creates the dimensions of the window (width, height) in pixels getContentPane() method manages the contents inside the Frame. Returns reference to JFrame via JPanel a subclass of Jframe Construct a new Greeting object using the add method Make Frame visible Greeting inherits all of JComponent’s methods frame.setSize(400,100); JPanel pane = (JPanel) frame.getContentPane(); pane.add(new Greeting()); frame.setVisible(true); class Greeting extends JComponent

Create constructor of class Greeting to call paint method Paint method to be able to “paint” or fill frame with images and text. The parameter of the method is a Graphics object that is created by Java and assigned to object g which stores the paint properties (i.e. color) Property of what will be drawn in the frame Text “Hello World” which is 150 px from the left and 50 px from the top public Greeting () { repaint(); } public void paint (Graphics g) g.drawString("Hello, world",150,50);

Layout Managers The position and size of the components in a frame can be controlled by a layout manager The objects are constructed form the class FlowLayout in the package java.awt To construct a flow layout use the object called pane from Jpanel pane.setLayout(new FlowLayout()); Example 1 Section 13.3 uses the Flowlayout with buttons

Border Layout To create a structure with different regions use BorderLayout Use a version of the overloaded method add with two parameters In Example 2 section 13.3 they use JButton and BorderLayout This type of layout creates moveable regions

Grid Layout This type of layout enables the creation of rows and columns to create a table or grid This uses the class GridLayout with two parameters indicating the row and column Pane.setLayout(new GridLayout(3,2)); 3 rows, 2 columns Example 3 in section 13.3 provides an example Example 5 in section 13.3 combines the layout components with graphical displays

Button Events Buttons enable interactions with the user When a button is pressed, an action event occurs The button object is the source of the action event If you want to know if the action occurred you need to register as a listener Objects that can produce events maintain a list of listeners Explained in the following example (Example 1, 13.4)

Button Event Text Example 1, 13.4 import java.awt.*; import java.awt.event.*; import java.swing.*; class ButtonColour{ public static void main (String[] args) { ColourFrame frame = new ColourFrame(); } class ColourFrame extends JFrame implements Action Listener{ private Jpanel pane; public ColourFrame (){ super(“Button Events”); Jbutton colourButton=new Jbutton(“Change Colour”); colourButton.addActionListener(this); pane=(Jpanel) getContentPnae(); pane.setLayout(new FlowLayout()); pane.add(colourButton); setSize(300,100); setVisible(trure); } Packages ColourFrame constructor Call constructor Jframe with title “Button Events Create Button and Listeners Sets size and makes it visible Set Listener Object Creation ColourFrame Class Create content pane and add buttons

Button Event Text Example 1, 13.4 public void actionPerformed(ActionEvent e){ pane.setBackground(new Color(strength(),strength(),strength())) } public static int strength(){ return(int)(256*Math.random()); } Automatic call of pressed button Method generates a random value in the range of 0 to 255 which controls the colour of the background component 3 To generate more colours Selection structures can be used on action events (Example 2, 13.4)

Mouse Events Mouse events are handled in the same way as Button events – with more activities Two kinds of listener interfaces ­MouseMotionListener ­MouseListener MouseMotionListener requires implementation of the following 3 methods ­ mouseMoved - called when moving a mouse ­ mouseDragged - called when mouse is dragged over more than one component

Mouse Listener Events MouseListener requires implementation of the following 5 methods ­ mousePressed - called when mouse is pressed ­ mouseReleased -called when mouse is released or let go ­ mouseClicked -called when mouse is pressed & released quickly ­ mouseEntered -called when mouse enters area of interest ­ mouseExited -called when mouse exists area of interest

Mouse Event instance class Mouse event object e provide two instance methods ­ int getX() – method returns horizontal coordinate of the mouse when event occurred ­ int getY() – method returns vertical coordinate of the mouse when event occurred

Using Mouse Events Example 1, 13.5 class CircleFrame extends Jframe implements MouseListener{ private int x; private iny y; private static final int RADIUS=10; public CircleFrame(){ super(“Mouse Droppings”); addMouseListener(this); setSize(300,100); setVisible(true); } public void mousePressed (MouseEvent e){ x=e.getX() y=e.getY() } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } Public void paint(Graphics g){ g.fillOval(x-RADIUS,y-RADIUS,2*RADIUS,2*RADIUS); }} Region must listen to events occurring within itself coordinates of the centres of the circle CircleFrame class implements MouseListener methods sets size of circle Obtains coordinates of the location of any mouse click and asks Java to paint to implement methods Displaying the graphic

The Adapter class The adapter class includes the MouseListener methods for simple versions ­Implementation of MouseListener is done in MouseAdaptor ­Constructor of CircleFrame calls on the constructor of the ClickHandler class ­ClikcHandler class creates object Puts object on the mouse listener’s list of objects of events. class ClickHandler()extendsMouseAdapter{ public void mouseClicked (MouseEvent e){ x=e.getX(); y=e.getY(); repaint(); }

Text Input and Output Use the JTextField class: JTextField inField= new JTextField(20); Creates a component that holds 20 characters JTextField outField= new JTextField(“result”,10); outField.setEditable(false); Creates a field to display output only Example 7, 13.6 provides more details