Jazz Up The Standard Java Fonts

xiaoxiao2021-03-06  92

few simple tricks applied to standard Java fonts can help make your Web site stand out from the crowd. With the basic knowledge presented here, you will be able to create a set of font styles richer than the standard plain, bold, and italic. And Do Not Confine Yourself to To Sseas - They Just Lay The GroundWork. You Only NEED A LIMITED MATHematical Background To Understand The Computations. So Read On and Discover The Possibilities!

Display your system fonts Before I proceed, you need to find out what fonts are available on your system. These can vary between Java Virtual Machine (JVM) implementations. The following code displays the names of all the fonts available on a particular JVM. The SCREENSHOT On The Right Illustrate The Fonts Available on My PC. Yours May Look Different.

String [] fonts = getToolkit (). Getfontlist (); font font; int font_size = 20; int x = 20; int y = 25; int line_spacing = 25; for (int i = 0; i

Position the points For most of the seven drawing tricks in this article you duplicate the text, slightly reposition it, and color the duplicate appropriately. The image on the right shows eight positions surrounding a center point. On the horizontal axis x increases from west to East, And On The Vertical Axis Y Increases from North To South. for Example, The Coordinate for the Northeast Position IS (X 1, Y - 1) IF The Center IS (X, Y). I'll Use the positions of ................ ..

The following functions are used to improve code readability: int ShiftNorth (int p, int distance) {return (p - distance);} int ShiftSouth (int p, int distance) {return (p distance);} int ShiftEast (int p, int distance (p distance);} int shiftwest (int p, int distance) {return (p - distance);

Add a shadow The easiest trick adds a shadow to the text. Use a dark color for the shadow and draw it near the center. Then draw the text in the center with its color on top of the shadow. The example here shows a shadow two Points Away In a Southeaster Direction. The Light Looks as if it's coming from the northwest.

G.SetColor (New Color (50, 50, 50)); g.drawstring ("Shadow", Shifteast (x, 2), ShiftSouth (Y, 2)); G.SetColor (New Color (220, 220, 220 )); g.drawstring ("shadow", x, y);

Engrave the text You can achieve an engraved effect by using a darker color for the background than for the text. To emulate a light projected on the inner walls of the engraved text, use a brighter color and draw it near the center. Finally, draw The text in the center.

THE EXAMPLE Here Draws The Inner Walls One Point Southeast of The Center with The Light Coming from The Northwest. This Effect Relies He Careful!

G.SetColor (New Color (220, 220, 220)); G.DrawString ("ENGRAVE", Shifteast (x, 1), ShiftSouth (Y, 1)); G.SetColor (New Color (50, 50, 50 )); g.drawstring ("eNGRAVE", X, Y);

Outline the letters You can outline the text by first drawing it at the northwest, southwest, northeast, and southeast positions with the outline color and then drawing the text in the center with the text color. The example here does exactly that with a red outline Color and a yellow text color.g.setcolor; g.drawstring ("Outline", Shiftwest (x, 1), Shiftnorth (Y, 1)); g.drawstring ("Outline", Shiftwest (x , 1), SHIFTSOUTH (Y, 1)); g.drawstring ("Outline", Shifteast (x, 1), Shiftnorth (Y, 1)); g.drawstring ("Outline", Shifteast (x, 1), ShiftSouth (Y, 1)); g.setcolor (color.yellow); g.drawstring ("Outline", X, Y);

Hollow them out Hollowing is nearly the same as outlining. When you draw the text in the center, use the background color instead. That way, only the contour of the text appears. In the example, bg is the background color used to draw the TEXT.

G.SetColor (color.ble); g.drawstring ("Hollow", Shiftwest (x, 1), Shiftnorth (Y, 1)); g.drawstring ("Hollow", Shiftwest (x, 1), ShiftSouth (Y , 1)); G. DrawString ("Hollow", Shifteast (x, 1), Shiftnorth (Y, 1)); g.drawstring ("Hollow", Shifteast (x, 1), ShiftSouth (Y, 1)) G.SetColor (BG); g.drawstring ("Hollow", X, Y);

Cut them into segments An IBM-logo effect requires two major steps. First you carefully measure the font dimensions. Then you draw the horizontal lines on the text with the background color at computed positions. You start those lines from below the descenders.

The following example uses bg as the background color. For each interval of three pixels, it draws a line from below the font's descenders (descent) and moves up until the ascenders (height) are reached. The string width is w, the ascender height IS H, AND THE DESCENDER IS D.INT W = (g.getFontMetrics ()). StringWidth ("segment"); int h = (g.getFontMetrics ()). getHeight (); int D = (g.GetFontMetrics) ))). getdescent (); G.SetColor (New Color (220, 220, 220)); g.drawstring ("segment", x, y); g.setColor (bg); for (int i = 0; i

Open a third dimension To achieve a three-dimensional effect, you first choose a color slightly darker than the text;. Then, for the main shadow, choose a second color much darker than the text Duplicate the text in one color along one direction ( Say, North) and in The Other Color Along Another Direction (Say, West). This Process Results in Two Shadow Faces for the text.

Finally, you draw the text in the center with its color on top of the shadows. The example here uses yellow as the text color and two darker yellows as the shadows. The thickness of the text is 5. The light comes roughly from the southeast .

Color Top_color = New Color (200, 200, 0); Color Side_Color = New Color (100, 100, 0); for (int i = 0; i <5; i ) {g.setcolor; g.drawstring ("3-Dimension", Shifteast (X, I), ShiftNorth (SHIFTSOUTH (Y, I), 1)); G.SetColor (Side_Color); g.drawstring ("3-Dimension", Shiftwest (Shifteast (x, I), 1), ShiftSouth (Y, I));} g.setcolor (color.yellow); g.drawstring ("3-dimension", Shifteast (x, 5), ShiftSouth (Y, 5));

Put the words in motion The idea behind this effect is enlarging the font larger as the text approaches the viewer. At the same time the text color should be darker, the farther it is from the viewer. Also, you need to draw the text from far to near so you do not block any part of the closest image. The example uses green as the text color. It increases the font size and makes the color brighter at a specified speed. width is the window width and w is the string Width. this example 20 Times.For (int i = 0; i <20; i ) {font_size = 12 i; g.setfont (New Font ("TimeesRoman", Font.Plain, font_size) ); W = (g.GetFontmetrics ()). StringWidth ("motion"); G.SetColor (New Color (0, 65 i * 10, 0)); g.drawstring ("Motion", (Width - W ) / 2, ShiftSouth (Y, speted * i));

Create your own effects This article is intended to inspire you with a few codes that create font styles, but there are limitations. For example, the engraving effect does not work with many colors. Also, some GUI components are not always able to incorporate these effects. That means you may want to make a set of your own GUI components. in fact, building a set of such components is one of the major applications you can use in the future. Another application might use the same techniques in a general drawing context to embellish text buttons, borders, bars, and other geometric shapes. Other possibilities include animations, snazzy titles, logos, and so forth. More advanced developers can probably apply image processing techniques to make even cooler effects.

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

New Post(0)