How to use Java to set fonts and colors _java

Source: Internet
Author: User
Tags int size

There are three main ways to display text in a Java drawing:
(1) DrawString (string Str,int x,int y): Displays a string at the specified location.
(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 up to 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 up to length characters.

The display position shown here (X,y) is the starting coordinate of the baseline for the text, not the upper-left corner of the rectangular area where the text is displayed.

There are three elements of a text font:
Font: Commonly used fonts have times New Roman, Symbol, song, italics and so on.
Style: There are three kinds of common styles: normal, Bold and italic, respectively, 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.
Size: The size of the word, measured in points.

In the Java language, type Font object font. The font class constructs methods are:
Font (String fontname,int style,int size), 3 parameters representing font, style, and font size, respectively. For example, code:
Font FnA = new Font ("Fine-ming", font.plain,12)
Set the font is: fine body, Normal style, 12-point font size.

Other common methods for the font class:

    • GetStyle (), returns the font style.
    • GetSize (), returns the font size.
    • GetName (), returns the font name.
    • Isplain () to test whether the font is normal.
    • IsBold () to test whether the font is bold.
    • Isitalic () to test whether the font is italic.

The example applet uses 6 font strings to display the font of the content description itself.

Import java.applet.*;
Import java.awt.*;
public class Example7_1 extends applet{
  font f1 = new Font ("Helvetica", font.plain,18);
  Font F2 = new Font ("Helvetica", font.bold,10);
  Font f3 = new Font ("Helvetica", font.italic,12);
  Font f4 = new Font ("Courier", font.plain,12);
  Font f5 = new Font ("Timesroman", font.bold+font.italic,14);
  Font f6 = new Font ("Dialog", font.italic,14);
  public void Paint (Graphics g) {
    setSize (250,200);
    G.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);
    G.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);
  }

There are two ways to create colors with objects of class color:
Color of the class: Black,red, White, yellow ... ;
The color is synthesized by the value of red, green, and Blue (RGB).

Common color-related methods:
(1) Create a color object with a class color construction method Color (int R, int g,int B), and the parameter r,g,b represent red, green, and blue, respectively, from 0 to 255.
(2) SetColor (Color C) with the class graphics method, and the value of parameter C is shown in table 12-1. The
(3) sets the background color with the class component method SetBackground (color C). Because the applet is a subclass of the component class, you can change the background color directly using the SetBackground () method. The
(4) GetColor () Gets the color using the class graphics method.
Color class predefined color constants

Example Small application sets colors and paints squares, where the method of drawing squares is described in a later section.

Import java.applet.*;
Import java.awt.*;
public class Example7_2 extends applet{public
  void Paint (Graphics g) {
    setSize (380,200);
    for (int i=0;i<=10;i++) {
      Color myredcolor = new color (i*25+5,0,0);
      G.setcolor (Myredcolor);
      G.fillrect (i*32+5,2,28,28);
    }
    for (int i=0;i<=10;i++) {
      Color mygreencolor = new color (0,i*25+5,0);
      G.setcolor (Mygreencolor);
      G.fillrect (i*32+5,32,28,28);
    }
    for (int i=0;i<=10;i++) {
      Color mybluecolor = new color (0,0,i*25+5);
      G.setcolor (Mybluecolor);
      G.fillrect (i*32+5,62,28,28);}}

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.