Java know how much (96) set the font and color of the drawing

Source: Internet
Author: User

There are three main ways to display text in a Java drawing:
(1) DrawString (string Str,int x,int y): Displays the string at the specified position.
(2) Drawchars (char data[],int offset,int length, int x, int y): Displays the text in the character array at the specified position, starting at the offset position of the character array and displaying a maximum of length characters.
(3) drawbytes (byte data[],int offset,int length,int x,int y), displays the text in the character array at the specified position, starting at the offset position of the character array and displaying a maximum length of characters.

The display position (x, y) given here is the starting coordinate for the baseline of the text, not the upper-left coordinate of the rectangular area of the text display.

There are three elements in a text font:
    • Fonts: Commonly used fonts are times New Roman, Symbol, Arial, italic and so on.
    • Style: There are three common styles: normal, bold and italic, with three constants: Font.plain (Normal), Font.Bold (bold), and Font.Italic (italic). Styles can be used in combination, for example, Font.bold+font.italic.
    • Font Size: The font sizes are the dimensions of the word, in points.

In the Java language, a font object with a class font. The font class is constructed by:
Font (String fontname,int style,int size), 3 parameters representing the font, style, and font size, respectively. For example, code:
Font FnA = new Font ("Fine Ming Ben", font.plain,12)
The font is set in fine-body, Normal style, 12-pound font size.

Other common methods of the font class:
    1. GetStyle (), returns the font style.
    2. GetSize (), returns the font size.
    3. GetName (), returns the font name.
    4. Isplain () to test whether the font is a normal font.
    5. IsBold () to test whether the font is bold.
    6. Isitalic () to test if the font is italic.

The "Example 12-1" applet uses 6 font strings to display the font of the content description itself.
1 Importjava.applet.*;2 Importjava.awt.*;3  Public classExample7_1extendsapplet{4Font F1 =NewFont ("Helvetica", font.plain,18);5Font F2 =NewFont ("Helvetica", font.bold,10);6Font F3 =NewFont ("Helvetica", font.italic,12);7Font F4 =NewFont ("Courier", font.plain,12);8Font f5 =NewFont ("Timesroman", font.bold+font.italic,14);9Font F6 =NewFont ("Dialog", font.italic,14);Ten      Public voidPaint (Graphics g) { OneSetSize (250,200); AG.setfont (F1);d rawstring ("18pt plain Helvetica", 5,20); -G.setfont (F2);d rawstring ("10pt bold Helvetica", 5,43); -G.setfont (F3);d rawstring ("12pt italic Helvetica", 5,58); theG.setfont (f4);d rawstring ("12pt plain Courier", 5,75); -G.setfont (F5);d rawstring ("14pt Bold & Italic Times Roman", 5,92); -G.setfont (f6);d rawstring ("14pt Italic Dialog", 5,111); -     } +}

Color is set with an object of class color, and there are two ways to produce various colors:
    • Color reserved for use with class color: black,red, white, yellow ... ;
    • The color is synthesized by the values of red, green, and Blue (RGB).

Common color-related methods:
(1) A color object is created with the constructor of the class color (int R, int g,int B), and the parameter r,g,b represents red, green, and blue, respectively, and their values are from 0 to 255.
(2) Using the class graphics method SetColor (Color c), the value of parameter C is described in table 12-1.
(3) Set the background color with the method SetBackground (color c) of the class component. Because the applet is a subclass of the component class, you can change the background color directly by using the SetBackground () method.
(4) Get the color using the method GetColor () of the class graphics.
Table 12-1 Color class pre-defined colors constants
Black Blue Cyan
Darkgray Gray Green
Lightgray Magenta Orange
Pink Red White
Yellow

The "Example 12-2" applet sets the color and paints the box, where the method of drawing the block is described in the next section.
1 Importjava.applet.*;2 Importjava.awt.*;3  Public classExample7_2extendsapplet{4      Public voidPaint (Graphics g) {5SetSize (380,200);6          for(inti=0;i<=10;i++){7Color Myredcolor =NewColor (i*25+5,0,0);8 G.setcolor (myredcolor);9G.fillrect (i*32+5,2,28,28);Ten         } One          for(inti=0;i<=10;i++){ AColor Mygreencolor =NewColor (0,i*25+5,0); - G.setcolor (mygreencolor); -G.fillrect (i*32+5,32,28,28); the         } -          for(inti=0;i<=10;i++){ -Color Mybluecolor =NewColor (0,0,i*25+5); - G.setcolor (mybluecolor); +G.fillrect (i*32+5,62,28,28); -         } +     } A}

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse EventsJava know how much (94) keyboard EventsJava know how much (95) Drawing Basics

Java know how much (96) set the font and color of the drawing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.