SUN Online Textbook - Java 2D Text Guide - Two Courses Drawing Art Words

zhaozj2021-02-08  266

Training Index 2D text Guide Lesson 2: Draw WordArt Author: Monica PawlanSeptember 1998 translation: Cherami Liumin November 2001. [<< lesson] [Contents] [next lesson >>] This lesson explains how to use java.awt .Font.TextLayout draws simple and complex art words to create interesting and pleasant visual results.

Draw a line of text Draw Multi-line text Text Tilt Image Filled Cut Region Filled Cut Region with Characters Filled Cut Region Text Properties and Replace Graphics Exercise

Draw a line of text java.awt.font.textLayout class to create art characters using characters, fonts, and property sets. Once you are created, the TextLayout object is not editable, but its method allows you to access layout, font, degree character, select, and click test information. The following code uses Font, TextLayout and FontRenderContext objects to draw a simple text, use 24 points Times black body. Timesb.java is a complete source code.

FontrenderContext FRC = G2.GetFontrenderContext ();

Font f = New font ("Helvetica", Font.Bold, 24);

String s = new string ("24 Pont Helvetica Bold");

TextLayout TL = New TextLayout (S, F, FRC);

Dimension THESIZE = GetSize ();

g2.setcolor (color.green);

Tl.draw (G2, THSIZE.WIDTH / 30, THESIZE.HEIGHT / 2);

Java.awt.font represents an instance of the font available in the system. Java.awt.TextLayout represents unality of artistic word data. Java.awt.font.FontRenderContext contains correct measurements and locates its information when drawing text. Draw a multi-line text you can use TextLayout and Java.awt.LineBreakmeasure to draw a paragraph of art. The next example creates and draws a digital line text using the LinebreakMeasurer object, and the layout of the text meets the width of the component. The font information returned by TextLayout and getDescent methods is used to locate the number of rows in the component. Text is stored as an AttributeCharacTerIterator, so the fonts and size properties can be stored with text. Linebreaksample.java is a complete source code. The SampleUtils.java class contains the text to be displayed by the LinebreakSample program.

note:

The LinebreakSample program supports foreign language text. See the fourth lesson: Foreign language support gets more information.

