Top Banner
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison [email protected] Faison Computing Inc. www.faisoncomputing.com
45

Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison [email protected] Faison Computing Inc. .

Jan 02, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Advanced User Interfaces with Java

SD’98 - Session 3206

Ted Faison

[email protected]

Faison Computing Inc.

www.faisoncomputing.com

Page 2: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

JDK 1.2 - A Better Graphics Framework

JDK 1.1 had little or no support for complex shapes, coordinate transformations or pixel blending

The graphics extensions were added as new packages under the AWT as the Java 2D API

Page 3: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

The Java 2D Packages

• java.awt.color: Color control• java.awt.font: Fonts as complex shapes• java.awt.geom: Coordinate

transformations and shapes• java.awt.print: Advanced printing support

– Books, Pages and Paper

Page 4: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Java 2D API Features• Support for separate user and device

coordinate spaces

• Coordinates can be integers, floats or doubles

User Space Device Space

Page 5: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Java 2D API Features

• Support for coordinate transformations, for translation, rotation, scaling and shearing

User Space Device Space

Page 6: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Java 2D API Features

• Support for complex shapes and hit-testing

• Support for complex clipping

• More precise color control

• Support for variable transparency, allowing color blending

Page 7: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Java 2D API Features

• Better image-processing support, with convolution, color look-up, amplitude scaling.

• Improved screen updating, with offscreen buffers supporting BufferedImages and transparency

Page 8: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Basic Drawing

• The old Graphics context is still there

• All 2D drawing done using Graphics2D– Painting: typecasting Graphics into

Graphics2D

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.red);

//…

}

Page 9: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Coordinate Transformations• Functions that map a point in one space

to a point in another space

• Represented using a 3x3 matrix

• Transformations require multiplying each pixel by a transformation matrix

• Positive angles rotate the X+ axis towards the Y+ axis

• Can be used to invert axes, bend images, distort space arbitrarily

Page 10: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Coordinate Transformations

a11 a12 a13

a21 a22 a23

0 0 1

x

y

1

a11x a12y a13

a21x a22y a23

0 0 1

x’

y’

1

= =

Page 11: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Affine Transforms• Maintain straightness and parallelism• Translation

• setToTranslation(double dx, double, dy);• used to support graphics scrolling

User Space Device Space

Page 12: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Affine Transforms• Rotation

– Rotating about the origin• setToRotation(double theta);

User Space Device Space

Page 13: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Affine Transforms

– Rotation about an arbitrary point• SetToRotation(theta, x, y);

User Space Device Space(x, y) (x, y)

Page 14: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Affine Transforms

• Shearing• setToShear(double sh, double sy)

User Space Device Space

Page 15: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Affine Transforms• Scaling

• setToScale(double sx, double sy)• anisotropic vs isotropic scaling

User Space Device Space

Page 16: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Using class AffineTransform• Commands can be cumulative

– concatenating transformations

• Commands are not commutative– Matrix multiplication is not commutative

• Dealing directly with the transformation matrix, to effect combined transformations in one pass

• g2D.setTransform(myAffineTransform);

Page 17: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Affine Transforms

• Handling transformed images with offscreen buffers

– Examples

• ScalingImages.java

• RotatingImages.java

• ShearingImages.java

Page 18: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Drawing with Paths• All 2D shapes are drawn as paths,

including lines and rectangles

• Class GeneralPath– Used to define arbitrary paths– The outline can be stroked– Graphics2D uses a default stroke:

• square pen• width is 1 pixel• continuous drawing - no dashes

Page 19: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Bezier curves• Used by the 2D API for cubic curves.

• Defined by simple control points

Page 20: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Drawing a Straight Line

public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.red);

GeneralPath path =

new GeneralPath(GeneralPath.EVEN_ODD);

path.moveTo(50.0f, 50.0f);

path.lineTo(200.0f, 200.0f);

g2d.draw(path);

}

Page 21: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Drawing a Straight Line

Page 22: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a shape public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.blue);

GeneralPath path =

new GeneralPath(GeneralPath.EVEN_ODD);

path.moveTo(20.0f, 20.0f);

path.lineTo(100.0f, 20.0f);

path.lineTo(100.0f, 70.0f);

path.lineTo(20.0f, 70.0f);

path.closePath();

g2d.fill(path);

}

Page 23: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a shape

Page 24: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a Shape with a PatternBufferedImage image; // create a buffered image

Rectangle2D.Float rect = new Rectangle2D.Float(0.0f, 0.0f, 100.0f, 200.0f);

