0% found this document useful (0 votes)
28 views10 pages

Lect05 (Clipping and Text)

The document discusses clipping shapes and handling text in Java, emphasizing the use of clipping paths to define visible regions for rendered images. It explains how text is treated as geometric objects, detailing font creation, manipulation, and metrics in Java 2D. Additionally, it covers advanced text rendering techniques, including glyphs and ligatures, to achieve various visual effects.

Uploaded by

lamiw55363
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views10 pages

Lect05 (Clipping and Text)

The document discusses clipping shapes and handling text in Java, emphasizing the use of clipping paths to define visible regions for rendered images. It explains how text is treated as geometric objects, detailing font creation, manipulation, and metrics in Java 2D. Additionally, it covers advanced text rendering techniques, including glyphs and ligatures, to achieve various visual effects.

Uploaded by

lamiw55363
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SHAPE

CLIPPING AND
DEALING WITH
TEXT
OBJECTIVES
 To deal with clipping of shapes
 to handle text in Java
 To deal with complex operation on text rendering
CLIPPING
- The rendered image may be clipped by a clipping path Hello
- A clipping path defines a region in which the objects will actually be
visible.
- When an object is drawn, it is clipped against the clipping region
- Portions of the object falling outside the clipping path will not be drawn
can be used
- Any Shape object can be used for clipping
- This method can be called repeadly to clip the current clipping region
further with theEllipse2D.Double(0,0,200,100));
g2.clip(new specified shape.
for (int i=0;i<this.getBounds().getWidth();i+=5)
g2.drawLine(i, 0, i,
(int)this.getBounds().getHeight());
DEALING WITH TEXT
- In computer graphics, text represents a special type of geometric objects
- A text string can be compactly represented by a sequence of characters
with standard coding schemes, such as ASCII and Unicode
- The actual rendering shapes of the characters are determined by
predefined fonts
- The geometry describing the shape of a character is known as a glygh
- Java 2D offers a rich set of font and text manipulation features
- The most common high level usages of texts involve creating a font object
and calling the methods setFont and drawString in Graphics2D
Logical fonts Font styles
- A Font object can be created with the following constructor: Serif
PLAIN
 Font (String name, int style, int size) SansSerif ITALIC
Monospaced BOLD
Dialog
DialogInput
DEALING WITH TEXT
- A Font object can be selected in a Graphics2D object with the method:
- void setFont (Font font)
- The font will take effect for subsequent calls to methods for drawing text
- Void drawString(string S, int x, int y);
- In addition to applying existing fonts in the system , it is also possible to
derive new fonts from existing ones by modifying certain attributes
- The following methods in the Font class generate derived fonts:
 Font deriveFont (int style)
 Font deriveFont (float size)
 Font deriveFont(int style, float size)
 Font deriveFont (AffineTransform tx)
 Font deriveFont (int style, AffineTransform tx)
DEALING WITH TEXT
- Font metrics are measurements of rendered texts with specific font
- The following methods of Font provide font metric information
- Rectangle2D getStringBounds(String str, FontRenderContext frc)
- lineMetrics getLineMetrics(String str, FontRenderContext frc)
- The FontRenderContext object can be obtained through a method in Graphics2D
- FontRenderContext getFontRenderContext()
- The getStringBounds method returns a bounding rectangle for the string
- The getLineMetrics method returns a LineMetrics object that contains more detail metric data
- The baseline is the reference line of a font
- The ascent is the amount that the font extends above the baseline
- The descent is the amount extending below the baseline
- The leading is the extra space between the two lines

- The following methods of the LineMetrics retrieve the metrics


- float getAscent()
- float getDescent()
- float getLeading()
Font font = new Font("Serif", Font.BOLD, 36);
AffineTransform tx = new AffineTransform();
tx.shear(0.5, 0);

DEALING WITH TEXT


g2.setFont(font.deriveFont(tx));
g2.drawString("Derived font", 100, 100);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
String str = "String bounds";
Rectangle2D bounds = font.getStringBounds(str, frc);
g2.translate(100, 200);
g2.draw(bounds);
g2.drawString(str, 0, 0);
str = "Baseline, ascent, descent, leading";
g2.translate(0,100);
int w = (int)font.getStringBounds(str, frc).getWidth();
LineMetrics lm = font.getLineMetrics(str, frc);
g2.drawLine(0, 0, w, 0);
int y = -(int)lm.getAscent();
g2.drawLine(0, y, w, y);
y = (int)lm.getDescent();
g2.drawLine(0, y, w, y);
y = (int)(lm.getDescent()+lm.getLeading());
g2.drawLine(0, y, w, y);
g2.drawString(str,0,0);
DEALING WITH TEXT-
LIGATURE
 Glyphs of characters in a font can be retrieved as shape objects
This enables sophisticated processing and application of the glyphs to
achieve varieties of visual effects
A glyph may contain multiple letters
 A common ligature:
 We can get the geometry of text as:

Font font = new Font("Serif", Font.BOLD, 144);


FontRenderContext frc = g2.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, "Java");
Shape glyph = gv.getOutline(100,200);
DEALING WITH TEXT-GLYPH
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Font font = new Font("Serif", Font.BOLD, 144);
FontRenderContext frc = g2.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, "Java");
Shape glyph = gv.getOutline(100,200);
g2.setClip(glyph);
g2.setColor(Color.red);
for (int i = 0; i < 2000; i++) {
Shape shape = new Ellipse2D.Double(Math.random()*500,
Math.random()*400, 30, 20);
g2.draw(shape);
}
DEALING WITH TEXT-GLYPH
Font font = new Font("Serif", Font.BOLD, 144);
FontRenderContext frc = g2.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, "Java");
Shape glyph = gv.getOutline(100,200);
g2.setClip(glyph);
g2.setColor(Color.red);
for (int i=0;i<this.getBounds().getWidth();i+=5)
g2.drawLine(i, 0, i, (int)this.getBounds().getHeight());

You might also like