1. rendering process
Graphics Primitives: graphic, text, picture (raw material).
Rendering Engine: Changes, combined, rendering, cutting, and so on.
Output Devices: Outputs the transformed product.
2. Geometry (Graphics Primitives)
a) Point2D
i. Description: Point2D, used to describe points in the image.
ii. Class diagram:
b) shape & pathiterator
i. shape:
1. Description: For Graphics2D, she only knows how to draw a shape, so in java2d, all graphics are from Shape Inherited two. We can draw a shape border through Draw (), draw an internal graphic of a shape by fill ().
2. Class diagram:
3. code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2d.float r2 = new rectangle2d.float (10, 10, 100, 50);
g2.setpaint (color.red);
G2.DRAW (R2);
}
4. Runtime:
II. PathIterator
1. Description: For each Shape, his border can be broken down into a collection of some lines, this collection is in java2d Using Pathiterator, the lines in the collection include straight lines, arcs, and the like. Therefore, Pathiterator defines constant SEG_MOVETO, which is used to determine the line segment type. Pathiterator has similar but not completely equivalent to what we usually use. 2. Code:
Public static void main (String [] args) {
Ellipse2d.float E2 = new elipse2d.float (0, 0, 100, 50);
Pathiterator Pi = E2.GetPathiterator (NULL);
While (pi.isdone () == false) {
Double [] CO = New Double [6];
INT TYPE = Pi.currentSegment (Co);
Switch (Type) {
Case pathiterator.seg_moveto:
System.out.println ("Move to" Co [0] "," Co [1]);
Break;
Case pathiterator.seg_lineto:
System.out.println ("Line to" Co [0] "," Co [1]);
Break;
Case pathiterator.seg_quadto:
System.out.println ("Quadratic To" Co [0] "," Co [1]
"," CO [2] "," Co [3]);
Break;
Case pathiterator.seg_cubicto:
System.out.println ("Cubic To" Co [0] "," Co [1] ","
CO [2] "," CO [3] "," Co [4] "," Co [5]);
Break;
Case pathiterator.seg_close:
System.out.println ("close");
Break;
}
PI.NEXT ();
}
}
3. Runtime:
Move to 100.0, 25.0
Cubic to 100.0, 38.80711874576983, 77.61423749153965, 50.0, 50.0, 50.0
Cubic TO 22.38576250846033, 50.0, 0.0, 38.80711874576983, 0.0, 25.0
Cubic To 0.0, 11.192881254230166, 22.38576250846033, 0.0, 50.0, 0.0
Cubic to 77.61423749153965, 0.0, 100.0, 11.192881254230166, 100.0, 25.0close
iii. generalpath
1. Description: We can use the methods provided by GeneralPath, with manual way to specify a SHAPE border.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
GeneralPath gp = new generalpath ();
gp.moveto (10, 10);
GP.LineTo (80, 40);
gp.quadto (160, 100, 100, 60);
Gp.curveto (200, 10, 20, 30, 70, 20);
g2.setpaint (color.red);
G2.DRAW (GP);
}
3. Runtime:
c) graphics library:
i. line2d:
1. Description: It is used to represent a straight line.
2. Class diagram:
3. code:
Public void paint (graphics g) {
Color [] mcolors = {color.red, color.green, color.blue};
Graphics2D G2 = (Graphics2D) g;
For (int i = 0; i <25; i ) {
Double Ratio = (Double) I / (Double) 25;
Line2D line = new line2d.double (0, Ratio * 200, Ratio * 200, 200);
G2.SETPAINT (McOLORS [I% Mcolors.Length]);
g2.draw (line);
}
}
4. Runtime:
ii. Quadcurve2D: 1. Description: Used to represent a quadratic curve.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Quadcurve2d.float Q2 = new quadcurve2d.float (0, 0, 100, 200, 200, 0);
g2.setpaint (color.red);
G2.DRAW (Q2);
}
3. Runtime:
iii. Cubiccurve2D:
1. <-> Description: It is used to represent a three curve.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Cubiccurve2d.float c2 = new cubiccurve2d.float (0, 50, 50, 100, 150, 0,
200, 50);
g2.setpaint (color.red);
G2.DRAW (C2);
}
3. Runtime:
iv. Rectangle2D
1. -> Description: It is used to represent a rectangle. We can use intersectsline () and Outcode () to determine a relative relationship between a line or a point and a rectangle.
2. Class diagram:
3. code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2d.float r2 = new rectangle2d.float (0, 0, 100, 50);
RoundRectangle2d.float rr2 = new ruggage2d.float (150, 0, 100, 50, 5, 5);
g2.setpaint (color.red);
G2.DRAW (R2);
g2.setpaint (color.blue);
G2.DRAW (RR2);
}
4. Runtime:
v. Ellipse2D:
1. Description: Used to indicate an elliptical type.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Ellipse2D E2 = New Ellipse2d.float (0, 0, 100, 50);
g2.setpaint (color.red);
G2.DRAW (E2);
}
3. Runtime:
vi. arc2d:
1. <-> Description: Used to represent curved or arcuate.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Arc2d A21 = New Arc2d.float (0, 0, 50, 50, -45, 90, arc2d.open);
Arc2d A22 = New Arc2d.float (100, 0, 50, 50, -45, 90, arc2d.pie);
Arc2d A23 = New Arc2d.float (200, 0, 50, 50, -45, 90, arc2d.chord);
g2.setpaint (color.red);
G2.DRAW (A21);
G2.SETPAINT (Color.green);
G2.DRAW (A22);
g2.setpaint (color.blue);
G2.DRAW (A23);
}
3. Runtime:
d) -> shapes combining
i. Description: Java2d provides an operation of seeking, detergeting, different or equal operations between graphics. These operations are provided through Area, and we can package a shape into an Area through the constructor of Area, and then use these operations that have been implemented in AREA. Since Area also implements Shape, he can be output directly. ii. renderings:
3. Painting & strokeing
a) Description: Painting refers to the internal operation of the Shape, such as coloring, gradient, texture, etc. Stroking refers to operations of the Border of Shape, such as the width, style, color, etc. of the border.
b) Painting:
i. Monochrome fill:
1. Description: Painting supports monochrome fill. Since the Color extends Paint, he can use the parameters of setPaint ().
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (50, 50, 100, 50);
g2.setpaint (color.red);
G2.Fill (R2);
}
3. Runtime:
II. Gradient fill:
1. -> Description: Painting supports gradient fill.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2d R21 = New Rectangle2d.float (50, 50, 100, 50); gradientPaint GP1 = New GradientPaint (80, 80, Color.Red, 120, 70,
Color.Blue, False;
Rectangle2D R22 = New Rectangle2d.float (200, 50, 100, 50);
GradientPaint GP2 = New GradientPaint (230, 80, Color.Red, 270, 70,
Color.Blue, true);
G2.SETPAINT (GP1);
G2.FILL (R21);
G2.SETPAINT (GP2);
G2.fill (R22);
}
3. Runtime:
iii. Texture Plip:
1. Description: Painting supports texture filling, so-called texture fill is to use a piece of picture as a background and filled the image inside.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
BufferedImage Image = NULL;
Try {
Image = (bufferedimage) imageio.read (New File ("libra.jpg");
} catch (exception e) {}
Rectangle2D R21 = New Rectangle2d.float (50, 50, 100, 50);
TexturePaint TP1 = New TexturePaint (Image, New Rectangle2d.float (0, 0,
10, 10));
Rectangle2D R22 = New Rectangle2d.float (200, 50, 100, 50);
TexturePaint TP2 = New TexturePaint (Image, New Rectangle2d.float (0, 0,
20, 20));
G2.SETPAINT (TP1);
G2.FILL (R21);
G2.SETPAINT (TP2);
G2.fill (R22);
}
3. Runtime:
c) strarking:
i. BasicsTroke:
1. Description: This is a basic implementation provided by Java2D for Stroke, through this class, we can set the width of the border, style Wait. 2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (10, 10, 100, 50);
Basicstroke BS = New Basicstroke (5, Basicstroke.cap_square,
Basicstroke.join_Round, 0, New float [] {4, 4, 8}, 0);
G2.setStroke (BS);
G2.DRAW (R2);
}
3. Runtime:
4. Supplement:
a) end_style
b) join_style
c) Dash array
d) Dash Phase
II. Advance stroke
1. Description: When we use DRAW () instead of Fill (), we will apply the above to Painting settings Draw on the border.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
GradientPaint GP = New GradientPaint (0, 0, Color.Red, 100, 50,
Color.Blue, true);
Rectangle2D R2 = New Rectangle2d.float (0, 0, 100, 50);
Basicstroke BS = New Basicstroke (5);
G2.setStroke (BS);
G2.SETPAINT (GP); G2.DRAW (R2);
}
3. Runtime:
4. rendering
a) Transforming
i. Description: Java2D provides functions such as translation and rotation of images.
II. Translation
1. Description: Translate the image.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (0, 0, 50, 50);
g2.setpaint (color.red);
G2.DRAW (R2);
G2.Translate (60, 80);
g2.setpaint (color.blue);
G2.DRAW (R2);
}
3. Runtime:
iii.
Rotation
1. Description: Rotate the graphic.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (100, 100, 80, 50);
g2.setpaint (color.red);
G2.DRAW (R2);
AffineTransform at = AffineTransform.GtrotateInstance (-1.57, 100,
100);
G2.SetTransform (at);
g2.setpaint (color.blue);
G2.DRAW (R2);
}
3. Runtime:
iv. scaling 1. : Stretching the graphic.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (10, 10, 80, 50);
g2.setpaint (color.red);
G2.DRAW (R2);
g2.scale (1.5, 1.5);
g2.setpaint (color.blue);
G2.DRAW (R2);
}
3. Runtime:
v. "- [endif] -> Shearing
1. Description: Divide the graphic.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (10, 10, 80, 50);
g2.setpaint (color.red);
G2.DRAW (R2);
G2.SHEAR (0.5, 0);
g2.setpaint (color.blue);
G2.DRAW (R2);
}
3. Runtime:
vi. Compound Transformations
1. Description: We can combine multiple transformations to implement a compliant transformation, pay attention to some transformations of the transformation cannot interact of.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (10, 10, 80, 50);
g2.setpaint (color.red);
G2.DRAW (R2);
G2.Translate (50, 50);
g2.scale (2, 1);
g2.setpaint (color.blue);
G2.DRAW (R2);
}
3. Runtime:
b) Compositing
i. Description: How to handle it when two graphics are superimposed.
ii. alphacompositing:
1. Description: Alphacompositing provides some strategies for graphical synthesis.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R21 = New Rectangle2d.float (10, 10, 80, 50);
Rectangle2D R22 = New Rectangle2d.float (10, 10, 50, 80);
g2.setpaint (color.red);
G2.FILL (R21);
Composite com = alphacomposite.getInstance (alphacomposite.src_over,
0.8F);
G2.setComposite (COM);
g2.setpaint (color.blue);
G2.fill (R22);
}
3. Runtime:
4. Supplement:
iii. xor
1. Description: Java2d provides the drawing method of xor. CD = CS1 ⊕ CX ⊕ CS2.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
g2.setxormode (Color.White);
Rectangle2D R21 = New Rectangle2d.float (10, 10, 80, 50);
Rectangle2D R22 = New Rectangle2d.float (10, 10, 50, 80);
G2.SETPAINT (Color.Red); G2.Fill (R21);
g2.setpaint (color.blue);
G2.fill (R22);
}
3. Runtime:
c) clipping
i. Description: Java2D provides clipping of the graphic in any Shape.
ii. code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Rectangle2D R2 = New Rectangle2d.float (10, 10, 80, 50);
Ellipse2D E2 = New Ellipse2d.float (20, 10, 30, 40);
g2.setpaint (color.red);
g2.setClip (E2);
G2.Fill (R2);
}
iii. Running:
d) -> rendering hints-> rendering hints
i. Description: Rendering Hints provides some options for rendering.
ii. code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
GeneralPath gp = new generalpath ();
gp.moveto (0, 0);
For (int i = 1; i <20; i ) {
GP.LineTo (i * 20, i% 2 * 50);
}
g2.setpaint (color.red);
G2.DRAW (GP);
g2.setrenderingHint (RenderingHints.Key_antialiaSing,
RenderingHints.Value_antialias_on;
G2.Translate (0, 80);
g2.setpaint (color.blue);
G2.DRAW (GP);
}
iii. Running:
<-! [! If supportLists] ->. iv supplement: Key Possible ValuesKEY_ANTIALIASINGVALUE_ANTIALIAS_ON | VALUE_ANTIALIAS_OFF | VALUE_ANTIALIAS_DEFAULTKEY_RENDERINGVALUE_RENDER_QUALITY | VALUE_RENDER_SPEED | VALUE_RENDER_DEFAULTKEY_DITHERINGVALUE_DITHER_DISABLE | VALUE_DITHER_ENABLE | VALUE_DITHER_DEFAULTKEY_COLOR_RENDERINGVALUE_COLOR_RENDER_QUALITY | VALUE_COLOR_RENDER_SPEED | VALUE_COLOR_RENDER_DEFAULTKEY_FRACTIONALMETRICSVALUE_FRACTIONALMETRICS_ON | VALUE_FRACTIONALMETRICS_OFF | VALUE_FRACTIONALMETRICS_DEFAULTKEY_TEXT_ANTIALIASINGVALUE_TEXT_ANTIALIAS_ON | VALUE_TEXT_ANTIALIAS_OFF | VALUE_TEXT_ANTIALIAS_DEFAULTKEY_INTERPOLATIONVALUE_INTERPOLATION_BICUBIC | VALUE_INTERPOLATION_BILINEAR | VALUE_INTERPOLATION_NEAREST_NEIGHBORKEY_ALPHA_INTERPOLATIONVALUE_ALPHA_INTERPOLATION_QUALITY | VALUE_ALPHA_INTERPOLATION_SPEEDVALUE_ALPHA_INTERPOLATION_DEFAULT
5.
TEXT
a) Description: We have four ways to render text, from simple to complex, use SWING control; use Graphics2D DrawString () function; use textLayout; use Glyph Shape.
b)
Drawing
TEXT
i. Description: Draw the font by Attribute String.
II. Supplement:
TEXTATTRIBUTE key and value.
KeyPossible ValuesBACKGROUND (text background) a PaintCHAR_REPLACEMENT (text replacement) a GraphicAttributeFAMILY (font-related) a String containing a font family nameFONT (font-related) a FontFOREGROUND (text foreground) a PaintJUSTIFICATION (text drawing) a Float from 0.0 to 1.0 | JUSTIFICATION_FULL | JUSTIFICATION_NONEPOSTURE (the font style) a Float | POSTURE_OBLIQUE | POSTURE_REGULARSIZE (font-related) a FloatSTRIKETHROUGH (strikeout) a Boolean | STRIKETHROUGH_ONSUPERSCRIPT (the font style) an Integer | SUPERSCRIPT_SUB | SUPERSCRIPT_SUPERSWAP_COLORS (before switching, background) a Boolean | SWAP_COLORS_ONTRANSFORM (text conversion ) a TransformAttributeUNDERLINE (underscore) an Integer | UNDERLINE_ONWEIGHT (the font style) a Float | WEIGHT_EXTRA_LIGHT | WEIGHT_LIGHT | WEIGHT_DEMILIGHT | WEIGHT_REGULAR | WEIGHT_SEMIBOLD | WEIGHT_MEDIUM | WEIGHT_DEMIBOLD | WEIGHT_BOLD | WEIGHT_HEAVY | WEIGHT_EXTRABOLD | WEIGHT_ULTRABOLDWIDTH (the font style) a Float | WIDTH_CONDENSED | WIDTH_SEMI_CONDENSED | WIDTH_REGULAR Width_semi_extended | width_extended | iii. Text Replacement:
1. Description: Text replacement is implemented through the TextAttribute of TEXTATTRIBUTE.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
AttributeString as1 = New AttributeString ("Hello World 1");
As1.addattribute (TextAtttribute.Foreground, Color.Red, 1, 4);
as1.addattribute (TextAttribute.Foreground, Color.Blue, 7, 10);
g2.drawstring (as1.getiterator (), 10, 10);
AttributeString as2 = New AttributedString ("Hello World 2"); Image Libra = NULL;
Try {
Libra = imageio.read (New File ("libram.jpg"));
} catch (exception e) {}
ImageGraphicattribute Iga = New ImageGraphicattribute (Libra,
Graphicattribute.roman_baseline);
As2.addattribute (TextAttribute.char_replacement, IGA, 2, 3);
g2.drawstring (as2.getiterator (), 10, 40);
AttributedString as3 = New AttributeDstring ("Hello World 3");
Ellipse2D E2 = New Ellipse2d.float (0, 0, 50, 25);
Shapegraphicattribute SGA = New ShapegraphiTtribute (E2,
Graphicattribute.roman_baseline, shapegraphicattribute.stroke;
As3.addattribute (TextAttribute.char_replacement, sga, 4, 5);
g2.drawstring (as3.getiterator (), 10, 80);
}
3. Runtime:
iv. text transformation
1. Description: All transforming technologies in rendering can be used in text.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
g2.setrenderingHint (RenderingHints.Key_antialiaSing,
RenderingHints.Value_antialias_on;
AttributedString as = New AttributedString ("JADE");
INT LIMIT = 6;
AffineTransform at = NULL;
For (int i = 1; i <= limited; i ) {
Float ratio = (float) I / (float) limit;
At = AffineTransform.getTranslateInstance (150, 150);
at.Rotate (Math.pi * (Ratio - 1));
Float alpha = ((i == limited)?
1.0F: Ratio / 3);
as.addattribute (TextAttribute.transform, AT);
as.addattribute (TextAttribute.size, New float (64)); g2.setcomposite (alphacomposite.getInstance)
Alphacomposite.src_over, alpha);
g2.drawstring (as.getiterator (), 0, 0);
}
}
3. Runtime:
v .- [Endif] -> Underline and Delete Line:
1. Description: Used to set the underline and deletion line of the text.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
AttributedString as = New AttributeString ("Hello World");
askDATTRIBUTE (TextAttribute.underline,
TextAtttribute.underline_on, 0,5);
askDATTRIBUTE (TextAttribute.strikeTHRIKETHROUGH,
TextAttribute.strikethRibute.StrikeTHROUGH_ON, 6, 11);
askDATTRIBUTE (TextAttribute.size,
64.0f);
g2.drawstring (as.getiterator (), 10, 60);
}
3. Runtime:
c) fonts:
i. Description: Fonts has three names, one is Family name; one is font name, unique; one is logical name, by operation The system is defined, and the actual font will be mapped when used.
ii. Show all system fonts:
1. Description: You can get all registered fonts in your system via Graphicsenviroment.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Font [] fonts = graphicsenvironment.getlocalgraphicsenvironment (). GetAllFonts ();
FOR (Font F: fonts) {
System.out.printf ("Family Name:% s. / T / t / t
FONT NAME:% S / N ", F.GetFamily (), F.GetFontName ());
}
}
3. Runtime:
Family name: arial. Font name: arial
Family name: Arial Black. Font name: Arial Black
Family name: arial. Font name: arial bold
...
Family name: Young circle. Font Name: young circle
iii. font transformation:
1. Description: We can transform fonts through AffineTransform.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Font fo = new font ("New Song", Font.Plain, 12);
g2.setfont (fo);
G2.drawstring ("Hello World", 40, 10);
AffineTransform at = AffineTransform.GtrotateInstance (3.14 / 6);
FONT fd = fo.derivefont (at);
G2.SetFont (FD);
G2.drawstring ("Hello World", 40, 40);
}
3. Runtime:
iv. font render context
1. Description: When we need to get information about the string rendering, we need to have a FontRenderContext instance.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
INT middle = 50;
FontrenderContext FRC = G2.GetFontrenderContext ();
Rectangle2D R2 = g2.GetFont (). GetStringBounds ("Hello World", FRC); G2.DrawString ("Hello World", (FLOAT) (Middle - R2.Getwidth () / 2), 20);
R2 = g2.Getfont (). getStringBounds ("Nice to Meet you", FRC);
g2.drawstring ("nice to meet you", (float) (Middle - R2.Getwidth () / 2),
40);
}
3. Runtime:
v. "- [endif] -> line metrics
1. Description: Used to get information related to text.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Font Font = New Font ("New Song", Font.Plain, 64);
g2.setfont (font);
FontrenderContext FRC = G2.GetFontrenderContext ();
Rectangle2D R2 = G2.GetFont (). GetStringBounds ("Google", FRC);
INT width = (int) r2.Getwidth ();
INT x = 20;
INT Y = 80;
LINEMETRICS LM = G2.GetFont (). GetLinemetrics ("Google", FRC);
G2.drawstring ("Google", X, Y);
Line2D Baseline = New Line2D.float (x, y, x width, y);
Line2d ascentline = new line2d.float (x, y - lm.getascent (), x width,
Y - lm.getascent ());
Line2D descentline = new line2d.float (x, y lm.getdescent (),
X Width, Y LM.GetDescent ());
Line2D leadingline = new line2d.float (x, y lm.getdescent ()
lm.getdescent (), x width, y lm.getdescent ()
lm.getdescent ());
g2.setpaint (color.gray);
g2.draw (baseline);
g2.setpaint (color.red);
g2.draw (ascentline);
G2.SETPAINT (Color.green);
g2.draw (descentline);
G2.SETPAINT (color.blue); g2.draw (leadingline);
}
3. Runtime:
4. Supplement:
d )-> Textlayout
i. base:
1. Description: Use textLayout to render text.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
FontrenderContext FRC = G2.GetFontrenderContext ();
TextLayout TL = New Textlayout ("Hello World", G2.Getfont (), FRC);
TL.DRAW (G2, 10, 20);
}
3. Runtime:
II.
Text metrics
1. Description: Used to get information related to text.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Font Font = New Font ("New Song", Font.Plain, 64);
g2.setfont (font);
FontrenderContext FRC = G2.GetFontrenderContext ();
TextLayout TL = New TextLayout ("Google", G2.GetFont (), FRC);
Int width = (int) tl.getbounds (). getWidth ();
INT x = 20;
INT Y = 80;
TL.DRAW (G2, X, Y);
Line2D Baseline = New Line2D.float (x, y, x width, y);
Line2d ascentline = new line2d.float (x, y - tl.getascent (), x width, y - tl.getascent ());
Line2D descentline = new line2d.float (x, y tl.getdescent (),
X Width, Y TL.getDescent ());
Line2d leadingline = new line2d.float (x, y tl.getdescent ()
TL.GetDescent (), X Width, Y TL.GetDescent ()
TL.GetDescent ());
g2.setpaint (color.gray);
g2.draw (baseline);
g2.setpaint (color.red);
g2.draw (ascentline);
G2.SETPAINT (Color.green);
g2.draw (descentline);
g2.setpaint (color.blue);
g2.draw (leadingline);
}
3. Runtime:
iii. Hit Testing
1. Description: Which of the characters used to determine the string is clicked by the mouse.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
G2.SetFont (New Font ("New Song", Font.Plain, 24);
FontrenderContext FRC = G2.GetFontrenderContext ();
Final TextLayout TL = New TextLayout ("Hello World", G2.Getfont (), FRC);
TL.DRAW (G2, 10, 20);
AddMouseListener (new mouseadapter () {
Public void mouseclicked (mouseevent e) {
Texthitinfo thi = tl.hittestchar (E.GETX (), E.GETY ());
System.out.println ("Hit" twi.getcharindex ());
}
});
}
3. Runtime:
The output when you click on W.
Hit 7
iv. Caret Support
1. Description: Caret is the cursor we usually say, his location, that is, the location of our next input character TextLayout provides a way to operate to the CARET. v .- [endif] -> highlighting support
1. Description: Used to highlight the parts in the text. When we want to use click to highlight some of the contents, you can call another getVisualHighTshape () method for TextLayout through the TextHitInfo obtained in Hiting Test.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
G2.SetFont (New Font ("New Song", Font.Plain, 24);
FontrenderContext FRC = G2.GetFontrenderContext ();
TextLayout TL = New Textlayout ("Hello World", G2.Getfont (), FRC);
Shape s = tl.getLogicalHighlightshape (1, 5);
Shape Ts = AffineTransform.gettranslateInstance (10, 20)
.createTransformedshape (s);
G2.SETPAINT (Color.green);
G2.FILL (TS);
G2.SETPAINT (color.black);
TL.DRAW (G2, 10, 20);
}
3. Runtime:
vi.
Paragraph Layout on Multiple Lines
1.
2. Code:
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
String content = "this method is the Same as the last method, but it
Adds More Constraints on The Line Breaking Algorithm.The Returned
Line Can Contain Only Characters Up to the Supplied OffsetLimit,
Which is a logical character offset. "; attributedString as = new attributedstring (content);
askDATTRIBUTE (TextAttribute.size,
24F);
AttributedCharacteriterator it = as.getiterator ();
FontrenderContext FRC = G2.GetFontrenderContext ();
LinebreakMeasurer LBM = New LinebreakMeasurer (ITER, FRC);
Int maxadvance = 400;
TextLayout TL = NULL;
INT x = 20;
INT Y = 40;
While (lbm.getPosition () TL = lbm.nextLayout (maxadvance); Y = tl.getascent (); TL.DRAW (G2, X, Y); Y = tl.getdescent () tl.getleading (); } Rectangle2d r2 = new rectangle2d.float (20, 40, 400, y - 40); g2.setpaint (color.red); G2.DRAW (R2); } 3. Runtime: e) Glyph Shape i. Base 1. Description: Use Glyph Shape to output text. 2. Code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; G2.SetFont (New Font ("New Song", Font.Plain, 36)); String content = "Hello World"; FontrenderContext FRC = G2.GetFontrenderContext (); Glyphvector gv = g2.getfont (). CreateGlyphVector (FRC, Content); g2.drawglyphvector (GV, 10, 30); Ellipse2D E2 = New Ellipse2d.float (10, 50, 200, 50); g2.setpaint (color.red); G2.Clip (gv.getoutline (10, 80)); G2.Fill (E2); } 3. Runtime: II. Retrieving Glyph Shape 1. Description: Through GlyphVector, we can get the Glyph Shape of each character, and then proceed further. 2. Code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; g2.setrenderingHint (RenderingHints.Key_antialiaSing, RenderingHints.Value_antialias_on; String s = "g o g l e"; Font Font = New Font ("New Song", Font.Plain, 24); FontrenderContext FRC = G2.GetFontrenderContext (); G2.Translate (20, 40); GlyphVector gv = font.createglyphvector (frc, s); INT length = gv.getnumglyphs (); For (INT i = 0; i Point2D P2 = gv.getglyphPosition (i); Double Theta = i * math.pi / 4; Shape glyph = gv.getglyphoutline (i); Rectangle2D r = glyph.getbounds (); AffineTransform at = AffineTransform.GtrotateInstance (Theta, R .getCenterx (), r.getCentery ()); Shape transformedglyph = at.createtransformedshape (glyph); G2.fill (TransformeDGlyph); } } 3. Runtime: iii. Modifying Glyph Vector 1. Description: We can use the methods on the GlyphVector to modify the internal glyphshape. 2. Code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; g2.setrenderingHint (RenderingHints.Key_antialiaSing, RenderingHints.Value_antialias_on; String s = "g o g l e"; font font = new font ("New Song", Font.Plain, 24); FontrenderContext FRC = G2.GetFontrenderContext (); GlyphVector gv = font.createglyphvector (frc, s); INT length = gv.getnumglyphs (); For (INT i = 0; i Double Theta = i * math.pi / 6; AffineTransform at = AffineTransform.GtrotateInstance (Theta); GV.SETGLYPHTRANSFORM (I, AT); } g2.drawglyphvector (GV, 30, 40); } 3. Runtime: iv. glyph metrics 1. Description: 2. Code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; G2.SetFont (New Font ("New Song", Font.Plain, 36)); String content = "google"; FontrenderContext FRC = G2.GetFontrenderContext (); Glyphvector gv = g2.getfont (). CreateGlyphVector (FRC, Content); GLYPHMETRICS GM = NULL; For (int i = 0; i GM = GV.GETGLYPHMETRICS (i); System.out.printf ("Advance:% 4.2F LSB:% 4.2F RSB:% 4.2F / n ", GM .getadvance (), gm.getlsb (), gm.getrsb ()); } } 3. Runtime: Advance: 18.00 LSB: 0.98 RSB: 0.70 ... Advance: 18.00 LSB: 2.25 RSB: 2.11 4. Supplement: 6. Image a) base i. : We can use the subclass of the BufferedImageop provided by the system to process the image. ii. process: iii. Supplement: Class NameSupporting ClassesEffectConvolveOpKernel blur, sharpen, boundary checking AffineTransformOpAffineTransform geometric transformation LookupOpLookupTable, ByteLookupTable, ShortLookupTable separation, reverse RescaleOp lightening, darkening ColorConvertOpColorSpace transform ColorSpace b) convolveop i. Description: By providing different matrices (Kernel), graphic blur, sharpening, boundary check, etc. ii. code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; BufferedImage Image = NULL; Try { FileInputStream Fis = New FileInputStream ("libra.jpg"); JPEGIMAGEDECODER DOCODER = JPEGCODEC.CREATEJPEGDECODER (FIS); Image = docoder.decodeasbufferedImage (); } catch (exception e) { E.PrintStackTrace (); } Float [] blurs = new float [] {1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9F}; Convolveop co = New Convolveop (New Kernel (3, 3, Blurs); BufferedImage dest = Co. Filter (image, null); Int width = image.getwidth (); G2.drawImage (Image, NULL, 10, 10); G2.drawImage (Dest, NULL, 10 Width 10, 10); } Note: Imageio is not read using imageio, because the bufferedImage returned by imageio cannot perform convolveop operations. In addition, the value in the BLURS array must be a floating point value, otherwise it will be wrong. iii. Running: c) affinetransformop i. Description: Geometric transform to the picture. Since it is transforms the object point, it is not equivalent to the previously doing the geometric transformation on Graphics2D. ii. code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; BufferedImage Image = NULL; Try { Image = imageio.read (New File ("libra.jpg")); } catch (exception e) { E.PrintStackTrace (); } AffineTransform at = AffineTransform.GtrotateInstance (Math.pi / 6); AffineTransformoP AO = New AffineTransformop (at, null); BufferedImage dest = ao.filter (image, null); Int width = image.getwidth (); G2.drawImage (Image, NULL, 10, 10); G2.drawImage (Dest, NULL, 10 Width 10, 10); } iii. Running: D) -> Lookupop i. Description: lookupop The color mapping is made through the Lookuptable. Lookuptable is equivalent to a color mapping table, such as RGB (200, 150, 100) pixel points, which will find the value corresponding to this index value in the LOOKUPTABLE with the 200, 150, 100 as the index. So, like the following example, when we set the invert [i] (short) (505 - i), the value of 200 is 200 after the LOOKUPTABLE will become 55, so RGB is (200, 150, 100) value After the checklist is operated, it will become (55, 105, 155), thereby achieving the reversal of color. Through the different constructor provided by Lookuptable, we can also specify the SHORT arrays corresponding to each column. ii. code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; BufferedImage Image = NULL; Try { Image = imageio.read (New File ("libra.jpg")); } catch (exception e) {} BufferedImage Src = New BufferedImage (image.getwidth (), Image .getHeight (), bufferedimage.type_int_rgb); Src.creategraphics (). DrawImage (image, 0, 0, null); Short [] invert = new short [256]; For (int i = 0; i <256; i ) { Invert [I] = (short) (255 - i); } Lookuptable LT = New Shortlookuptable (0, Invert); LOOKUPOP LO = New Lookupop (LT, NULL); BufferedImage dest = lofter (src, null); Int width = image.getwidth (); G2.drawImage (SRC, NULL, 10, 10); G2.drawImage (Dest, NULL, 10 Width 10, 10); } Note: The middle here has added a conversion from Image to SRC, which is to do this because the bufferedImage read from Imageio uses IndexColormodel, which will cause the lookupo to be executed properly, so an additional conversion is required. iii. Running: e) rescaleop i. Description: Used to increase or reduce the brightness of the picture. CD = CO * ScaleFactor Offset. ii. code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; BufferedImage Image = NULL; Try { Image = imageio.read (New File ("libra.jpg")); } catch (exception e) { E.PrintStackTrace (); } Rescaleop RO = New Rescaleop 0.9F, 0, null; BufferedImage dest = ro.filter (image, null); Int width = image.getwidth (); G2.drawImage (Image, NULL, 10, 10); G2.drawImage (Dest, NULL, 10 Width 10, 10); } iii. Running: f) ColorConvertop i. Description: The colorspace used to convert images. ii. code: Public void paint (graphics g) { Graphics2D G2 = (Graphics2D) g; BufferedImage Image = NULL; Try { Image = imageio.read (New File ("libra.jpg")); } catch (exception e) { E.PrintStackTrace (); } ColorConvertop Cco = New ColorConvertop (Colorspace .GetInstance (Colorspace.cs_gray), NULL; BufferedImage dest = cco.filter (image, null); Int width = image.getwidth (); G2.drawImage (Image, NULL, 10, 10); G2.drawImage (Dest, NULL, 10 Width 10, 10); } iii. Running: 7. summary The core of the Java 2D model is Shape, all text, graphics, and the image must ultimately be converted to Shape, then the Java 2D engine can further operate him. For a shape, he contains two steps of the border and content, and we can specify the operation border or content to Java 2D by calling the Draw () or Fill () operation on Graphics2D. Whether it is border or content, we can perform operations such as transform, merge, combination.