Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Similar documents
Graphics and Painting

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

Java Programming Lecture 6

Programming graphics

Graphical User Interface (GUI)

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Graphics and Java2D. Objectives

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Chapter 12 GUI Basics

Introduction to the JAVA UI classes Advanced HCI IAT351

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

CS 251 Intermediate Programming GUIs: Components and Layout

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphical User Interface (GUI)

G51PRG: Introduction to Programming Second semester Applets and graphics

COMPSCI 230. Software Design and Construction. Swing

Chapter 12 Advanced GUIs and Graphics

Lecture 5: Java Graphics

Dr. Hikmat A. M. AbdelJaber

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

IT101. Graphical User Interface

CSC207H: Software Design Lecture 11

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Computer Science 210: Data Structures. Intro to Java Graphics

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

1.00 Lecture 14. Lecture Preview

Class 14: Introduction to the Swing Toolkit

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Lecture 3: Java Graphics & Events

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Class 16: The Swing Event Model

Swing. By Iqtidar Ali

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interfaces. Comp 152

Part 3: Graphical User Interface (GUI) & Java Applets

Sri Vidya College of Engineering & Technology

Graphical Applications

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

BASICS OF GRAPHICAL APPS

Assignment 2. Application Development

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

CS111: PROGRAMMING LANGUAGE II

Graphic Interface Programming II Using Swing and OOP more advanced issues. Uppsala universitet

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

2110: GUIS: Graphical User Interfaces

Java - Applets. public class Buttons extends Applet implements ActionListener

Graphics in Swing. Engineering 5895 Faculty of Engineering & Applied Science Memorial University of Newfoundland

Lecture 18 Java Graphics and GUIs

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

Unit 7: Event driven programming

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

2IS45 Programming

Java IDE Programming-I

Some classes and interfaces used in this chapter from Java s original graphics capabilities and from the Java2D API.

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Window Interfaces Using Swing Objects

Programming Mobile Devices J2SE GUI

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Swing from A to Z Some Simple Components. Preface

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Chapter 6: Graphical User Interfaces

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

User interfaces and Swing

UI Software Organization

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

CompSci 230 S Programming Techniques. Basic GUI Components

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming

Programmierpraktikum

PIC 20A GUI with swing

Here is a list of a few of the components located in the AWT and Swing packages:

Widgets. Widgets Widget Toolkits. User Interface Widget

Contents Chapter 1 Introduction to Programming and the Java Language

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming

Programming Languages and Techniques (CIS120)

Window Interfaces Using Swing Objects

CSE 331 Software Design & Implementation

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Basics of programming 3. Java GUI and SWING

Human-Computer Interaction IS4300

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Java TM Applets. Rex Jaeschke

Transcription:

Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017

Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple GUI applications For different platforms, AWT components mapped to platform-specific components. Prone to platform specific bugs. Primary package: java.awt. See other awt package APIs on docs.oracle.com/javase/8/docs/api/ When Java 2 was released, a library known as the Swing components were introduced with the idea of replacing the older AWT user-interface components (like Button, TextField, TextArea, etc). less dependent on target platform a more robust and flexible library Primary package: javax.swing Since release of Java 2, the Swing components are recommended for building graphic user interfaces for later browsers

