Docsity

Two Dimensional Graphics - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Two Dimensional Graphics - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Java Two Dimensional Graphics

with Affine Transforms

Contents

• Introduction to 2D using Java

• Drawing Geometric Primitives

• What are Affine Transforms

• Java Frame Works – affine

• References

Shapes and Areas

  • Shapes - create standard shape objects and draw them in one method call instead of drawing each segment with multiple method calls. The shapes include rectangles and ellipses and also arbitrary shapes such as the Polygon class. The shape classes implement the Shape interface and provide a PathIterator that steps through the segments of the given shape.
  • Areas - combine shapes in different ways to create new areas.
  • Stroke - vary the thickness of a line, produce arbitrary dot- dash sequences, select how corners and line ends are shaped, etc.
  • Paint - fill the interiors of shapes, as well as outlines, not just with solid colors but with - gradients of one color to another, and - textured patterns of tiled images.

Strokes and Paint

Compositing and Clipping

  • Compositing - overlay shapes and images according to several optional rules that take advantage of the alpha transparency.
  • Clipping - to speed up performance, only redraw the portion of the display that lies within a given outline or clip. With Java2d one can use arbitrary clip outlines rather than just rectangles.

Text, Color and Images

  • Text - vast array of text rendering capabilities that range from the basic drawString() method to creating your own editor from scratch.
  • Color - lots of tools to help render colors in a consistent manner regardless of the device on which they are displayed.
  • Images - use many of the above tools and techniques that are used with shapes - e.g. transformations, compositing, clipping etc. - on images.

Draw, then Display

  • The typical Java 2D process begins with
method invocations to draw shapes and
strokes on what can be thought of as an
off-screen image surface with arbitrarily
fine resolution. A rasterizer converts this
off-screen image to a 2D array of pixel or
point values, which can be rendered on a
monitor screen or a printer.

The Graphics class

• The vast majority of uses for the Java 2D

API utilize a small subset of its capabilities

encapsulated in the java.awt.Graphics class.

Most methods of the Graphics class can be

divided into two basic groups:

Drawing method examples

  • drawString – For drawing text g.drawString("Hello", 10, 10);
  • drawImage – For drawing images g.drawImage(img, 0, 0, width, height, 0, 0, imageWidth, imageHeight, null);
  • drawLine, drawArc, drawRect, drawOval, drawPolygon – For drawing geometric shapes g2.draw(new Line2D.Double(0, 0, 30, 40));

Image scaling

• You can choose one of several methods in

the Graphics class based on whether you

want to render the image at the specified

location in its original size or scale it to fit

inside the given rectangle

2D Coordinates

  • Whether you draw a line of text or an image, remember that in 2D graphics every point is determined by its x and y coordinates. All of the draw and fill methods need this information which determines where the text or image should be rendered.

Using x,y Coordinates

  • For example, to draw a line, an application calls the following: java.awt.Graphics.drawLine(int x1, int y1, int x2, int y2) In this code (x1, y1) is the start point of the line, and (x2, y2) is the end point of the line.
  • So the code to draw a horizontal line is as follows:

Graphics.drawLine(20, 100, 120, 100);

Weather example (continued)

The setFont and drawString methods render the temperature and the weather condition. ... // Freezing, Cold, Cool, Warm, Hot, // Blue, Green, Yellow, Orange, Red Font font = new Font("Serif", Font.PLAIN, 36); g.setFont(font); String tempString = feels + " " + temperature+"F"; FontRenderContext frc = ((Graphics2D)g).getFontRenderContext(); ...

Weather example (continued)

The setFont and drawString methods render the temperature and the weather condition. ... g.setColor(textColor); int xTextTemp = rX-(int)boundsTemp.getX(); int yTextTemp = rY-(int)boundsTemp.getY(); g.drawString(tempString, xTextTemp, yTextTemp); int xTextCond = rX-(int)boundsCond.getX(); int yTextCond = rY-(int)boundsCond.getY() + (int)boundsTemp.getHeight(); g.drawString(condStr, xTextCond, yTextCond);