Graphics and Java 2D

Download Report

Transcript Graphics and Java 2D

Graphics and Java 2D
Introduction
• Java’s graphics capabilities
– Drawing 2D shapes
– Controlling colors
– Controlling fonts
• Java 2D API
– More sophisticated graphics capabilities
• Drawing custom 2D shapes
• Filling shapes with colors and patterns
2
Introduction:
Classes and Interfaces
Object
Color
Component
Font
FontMetrics
Graphics
Polygon
Classes and interfaces from the Java2D API that appear
in package java.awt
Graphics2D
interface
java.awt.Paint
BasicStroke
interface
java.awt.Shape
GradientPaint
interface
java.awt.Stroke
TexturePaint
Classes from the Java2D API that appear in package
java.awt.geom
GeneralPath
Line2D
RectangularShape
Arc2D
Ellipse2D
Rectangle2D
RoundRectangle2D
3
Introduction
• Java’s coordinate system
– Scheme for identifying all
points on screen
– Upper-left corner has
(0, 0)
coordinates (0,0)
– Coordinate point
composed of x-coordinate
and y-coordinate
+x
X a xis
(x , y )
+y
Y a xis
4
Graphics Contexts and Graphics
Objects
• Graphics context
– Enables drawing on screen
– Graphics object manages graphics context
• Controls how information is drawn
– Class Graphics is abstract
• Cannot be instantiated
• Contributes to Java’s portability
– Class Component method paint takes Graphics
object
public void paint( Graphics g )
– Called through method repaint
5
Color Control
• Class Color
– Defines methods and constants for
manipulating colors
– Colors are created from red, green and blue
components
• RGB values
6
Color constant and value
Color constant
public final static Color ORANGE
Color
RGB value
public final static Color PINK
orange
pink
255, 200, 0
255, 175, 175
public final static Color CYAN
cyan
0, 255, 255
public final static Color MAGENTA
magenta
255, 0, 255
public final static Color YELLOW
yellow
255, 255, 0
public final static Color BLACK
black
0, 0, 0
public final static Color WHITE
white
255, 255, 255
public final static Color GRAY
gray
128, 128, 128
public final static Color LIGHT_GRAY
light gray
192, 192, 192
public final static Color DARK_GRAY
dark gray
64, 64, 64
public final static Color RED
red
255, 0, 0
public final static Color GREEN
green
0, 255, 0
public final static Color BLUE
blue
0, 0, 255
7
Color methods & color-related
Graphic methods
Method
Description
Color constructors and methods
public Color( int r, int g, int b )
Creates a color based on red, green and blue components expressed as integers
from 0 to 255.
public Color( float r, float g, float b )
Creates a color based on red, green and blue components expressed as
floating-point values from 0.0 to 1.0.
public int getRed()
Returns a value between 0 and 255 representing the red content.
public int getGreen()
Returns a value between 0 and 255 representing the green content.
public int getBlue()
Returns a value between 0 and 255 representing the blue content.
Graphics methods for manipulating Colors
public Color getColor()
Returns a Color object representing the current color for the graphics
context.
public void setColor( Color c )
Sets the current color for drawing with the graphics context.
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ShowColors.java
// Choosing colors with JColorChooser.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowColors extends JFrame {
private JButton changeColorButton;
private Color color = Color.LIGHT_GRAY;
private Container container;
// set up GUI
public ShowColors()
{
super( "Using JColorChooser" );
container = getContentPane();
container.setLayout( new FlowLayout() );
// set up changeColorButton and register its event handler
changeColorButton = new JButton( "Change Color" );
changeColorButton.addActionListener(
9
24
new ActionListener() { // anonymous inner class
25
26
// display JColorChooser when user clicks button
27
public void actionPerformed( ActionEvent event )
28
{
29
color = JColorChooser.showDialog(
30
ShowColors.this, "Choose a color", color );
31
32
JColorChooser
allows // set default color, if no color is returned
33user to choose from
if ( color == null )
34
color = Color.LIGHT_GRAY;
among several colors
35
36
// change content pane's background color
37
container.setBackground( color );
38
}
static method
39
} // end anonymous inner class
showDialog displays
40
); // end call to addActionListener
the color chooser dialog
41
42
container.add( changeColorButton );
43
44
setSize( 400, 130 );
45
setVisible( true );
46
} // end ShowColor2 constructor
10
47
48
49
50
51
52
53
// execute application
public static void main( String args[] )
{
ShowColors application = new ShowColors();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class ShowColors2
11
Font Control
• Class Font
– Contains methods and constants for font
control
– Font constructor takes three arguments
• Font name
– Monospaced, SansSerif, Serif, etc.
• Font style
– Font.PLAIN, Font.ITALIC and Font.BOLD
• Font size
– Measured in points (1/72 of inch)
12
Font-related methods and
constants
Method or constant
Description
Font constants, constructors and methods for drawing polygons
public final static int PLAIN
A constant representing a plain font style.
public final static int BOLD
A constant representing a bold font style.
public final static int ITALIC
A constant representing an italic font style.
public Font( String name, int style, int size )
Creates a Font object with the specified font, style and size.
public int getStyle()
Returns an integer value indicating the current font style.
public int getSize()
Returns an integer value indicating the current font size.
public String getName()
Returns the current font name as a string.
public String getFamily()
Returns the font’s family name as a string.
public boolean isPlain()
Tests a font for a plain font style. Returns true if the font is plain.
public boolean isBold()
Tests a font for a bold font style. Returns true if the font is bold.
public boolean isItalic()
Tests a font for an italic font style. Returns true if the font is italic.
13
Font-related methods and
constants
Method or constant
Description
Graphics methods for manipulating Fonts
public Font getFont()
Returns a Font object reference representing the current font.
public void setFont( Font f )
Sets the current font to the font, style and size specified by the Font
object reference f.
// set font to Serif (Times), bold, 12pt and draw a string
g.setFont( new Font( "Serif", Font.BOLD, 12 ) );
g.drawString( "Serif 12 point bold.", 20, 50 );
// set font to Monospaced (Courier), italic, 24pt and draw a string
g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) );
g.drawString( "Monospaced 24 point italic.", 20, 70 );
// set font to SansSerif (Helvetica), plain, 14pt and draw a string
g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) );
g.drawString( "SansSerif 14 point plain.", 20, 90 );
// set font to Serif (Times), bold/italic, 18pt and draw a string
g.setColor( Color.RED );
g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) );
g.drawString( g.getFont().getName() + " " + g.getFont().getSize() +
" point bold italic.", 20, 110 );
14
Java 2D API
• Java 2D API
– Provides advanced 2D graphics capabilities
• java.awt
• java.awt.image
• java.awt.color
• java.awt.font
• java.awt.geom
• java.awt.print
• java.awt.image.renderable
– Uses class java.awt.Graphics2D
• Extends class java.awt.Graphics
15
Java 2D API
• when using Swing overide
paintComponent instead of paint.
16
Java 2D API
• Java 2D shapes
– Package java.awt.geom
• Ellipse2D.Double
• Rectangle2D.Double
• RoundRectangle2D.Double
• Arc3D.Double
• Lines2D.Double
17
Java 2D API
• The Java 2D API introduces java.awt.Graphics2D , a
new type of Graphics object.
• Graphics2D extends the Graphics class to provide
access to the enhanced graphics and rendering features
of the Java 2D API.
• To use Java 2D API features, you cast the Graphics
object passed into a component's rendering method to a
Graphics2D object.
public void Paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
...
}
18
Java 2D Drawing Process: Step 1
• Cast Graphics object to Graphics 2D
public void paintComponent(Graphics g) {
super.paingComponent(g); // Typical Swing
Graphics2d g2d = (Graphics2d) g;
g2d.doSomeStuff(...);
...
}
• Note
– All methods that return Graphics in Java 1.1 return
Graphics2D in Java 2
• paint, paintComponent
• getGraphics
19
Java 2D Drawing Process: Step 2
• Set pen parameters
–
–
–
–
–
–
–
–
–
g2d.setPaint(fillColorOrPattern);
g2d.setStroke(penThicknessOrPattern);
g2d.setComposite(someAlphaComposite);
g2d.setFont(someFont);
g2d.translate(...);
g2d.rotate(...);
g2d.scale(...);
g2d.shear(...);
g2d.setTransform(someAffineTransform);
20
Java 2D Drawing Process: Step 3
• Create a Shape object.
Rectangle2D.Double rect = ...;
Ellepse2D.Double ellipse = ...;
Polygon poly = ...;
GeneralPath path = ...;
// satisfies Shape interface
SomeShapeYouDefined shape = ...;
• Note
– Most shapes are in the java.awt.geom package
– There is a corresponding Shape class for most of the
drawXxx methods of Graphics
21
Java 2D Drawing Process: Step 4
• Draw an outlined or filled version of the
Shape
g2d.draw(someShape);
g2d.fill(someShape);
• The legacy methods are still supported
– drawString
– drawLine, drawRect, fillRect
22