











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












11
2
3
Platform Graphics Architecture
Platform Windowing System
Java AWT Java 2D Graphics API
Swing
4
i
JLabel
Custom Draw ng
Swing Components: and JTextField
7
paintComponent() paintBorder() paintChildren()
8
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
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?
moment the paint methods are called.
Graphics object.
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
... ... ...
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
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
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
device.
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
}
} }
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
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
22
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
JLabel s.
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
}
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
} }
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
approaches that of a circle as the number of sides increases.
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
NgonView paintComponent() code to
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
CENTER
SOUTH
JFrame with BorderLayout
NgonView extends JPanel
JPanel with FlowLayout
33
paintComponent (textX, textY).
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
100
100
400
400
37
θ =π ( ) θ^ (^ )
( θ θ)
2 / 5 0.0,0. 1.0,0.
cos ,sin
38
() to
Add the
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.