Public class linebreaksample extends component {

Private LinebreakMeasurer LineMeasure;

Private int ParagraphStart;

Private int paragraphend;

Public Linebreaksample

(Attributedcharacteriterator paragraph) {

FontrenderContext FRC =

SampleUtils.getDefaultfontrenderContext ();

ParagraphStart = paragraph.getBeginDex ();

Paragraphend = paragraph.GetendIndex ();

LineMeasurer =

New LinebreakMeasurer (Paragraph, FRC);

}

Public void paint (graphics g) {

Graphics2d graphics2d = (graphics2d) g;

Dimension size = getsize ();

Float FormatWidth = (float) size.width;

FLOAT DRAWPOSY = 0;

LineMeasurer.SetPosition (ParagraphStart);

While (linemeasurer.getPosition ()

TextLayout Layout =

LineMeasurer.nextLayout (FormatWidth);

Drawposy = layout.getascent ();

Float dramposx;

IF (layout.islefttoright ()) {

Drawposx = 0;

}

Else {

Drawposx = formatwidth - layout.getadvance ();

}

Layout.Draw (Graphics2D, Drawposx, Drawposy);

Drawposy = layout.getdescent ()

Layout.getleading ();

}

}

Tilt text You can create a font object from a transform object that provides information about how to rotate, scales, move or clip fonts. The next example uses a cutting transformation to draw a tilt text. First, the string does not change any transformation, then it is drawn in a different place. Still ProFe.java is a complete source code.

INT w = getSize (). width;

INT H = getSize ().

// Create a transformation

AffineTransform at = new affinetransform ();

At.Settotranslation (30, 50);

AffineTransform Fontat = New AffineTransform ();

FONTAT.SHEAR (0.2, 0.0);

// Create a font and text layer

FontrenderContext FRC = G2.GetFontrenderContext ();

Font thefont = new font ("Times", Font.Bold, W / 25);

String s = New String ("STILL LIFE with TEXT");

TextLayout Tstring = New TextLayout (S, Thefont, FRC);

Font myderivedfont = thefont.derivefont;

String str = New String ("Still Life with Slanted Text");

TextLayout Tstring2 = New Textlayout (Str,

THEDERIVEDFONT, FRC);

// Draw a normal string

g2.setcolor (color.blue);

G2.Transform (AT);

TSTRING.DRAW (G2, (Float) 0, (FLOAT) 0);

/ / Draw the tilt font string

g2.setcolor (color.green);

G2.Transform (AT);

Tstring2.draw (g2, (float) 0, (float) 0);

The next example of image filling clipping zone Creates a shear area from the text string The Starry Night, then uses Vincent Van GOGH Painting The Starry Night populates the shear area. Starry.java is a complete source code. This sample code creates a text layer with Times Black Body String The Starry Night. Then get the width of the TextLayout range. The range contains all the pixels that can be drawn. It then obtains the outline of the text layer and calculates the x and y positions of the original layer with the width of the range. The outline is used to create a Shape object, and the Rectangle object is created from the SHAPE object. At this time, the foreground color of the graphics environment is set to blue and the Shape object is drawn to the graphical environment. The following images and code snippets show what happened. FontrenderContext FRC = G2.GetFontrenderContext ();

Font f = New font ("Times", Font.Bold, W / 10);

String s = new string ("The Starry Night");

TextLayout TL = New TextLayout (S, F, FRC);

FLOAT SW = (FLOAT) TL.GetBounds (). getWidth ();

AffineTransform Transform = New AffineTransform ();

Transform.Settotranslation (W / 2-SW / 2, H / 4);

Shape shape = tl.getoutline (Transform);

Rectangle r = shape.getbounds ();

g2.setcolor (color.blue);

g2.draw (shape);

Finally, use the Shape object to create a shear area in a graphical environment, then Starry.gif is drawn to the clipping area, the starting point at the top left corner of the Rectangle object (R.x and R.Y) and fill the entire range of rectangular objects.

g2.setClip (Shape);

G2.drawImage (IMG, R.X, R.Y, R.Width, R.Height, this);

Filling the shear area in order to obtain a slightly different effect, the following example creates a shear area from the text layer BY, which is filled with blue, and then draws a yellow horizontal line in the blue shear area. The information returned by the getascent method of TextLayout is used to locate the displayed lines. CLIPPING.JAVA is a complete source code.

FontrenderContext FRC = G2.GetFontrenderContext ();

Font f = New font ("Helvetica", Font.Bold, W / 8);

String s = new string ("by");

TextLayout TL = New TextLayout (S, F, FRC);

FLOAT SW = (FLOAT) TL.GetBounds (). getWidth ();

AffineTransform Transform = New AffineTransform ();

Transform.Settotranslation (W / 2-SW / 2, H / 2);

Shape shape = tl.getoutline (Transform);

g2.setClip (Shape);

g2.setcolor (color.blue);

g2.fill (shape.getbounds ());

g2.setcolor (color.yellow);

For (int J = shape.getbounds (). y;

J

Line2D line = new line2d.float (0.0f, (float) J,

(float) w, (float) j);

g2.draw (line);

}

Using a Character Fill Shear Region The next example creates a shear area on the text layer Vincent Van GOGH, using blue filled, with white asterisk (*) (the original note: the actual use of the plus ) Fill this area to get the effect of white stars on the blue sky. TextClipping.java is a complete source code.

FontrenderContext FRC = G2.GetFontrenderContext ();

Font f =

New Font ("Times New Roman Bold", Font.Plain, W / 8);

String s = new string ("Vincent Van GOGH");

TextLayout TL = New TextLayout (S, F, FRC);

FLOAT SW = (FLOAT) TL.GetBounds (). getWidth ();

AffineTransform Transform = New AffineTransform ();

Transform.Settotranslation (W / 2-SW / 2, H-H / ​​6);

Shape shape = tl.getoutline (Transform);

g2.setClip (Shape);

g2.setcolor (color.blue);

g2.fill (shape.getbounds ());

G2.SetColor (color.white);

f = New font ("Helvetica", Font.Bold, 10);

TL = New TextLayout (" ", F, FRC);

SW = (float) tl.getbounds (). getWidth ();

Rectangle r = shape.getbounds ();

INT x = r.x;

INT Y = R.Y;

While (y <(R.Y R.Height (int) tl.getascent ())) {

TL.DRAW (G2, X, Y);

IF ((x = (int) SW)> (R.X R.WIDTH)) {

x = r.x;

Y = (int) tl.getascent ();

}

}

Text properties and replacement graphical TextAttribute classes define the properties of the text, so you can define an Attributed string using the text property. The next example uses the text attribute to define the image and features to replace the image with the java.awt.text.attributetedString of the text The Starry Night. A ImageGRAPHICATTRIBUTE object created from the image completes this job. MakeImage.java is a complete source code.

Public void paint (graphics g) {

Graphics2d g2;

G2 = (graphics2d) g;

g2.setrenderingHint (RenderingHints.Key_antialiaSing,

RenderingHints.Value_antialias_on;

G2.seTrenderingHint (RenderingHints.Key_rendering,

RENDERINSIS.VALUE_RENDER_QUALITY); FONT F = New Font ("Times", Font.Bold, 24);

AttributedString ATS =

New AttributeString ("The Starry Night");

ImageGraphicattribute Iga =

New imagegraphicattribute (IMG,

(int) @ttom_alignment;

Ats.addattribute

TextAttribute.char_replacement, IGA, 4, 5);

Ats.addattribute

TextAttribute.char_replacement, IGA, 11, 12);

Ats.addattribute (TextAttribute.font, f, 0, 18);

AttributedCharacteriterator it = ats.getitorator ();

FontrenderContext FRC = G2.GetFontrenderContext ();

TL = New TextLayout (iTER, FRC);

g2.setcolor (color.red);

TL.DRAW (G2, (FLOAT) 20, (FLOAT) 30);

}

转载请注明原文地址:https://www.9cbs.com/read-1943.html

New Post(0)