Docsity

Java 2D Graphics API: Custom Drawing in Swing, Study notes of Architecture

Toggle sidebar
bg1
1
1
Introduction to Computation and Problem
Solving
Prof. Steven R. Lerman
and
Dr. V. Judson Harward
Class 17:
Class 17:
Lab
Lab: The Graphics 2D API
: The Graphics 2D API
2
The Origins of the Java
Graphics 2D API
• The original Java GUI toolkit, the AWT, was a
quick and dirty solution. It used native peer
components to draw all widgets.
• Swing draws all components except the top level
containers using Java methods rather than
relying on platform specific widgets.
• To do this, Swing required improved graphics.
• The Java 2D API was born as enabling
technology for Swing.
• There is now a Java 3D API also.
• See tutorial at
http://java.sun.com/docs/books/tutorial/2d/index.html
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Java 2D Graphics API: Custom Drawing in Swing and more Study notes Architecture in PDF only on Docsity!

11

Introduction to Computation and Problem

Solving

Prof. Steven R. Lerman

and

Dr. V. Judson Harward

Class 17:Class 17:

Lab Lab : The Graphics 2D API: The Graphics 2D API

2

The Origins of the Java

Graphics 2D API

  • The original Java GUI toolkit, the AWT, was a quick and dirty solution. It used native peer components to draw all widgets.
  • Swing draws all components except the top level containers using Java methods rather than relying on platform specific widgets.
  • To do this, Swing required improved graphics.
  • The Java 2D API was born as enabling technology for Swing.
  • There is now a Java 3D API also.
  • See tutorial at http://java.sun.com/docs/books/tutorial/2d/index.html

3

Java Graphics Architecture

Platform Graphics Architecture

Platform Windowing System

Java AWT Java 2D Graphics API

Swing

4

NgonApp

i

JLabel

Custom Draw ng

Swing Components: and JTextField

7

occur in rapid succession.

  • Swing calls the following three methods in

order (on the event thread):

paintComponent() paintBorder() paintChildren()

children.

How does a GUI Get Drawn?

Swing schedules the drawing. It may

combine multiple redraw requests that

The last recursively paints a container's

8

How to Do Custom Drawing

  • JLabel and JComboBox use paintComponent() themselves.
  • container class, usually JPanel , and override paintComponent().
  • paintComponent() JPanel
  • Override getPreferredSize() or call setPreferredSize() JPanel

Standard Swing components like to draw

If you want to do custom drawing, extend a

Use calls from the 2D API in to draw what you want on the background.

to size to your drawing.

9

The Graphics Class

  • paintComponent() Graphics g , that serves as a drawing toolkit
  • In more recent versions of the JDK, the argument Graphics2D Graphics for the 2D API. So cast it. Graphics
  • public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2 = (Graphics2D) g; //drawing commands go here }

is called with argument

initialized to the component's defaults.

is really a object, which extends

was the original AWT class. Using the 2D API usually starts off like this:

10

Where Does the Graphics Argument Come From?

  • The Graphics argument to the paintComponent()

moment the paint methods are called.

Graphics object.

  • Graphics instance in one call to paintComponent( ) are remembered

changes, like setFont( ) are propagated back to the component itself.

method is a snapshot of your component's default graphics values like font and drawing color at the

It is only a copy of these values. Each time the paint methods are called, you get a new version of the

No changes you make to a

the next time you enter the method. And no

13

Custom Drawing Template

... ... ...

import java.awt.; // for Graphics2D, Paint, Shape, … import java.awt.geom.; // for concrete Shape classes import javax.swing.; // for JPanel, etc public class MyPanel extends JPanel {*

public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2 = (Graphics2D) g;

g2.set Paint/Stroke/Font/etc (...); Shape s = new Shape2D.Float/Double ( ... ); g2. draw/fill ( s );

14

2D Shapes

  • Shape classes that implement Shape are all defined in java.awt.geom.

Ellipse2D.Double // high precision Ellipse2D.Float // low precision

high precision or low.

is an interface defined in java.awt, but the

Shapes all come in two versions, one with high precision coordinates and one with low, e.g.:

Each shape has different constructor arguments, doubles or floats depending on whether they are

15

Creating an Ellipse

Shape s = new Ellipse2D.Double(

300.0, // width 150.0 // height );

100.0, // x coord of bounding box 50.0, // y coord of bounding box

16

Graphics2D

  • The Graphics2D coordinates (as opposed to device coordinates ) that Java calls user space.
  • Graphics2D shapes and operations are defined in floating point ( float or double ) but the y
  • Some Graphics2D floats.

device.

Coordinate System

object uses a variety of world

coordinate increases downwards. calls only take The floating point coordinates are designed to make your graphics independent of the output

The 2D API is designed for printing as well as output to screens at different resolutions.

19

Ellipse.java, 2

}

} }

