Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.

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

1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
A Simple Applet.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
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 CheckWriter Application Introducing Graphics and Printing.
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.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Drawing and Filling Geometric Shapes. Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Java Applets.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Java Applets.
Chapter 12 GUI Basics.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II

© 2006 Pearson Addison-Wesley. All rights reserved19-2 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

© 2006 Pearson Addison-Wesley. All rights reserved19-3 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

© 2006 Pearson Addison-Wesley. All rights reserved19-4 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)

© 2006 Pearson Addison-Wesley. All rights reserved19-5 Adding Color

© 2006 Pearson Addison-Wesley. All rights reserved19-6 Defining Colors Standard colors in the class Color are already defined –These are listed in Display 17.5 in Chapter 17, and shown on 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

© 2006 Pearson Addison-Wesley. All rights reserved19-7 The Color Constants

© 2006 Pearson Addison-Wesley. All rights reserved19-8 Defining Colors 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);

© 2006 Pearson Addison-Wesley. All rights reserved19-9 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 reserved19-10 Some Methods in the Class Color (Part 1 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved19-11 Some Methods in the Class Color (Part 2 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved19-12 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);

© 2006 Pearson Addison-Wesley. All rights reserved19-13 JColorChooser Dialog (Part 1 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-14 JColorChooser Dialog (Part 2 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-15 JColorChooser Dialog (Part 3 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-16 JColorChooser Dialog (Part 4 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-17 JColorChooser Dialog (Part 5 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-18 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);

© 2006 Pearson Addison-Wesley. All rights reserved19-19 Using drawString (Part 1 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-20 Using drawString (Part 2 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-21 Using drawString (Part 3 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-22 Using drawString (Part 4 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-23 Using drawString (Part 5 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-24 Using drawString (Part 6 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-25 Using drawString (Part 7 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-26 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);

© 2006 Pearson Addison-Wesley. All rights reserved19-27 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

© 2006 Pearson Addison-Wesley. All rights reserved19-28 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

© 2006 Pearson Addison-Wesley. All rights reserved19-29 Result of Running FontDisplay.java (Found on the Accompanying CD)

© 2006 Pearson Addison-Wesley. All rights reserved19-30 Some Methods and Constants for the Class Font (Part 1 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved19-31 Some Methods and Constants for the Class Font (Part 2 of 2)