Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
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.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Swing II.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
XP 1 Microsoft Word 2002 Tutorial 1 – Creating a Document.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Swing II. Swing II Topics WindowListener interface WindowListener interface Icons Icons Scrollbars Scrollbars The Graphics class The Graphics class Colors.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Basics of GUI Programming Chapter 11 and Chapter 22.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
Chapter 141 More Swing Chapter Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the.
Chapter 14- More Swing, Better looking applications.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Chapter 14 Applets. Applets: Introduction Java programs are divided into two main categories, applets and applications An application is an ordinary Java.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Java Applets.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Java Applets.
Graphical User Interface (GUI) Programming III
Chapter 13: Advanced GUIs and Graphics
Java Applets.
Chapter 12 GUI Basics.
Advanced GUIs and Graphics
Presentation transcript:

Graphical User Interface (GUI) Programming IV

Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars in GUI components Changing visibility of GUI components How to update a GUI Using colors in Java Using fonts in Java

Icons JLabels, JButtons, and JMenuItems can have icons  An icon is just a small picture (usually)  It is not required to be small An icon is an object of the ImageIcon class  It is based on a digital picture file such as.gif,.jpg, or.tiff Labels, buttons, and menu items may display a string, an icon, a string and an icon, or nothing

The class ImageIcon is used to convert a picture file to a Swing icon ImageIcon dukeIcon = new ImageIcon("duke_waving.gif");  The picture file must be in the same directory as the class in which this code appears, unless a complete or relative path name is given.  Note that the name of the picture file is given as a string. Icons (Cont’d)

An icon can be added to a label using the setIcon method as follows: JLabel dukeLabel = new JLabel("Mood check"); dukeLabel.setIcon(dukeIcon); Instead, an icon can be given as an argument to the JLabel constructor: JLabel dukeLabel = new JLabel(dukeIcon); Text can be added to the label as well using the setText method: dukeLabel.setText("Mood check"); Icons (Cont’d)

Icons and text may be added to JButtons and JMenuItems in the same way as they are added to a Jlabel JButton happyButton = new JButton("Happy"); ImageIcon happyIcon = new ImageIcon("smiley.gif"); happyButton.setIcon(happyIcon); Icons (Cont’d)

Button or menu items can be created with just an icon by giving the ImageIcon object as an argument to the JButton or JMenuItem constructor ImageIcon happyIcon = new ImageIcon("smiley.gif"); JButton smileButton = new JButton(happyIcon); JMenuItem happyChoice = new JMenuItem(happyIcon);  A button or menu item created without text should use the setActionCommand method to explicitly set the action command, since there is no string. Icons (Cont’d)

Using Icons: An Example

Using Icons: An Example (Cont’d)

Insets Class Objects of the class Insets are used to specify the size of the margin in a button or menu item  The arguments given when an Insets class object is created are in pixels  The Insets class is in the package java.awt: public Insets(int top, int left, int bottom, int right)

Scroll Bars When a text area is created, the number of lines that are visible and the number of characters per line are specified as follows: JTextArea memoDisplay = new JTextArea(15, 30); However, it would often be better not to have to set a firm limit on the number of lines or the number of characters per line  This can be done by using scroll bars with the text area

When using scroll bars, the text is viewed through a view port that shows only part of the text at a time  A different part of the text may be viewed by using the scroll bars placed along the side and bottom of the view port Scroll bars can be added to text areas using the JScrollPane class  The JScrollPane class is in the javax.swing package  An object of the class JScrollPane is like a view port with scroll bars Scroll Bars (Cont’d)

View Port for a Text Area

When a JScrollPane is created, the text area to be viewed is given as an argument: JScrollPane scrolledText = new JScrollPane(memoDisplay); The JScrollPane can then be added to a container, such as a JPanel or JFrame textPanel.add(scrolledText); Scroll Bars (Cont’d)

The scroll bar policies can be set as follows: scrolledText.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrolledText.setVerticalScrollBarPolicy( JscrollPane.VERTICAL_SCROLLBAR_ALWAYS); If invocations of these methods are omitted, then the scroll bars will be visible only when needed  If all the text fits in the view port, then no scroll bars will be visible  If enough text is added, the scroll bars will appear automatically Scroll Bars (Cont’d)