TexturePaint pattern =

new TexturePaint(image, rect,

TexturePaint.NEAREST_NEIGHBOR);

g2d.setPaint(pattern);

g2d.drawString(styledString, 10, 10);

Page 25: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a Shape with a Pattern

Page 26: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a Shape with an ImageImage image = getToolkit().getImage(url);

AffineTransform at = new AffineTransform();

at.setToTranslation(0, 200);

g2d.transform(at);

g2d.setClip(myShape);

at.setToTranslation(0, -200);

g2d.drawImage(image, at, this);

at.setToTranslation(0, 200);

g2d.setClip(null);

g2d.draw(myShape);

Page 27: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a Shape with an Image

Page 28: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a Shape with a Gradient Font myFont = new Font("Helvetica", Font.PLAIN, 200);

StyledString styledString =

new StyledString("AB", myFont);

Shape myShape = styledString.getStringOutline();

GradientPaint gradient =

new GradientPaint(0.0f, 0.0f, Color.red,

200.0f, 200.0f,

Color.yellow);

g2d.setPaint(gradient);

g2d.drawString(styledString, 10, 200);

Page 29: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Filling a Shape with a Gradient

Page 30: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Custom Strokes

• Class BasicStroke– simple to use– define common stroke properties

• width• end caps• line joins• dash attributes

Page 31: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Defining a Custom Stroke

g2d.setStroke(

new BasicStroke(penWidth,

BasicStroke.CAP_ROUND,

BasicStroke.JOIN_MITER ) );

path.moveTo(10.0f, 40.0f);

path.lineTo(90.0f, 40.0f);

g2d.draw(path);

Page 32: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Defining a Custom Stroke

Page 33: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

2D Drawing Shortcuts

• interface Rectangle2D– Rectangle2D.Float– Rectangle2D.Double

• RoundRectangle2D

• Arc2D

• Ellipse2D

Page 34: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Clipping• Graphics2D.setClip(Path);• Clipping a circle with a rectangle Ellipse2D.Float circle = new Ellipse2D.Float(10.0f, 10.0f, 100.0f, 100.0f);

Rectangle2D.Float rect = new Rectangle2D.Float (10.0f, 30.0f, 100.0f, 70.0f);

g2d.setClip(rect);

g2d.setColor(Color.red);

g2d.fill(circle);

g2d.setClip(null);

g2d.setColor(Color.black);

g2d.draw(rect);

Page 35: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Clipping a Circle with a Rectangle

Page 36: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Clipping with TextFont myFont = new Font("Helvetica",Font.PLAIN,200);

StyledString styledString =

new StyledString("ABC", myFont);

Shape myShape = styledString.getStringOutline();

AffineTransform at = new AffineTransform();

at.setToTranslation(0, 200);

g2d.transform(at);

Ellipse2D.Float circle =

new Ellipse2D.Float(10.0f,-150.0f,400.0f,150.0f);

g2d.setClip(myShape);

g2d.setColor(Color.red);

g2d.fill(circle);

Page 37: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Clipping with Text

Page 38: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Blending objects– Transparency– The Alpha Channel – Compositing Operations

• called Raster Operations (ROP) in Windows• Class AlphaComposite

– Implements a subset of the Porter-Duff rules

Cd = Cs*Fs + Cd*Fd

Ad = As*Fs + Ad*Fd

C = Color d = destination

F = Fraction s = source

A = Alpha

Page 39: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Compositing Operations• Alpha=1 indicated total opaqueness

• Setting the Operation

AlphaComposite c = AlphaComposite.getInstance(

AlphaComposite.SRC_OVER,

0.5f);

g2d.setComposite(c);

Page 40: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Porter-Duff Operations Supported• CLEAR: destination cleared

• SRC: source copied to destination

• SRC_OVER: source is blended over dest

• SRC_IN: part of source already in dest replaces dest

• SRC_OUT: part of source not already in dest replaces dest

• DST_IN, DST_OUT, DEST_OVER

Page 41: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Using the SRC Rule

Page 42: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Using the SRC_OVER Rule

Page 43: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Using the SRC_IN Rule

Page 44: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Using the SRC_OUT Rule

Page 45: Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison ted.faison@computer.org Faison Computing Inc. .

Conclusion• The Java 2D API extends the AWT with:

– advanced geometric shapes– coordinate transformations– shapes of arbitrary complexity– text as a shape– arbitrary clipping regions– image blending through compositing– image processing capabilities– precise color control