Creating Graphics in Java

Post on 07-Jan-2016

27 views 0 download

description

Creating Graphics in Java. CSE301 University of Sunderland Harry R Erwin, PhD. Resources. Flanagan, David (2000). Java Foundation Classes in a Nutshell, O’Reilly. Darwin, Ian F. (2004). Java Cookbook, O’Reilly. Flanagan, David (2004). Java Examples in a Nutshell, O’Reilly. - PowerPoint PPT Presentation

Transcript of Creating Graphics in Java

Creating Graphics in Java

CSE301

University of Sunderland

Harry R Erwin, PhD

Resources

• Flanagan, David (2000). Java Foundation Classes in a Nutshell, O’Reilly.

• Darwin, Ian F. (2004). Java Cookbook, O’Reilly.

• Flanagan, David (2004). Java Examples in a Nutshell, O’Reilly.

Java GUI Frameworks

• Abstract Window Toolkit (java.awt)– The original set of windowing components designed for Java.

These were implemented as separate processes using the GUI features of the underlying operating system.

• Swing (or Java Foundation Classes—javax.swing)– A second generation set of ‘light-weight’ windowing components

introduced in Java 1.2. These were implemented as Java threads and so avoided the overhead of process context switches and enforced a common look and feel.

• SWT (part of Eclipse)• Discussed at:

http://java.sun.com/docs/books/tutorial/uiswing/index.html

Goals

• The goal of these two lectures is to provide the student an understanding of graphics in Java.

• GUI1 explores the Java 2D API, the basic graphics primitives.

• GUI2 explores image processing, image I/O, and animation.

Review of AWT

• The foundation of GUI in Java

• Implemented in separate processes

• Rudimentary

• Supported by most web browsers, so that most applets should use AWT instead of Swing.

AWT Graphics Features

• Basic facilities for creating a graphical user interface.

• Drawing graphics– java.awt.Graphics– java.awt.Color– java.awt.Font

• Java 2D API (added in Java 1.2)– java.awt.Graphics2D

Using AWT

• Import:– java.awt.*– java.awt.event.*

• Everything inherits from java.awt.Component, including graphics.

• The AWT GUI is organized by containers, inheriting from java.awt.Container (which inherits from Component).

• The arrangement of controls is managed by various layout managers.

• Notification of changes is handled by events and event listeners that run in a GUI thread.

Graphics Before Java 1.2

• Demonstrated in GraphicsSampler.java from Java Examples in a Nutshell (library).

• init() is called to initialise color resources.

• paint() is called every time the object needs to be redrawn, for example when the applet is covered by another window and the window moves or disappears.

The Graphics Object

• Used for all graphics operations prior to 1.2

• Represents a drawing surface. Can be used to draw on a Component, Image, or printer.

• Keeps track of the current state.

• Provides attributes and methods for drawing.

java.awt.Graphics

• An interface.• Cannot be created directly. You must:

– use getGraphics() on a Component– use getGraphics() on an image or PrintJob– copy from an existing Graphics object using

create().

• When no longer needed, call dispose() on it to free up the system resources it uses.

Graphics Attributes

• Color, the current Color object

• Font, the current Font object

• Clipping region, the Rectangle to be painted

• Origin, a point

• Paint mode, a boolean

• Background color, a Color

Graphics Operations

• drawLine() (1 pixel)

• drawPolyLine() (polygon)

• drawArc()

• drawOval()

• drawPolygon()

• drawRect()

• drawRoundRect()

• draw3DRect()

• fillArc()

• fillOval()

• fillPolygon() (polygon)

• fillRect()

• fillRoundRect()

• fill3DRect()

• drawBytes() (byte[])

• drawChars() (char[])

More Graphics Operations

• drawString()• drawImage()

– various versions, requiring an ImageObserver object

• clearRect()• various setters and

getters• copyArea()

• clipRect()• translate()• hitClip()• translate()

java.awt.Font

• Represents a font. The constructor is passed a name, a style (PLAIN, BOLD, ITALIC, BOLD+ITALIC), and a font size.

• Names include:– “serif”– “sansserif”– “monospaced”– “Dialog”– “DialogInput”– “Symbol”

java.awt.Color

• Represents colors.• Can represent a color coded in RGB space defined

from integers or floating point numbers.• Has a static method that allows you to create

colors in the HSB color space.• Provides some standard colors as constants, such

as Color.black or Color.white.• java.awt.SystemColor is a subclass with constants

for various colors used on the system desktop.

java.awt.Rectangle

• Defines a rectangle using the x and y coordinates of the upper left corner and the width and height. These four values are public and can be manipulated directly.

• Used to define rectangular regions, usually in an image being manipulated.

• Can be used for other purposes.

java.awt.Point

• A concrete subclass of java.awt.geom.Point2D• A Point is defined as two integers, x and y. These

are public fields and can be manipulated directly.• equals() is overridden so two points are equal if

they have the same x and y values.• hashCode() doesn’t appear to be overridden, so

don’t use a Point as an index in a HashMap with the expectation that it will be a value class.

This Week’s Exercise

• Access http://www.oreilly.com/catalog/jenut3

• Download the sample code and create an Eclipse project for GraphicsSampler.java (an Applet)

• Run the Applet in the debugger so you can observe how it works.