A Text Area with Scroll Bars: An Example

A Text Area with Scroll Bars: An Example (Cont’d)

Program Output:

Components with Changing Visibility A GUI can have components that change from visible to invisible and back again In the following example, the label with the character Duke not waving is shown first  When the " Wave " button is clicked, the label with Duke not waving disappears and the label with Duke waving appears  When the " Stop " button is clicked, the label with Duke waving disappears, and the label with Duke not waving returns  Duke is Sun Microsystem's mascot for the Java Language A component can be made invisible without making the entire GUI invisible

Components with Changing Visibility: An Example

Components with Changing Visibility: An Example (Cont’d)

Some More Details on Updating a GUI With Swing, most changes to a GUI are updated automatically to become visible on the screen  This is done by the repaint manager object Although the repaint manager works automatically, there are a few updates that it does not perform  For example, the ones taken care of by validate or repaint One other updating method is pack  pack resizes the window to something known as the preferred size

The validate Method An invocation of validate causes a container to lay out its components again  It is a kind of "update" method that makes changes in the components shown on the screen  Every container class has the validate method, which has no arguments Many simple changes made to a Swing GUI happen automatically, while others require an invocation of validate or some other "update" method  When in doubt, it will do no harm to invoke validate

Specifying a Drawing Color Using the method drawLine inside the paint method is similar to drawing with a pen that can change colors  The method setColor will change the color of the pen  The color specified can be changed later on with another invocation of setColor so that a single drawing can have multiple colors g.setColor(Color.BLUE)

Defining Colors Standard colors in the class Color are already defined  These are listed in the following slide The Color class can also be used to define additional colors  It uses the RGB color system in which different amounts of red, green, and blue light are used to produce any color

Defining Colors (Cont’d)

Integers or floats may be used when specifying the amount of red, green, and/or blue in a color  Integers must be in the range inclusive: Color brown = new Color(200, 150, 0);  float values must be in the range inclusive: Color brown = new Color((float)(200.0/255), (float)(150.0/255), (float)0.0); Defining Colors (Cont’d)

Pitfall: Using double s to Define a Color Constructors for the class Color only accept arguments of type int or float  Without a cast, numbers like 200.0/255, 0.5, and 0.0 are considered to be of type double, not of type float Don't forget to use a type cast when intending to use float numbers  Note that these numbers should be replaced by defined constants in any final code produced public static final float RED_VALUE = (float)0.5;

© 2006 Pearson Addison-Wesley. All rights reserved The JColorChooser Dialog Window The class JColorChooser can be used to allow a user to choose a color The showDialog method of JColorChooser produces a color-choosing window  The user can choose a color by selecting RGB values or from a set of color samples sample Color = JColorChooser.showDialog(this, "JColorChooser", sampleColor);

JColorChooser Dialog: An Example

JColorChooser Dialog: An Example (Cont’d)

The drawString Method The method drawString is similar to the drawing methods in the Graphics class  However, it displays text instead of a drawing  If no font is specified, a default font is used g.drawString(theText, X_START, Y_Start);

The drawString Method (Cont’d)

Resulting GUI: After clicking the buttonResulting GUI: Start view

Fonts A font is an object of the Font class  The Font class is found in the java.awt package The constructor for the Font class creates a font in a given style and size Font fontObject = new Font("SansSerif“, Font.PLAIN, POINT_SIZE); A program can set the font for the drawString method within the paint method g.setFont(fontObject);

Font Types Any font currently available on a system can be used in Java  However, Java guarantees that at least three fonts will be available: "Monospaced", "SansSerif", and "Serif" Serifs are small lines that finish off the ends of the lines in letters:  This S has serifs, but this S does not  A "Serif" font will always have serifs  Sans means without, so the "SansSerif" font will not have serifs  "Monospaced" means that all the characters have equal width

Font Styles Fonts can be given style modifiers, such as bold or italic  Multiple styles can be specified by connecting them with the | symbol (called the bitwise OR symbol) new Font("Serif“, Font.BOLD|Font.ITALIC, POINT_SIZE); The size of a font is called its point size  Character sizes are specified in units known as points  One point is 1/72 of an inch

FontDisplay.java: Program Output