public Ellipse() { ellipse = new Ellipse2D.Double( 100.0, 50.0, 300.0, 150.0 ); setBackground( Color.white ); setOpaque( true );

public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2 = (Graphics2D) g; g2.setPaint( Color.red ); g2.fill( ellipse );

20

Stroke s and Paint

  • Stroke is an interface, and BasicStroke
  • You can think of Stroke Paint as the corresponding ink.
  • A BasicStroke

Stroke

  • Color implements the Paint interface so a Java Color is the simplest sort of Paint.
  • Paint s plaid).

is the only supplied class that implements it. as a pen and

can have width and a dashed pattern as well as options that define how a ends and treats joints.

can use a pattern or texture (for example,

21

Exercise

  1. Use the Graphics2D draw() method instead of fill() to get an outline instead of a filled ellipse.
  2. Change the Stroke to be 10 units thick. What are the units?
  3. Make the ellipse a circle.
  4. Change the circle to be a square (lookup Rectangle2D ).

22

GeneralPath s

  • Shape?
  • Use a GeneralPath Shape s , s

void append( Shape s, boolean connect ); void lineTo( float x, float y ); void moveTo( float x, float y );

; void closePath();

How can you define your own

. You define the outline by adding path components that can be Line , or curves.

void quadTo( float x1, float y1, float x2, float y2 )

25

Drawing Text

JLabel s.

  • methods: public void drawSting( String s, int x, int y ); public void drawSting( String s, float x, float y );

Up until now we have presented text using

You can draw text directly using the Graphics2D

The coordinates define the left end of the baseline on which the text appears.

26

Signature.java, 1

}

import javax.swing.; import java.awt.Font; import java.awt.; import java.awt.geom.; public class Signature extends JPanel { private String name = "Judson Harward"; private Font signFont; private Stroke underStroke; private Line2D underline; public Signature() { setPreferredSize( new Dimension( 600, 400 )); setBackground( Color.white ); setOpaque( true ); underStroke = new BasicStroke( 1 ); underline = new Line2D.Float( 100F,300F,500F,300F ); signFont = new Font( "Serif", Font.ITALIC, 24 );*

27

Signature.java, 2

} }

public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2 = (Graphics2D) g;

g2.setStroke( underStroke); g2.setPaint( Color.lightGray ); g2.draw( underline ); g2.setFont( signFont ); g2.setPaint( Color.blue ); g2.drawString( name, 110F, 300F );

28

Exercise: Building NgonApp, 1

approaches that of a circle as the number of sides increases.

  • Unpack the files NgonApp.java and NgonView.java JavaFiles.zip

You are going to build an application that illustrates how the area of an inscribed polygon

from the in the lecture directory on the class web site and save them into a new directory. Create a new project in Eclipse for the directory that you just saved the files into. Compile and run.

31

Initial NgonView

NgonView paintComponent() code to

  • double getRegPolyArea( int n ):
  • Dimension getPreferredSize(): size
  • float transX( float x ), float transY( float y ): in a minute

is the class that does all the custom drawing. The initial version has just the draw the background and certain helper methods. void setSides(int n) : installs a new number of polygon sides calculates the area of a regular polygon with n sides inscribed in the unit circle returns a fixed

we'll come back to these

32

NgonApp, 1 st^ Version

After Adding Code

CENTER

SOUTH

JFrame with BorderLayout

NgonView extends JPanel

JPanel with FlowLayout

33

NgonApp , 1st Modification

  • Modify

paintComponent (textX, textY).

  • paintComponent()
  • Compile and test.

NgonView.java so that it will either display an error message or display the polygon area every time the number of sides changes. You will need to create an appropriate font, but then should only have to modify () to draw the appropriate message at Compile and test. Now modify again to draw a blue circle with a stroke two pixels wide as shown on the next slide. Don't use transX/transY().

Now try filling the circle in yellow. Do you want to draw the outline first and then fill, or vice versa? Why?

34

NgonView, Mod 1

Positioning the Circle

100

100

400

400

37

NgonView, Mod 3

Inscribing the Polygon, 1

θ =π ( ) θ^ (^ )

( θ θ)

2 / 5 0.0,0. 1.0,0.

cos ,sin

38

NgonView ,

Inscribing the Polygon, 2

  • (and methods)

() to

Add the

  • Compile and test.

Now use a GeneralPath transX and transY( ) to create the filled inscribed polygon. Start the path at (1,0). Calculate the central angle between vertices. Use a loop to generate the first n-1 sides, and closePath generate the last side. Remember that because y increases downwards the first vertex will be below the X-axis, not above as in regular coordinate geometry. appropriate method call to paintComponent() to fill the polygon.