Java Graphics API (Using Swing Components) Component: A base class for all non-menu graphic user interface classes. Container: a base class for container classes. A container is used to group smaller components. The most important containers are: JApplet - for holding Applets JFrame - for holding GUI components in applications. A window that is on the outer level (not inside another window) JPanel - invisible container holding user-interface components. Can be nested, and can be used as canvases for drawing graphics. JDialog - for creating dialog boxes (usually temporary popup messages or dialogs for receiving additional info.

JComponent Base class for all of the lightweight Swing components, which are graphical items places on the canvases or containers. Its subclasses are the basic elements for constructing GUIs. Here are just a few of the more common elements: JButton - for creating push buttons JCheckBox - for creating toggle checkboxes JMenu - for pop-up menus JRadioButton - for radio buttons (made into a group, only one can be selected) JLabel - a display area for a short string or image JList - a component allowing the user to select from a list JOptionPane - a component allowing the user to pop up an easy dialog box as an information message or for user input JTextField - component allowing an editable line of text JTextArea - multi-line area for displaying text

Helper classes Helper classes - used by components and containers to control drawing and placing of objects. Some important helper classes (from package java.awt): Graphics - abstract class. Provides graphical context for drawing Color - used for specifying colors in components and drawings Font - specify fonts used in Graphics drawings FontMetrics - abstract class. Encapsulates information and properties about the rendering of a font on screen Dimension - encapsulates width and height of a component in an object

Events Event: A signal that something has happened in a program. Examples: Button clicks, mouse movements, menu selections GUI programs generally driven by events, rather than a specific procedural order vents are handled with event objects. These are triggered by actions on source objects (components or objects on which the event is generated), and they must implement corresponding event listener interfaces. The listener listens for the event, and invokes an event handler when event occurs. java.util.eventobject: Base class for event classes in Java

Some examples of event types: These are just a few examples, not a comprehensive list. ActionEvent - clicking a button, pressing return on a text field ItemEvent - clicking a check box, selecting an item WindowEvent - Closing a window, opening a window ContainerEvent - component added to a container ComponentEvent - resizing a component, hiding a component TextEvent - changing a text value MouseEvent - clicking the mouse, dragging the mouse KeyEvent - pressing a key on the keyboard

paint() methods Lightweight Swing components that extend class JComponent have a method called paintcomponent, with this prototype: public void paintcomponent(graphics g) Another similar method is the paint method in class Component (and thus all its children) with this prototype: public void paint(graphics g) The JComponent version of paint() actually delegates its work to three methods: paintcomponent, paintborder, and paintchildren

paint() methods The idea behind paint() is that they are invoked for any component automatically whenever that component needs to be drawn or re-drawn. Some examples of triggering events: When the component first is placed on the application. When the component is resized. When the component is covered by some other application, then uncovered and comes to the forefront again. Since this is triggered by such events, the programmer seldom needs to call paint() or paintcomponent() explicitly. The programmer can call repaint() (also a Component method) to force the paint operation, if the need arises (i.e. some situation not covered by the automatic calls to paint().

More on paint() These methods both take as a parameter a reference variable of type Graphics which is an abstract class. The object will be a subtype that handles the drawing context for the given platform. For Swing components, it is usually sufficient to just define paintcomponent() for drawing aspects, unless you want to control the other parts (border, children) as well. So, what can we DO in the paint() or paintcomponent() methods? Pretty much anything that s available in the Graphics class, and then some.

class Graphics and other useful helper classes The Graphics Class Helps manage drawing on the screen for GUI applications and applets. Keeps track of state information like current font, current color, the Component object being drawn on, and more. Has methods for drawing various kinds of shapes (lines, ovals, polygons, rectangles, etc) as well as strings. Also has methods for setting the font, the color, the current clipping area, the paint mode, and other status information.

The Color Class Used for specifying colors in components and drawings. Colors stored and specified with RGB (Red Green Blue) values. RGB values can be specified with ints (0-255) or floats (0.0-1.0). Color constants exist for common colors (Color.BLUE, Color.GREEN, etc). To find out or set the current drawing color, use the Graphics methods getcolor() and setcolor(). Example: g.setcolor(color.magenta); g.setcolor(new Color(255, 128, 3)); //RGB values JColorChooser - a javax.swing component that enables application users to choose colors.

The Font Class Specify fonts used in Graphics drawings. Physical fonts are actual fonts on a system these depend on platform and what fonts are installed on a system. Logical fonts are the 5 font families supported in Java: Serif, Sans Serif, Monospaced, Dialog, and DialogInput. When using logical fonts, an appropriate font on the given system will be chosen. Font constructor takes three parameters: font name, font style, font size Font name can be physical or logical. Font styles are plain, italics, or bold. Font size measured in points.

Font Examples To set or find out the current drawing font, use the Graphics methods getfont() and setfont(). Example: Font f = g.getfont(); // retrieve current font g.setfont(new Font("Serif", Font.ITALICS, 12)); Other methods available in class Font to set or retrieve properties for a Font object.

The FontMetrics class Abstract class. Encapsulates information and properties about the rendering of a font on screen. Helps track more specific font information like height, descent, ascent, and leading (interline spacing). Graphics class has a couple of methods named getfontmetrics(): FontMetrics m1, m2; m1 = g.getfontmetrics(); //current font info m2 = g.getfontmetrics(f1); //info about font f1

The Polygon Class Helper class for representing information about Polygons. Stores a list of (x,y) coordinate pairs, representing vertices of a polygon. Several Graphics class methods are for drawing polygons - drawpolygon(), drawpolyline, fillpolygon. There are versions of these last two that take a Polygon object as a parameter.

Java2D The Java2D API provides advanced graphics capabilities, for more detailed and complex two-dimensional drawing. Allows more complex drawing, like lines of varying thickness, filling shapes with colors and patterns, drawing dashed lines, composite overlapping text and graphics, gradients and textures, and more. Need to use an instance of class Graphics2D, which is a subclass of class Graphics. Must cast the Graphics object in the paintcomponent() method into a Graphics2D reference when using: Graphics2D g2d = (Graphics2D) g; For more details, look up the class on the Oracle Java Documentation website.

Java2D Packages Java2D involves a variety of packages: java.awt java.awt.image java.awt.color java.awt.font java.awt.geom java.awt.print java.awt.image.renderable