Home>

There are three main ways to display text in java drawing:

(1) drawstring (string str, int x, int y):display the string at the specified position.

(2) drawchars (char data [], int offset, int length, int x, int y):display the text in the character array at the specified position,Starting from the offset position of the character array,Display up to length characters.

(3) drawbytes (byte data [], int offset, int length, int x, int y), display the text in the character array at the specified position,Starting from the offset position of the character array,Display up to length characters.

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

There are three elements to a text font:

Fonts:Commonly used fonts include times new roman, symbol, Song style, Kai style, etc.

Style:There are three commonly used styles:normal, bold and italic;Represented by 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 size is the size of the word.The unit is pounds.

In the java language, use the font object type.The font class constructors are:

font (string fontname, int style, int size), the three parameters represent the font, style and size respectively.For example, the code:

font fna=new font ("Detailed version", font.plain, 12)

The fonts set are:detailed, normal style, 12 point font size.

Other common methods of the font class:

getstyle (), returns the font style. getsize (), returns the font size. getname (), returns the font name. isplain (), tests whether the font is normal. isbold (), tests whether the font is bold. isitalic (), tests whether the font is italic.

[Example] Applet uses 6 font strings,Displays 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);drawstring ("18pt plain helvetica", 5,20);
    g.setfont (f2);drawstring ("10pt bold helvetica", 5,43);
    g.setfont (f3);drawstring ("12pt italic helvetica", 5,58);
    g.setfont (f4);drawstring ("12pt plain courier", 5,75);
    g.setfont (f5);drawstring ("14pt bold&italic times roman", 5,92);
    g.setfont (f6);drawstring ("14pt italic dialog", 5,111);
  }
}

Set color with color class object,There are two ways to generate various colors:

Predetermined colors with class color:black, red, white, yellow ...;

Colors are synthesized from red, green, and blue (rgb) values.

Common methods related to color:

(1) Create a color object using the color-like construction method color (int r, int g, int b),The parameters r, g, b represent red, green and blue,Their values ​​are from 0 to 255.

(2) Use the method setcolor (color c) of the graphics class. For the value of parameter c, see Table 12-1.

(3) Use the method setbackground (color c) of the class component to set the background color.Because applets are subclasses of component classes,You can change the background color directly using the setbackground () method.

(4) Get color using the method getcolor () of the graphics class.

color class predefined color constants

[Example] The applet sets colors and paints squares.The method of drawing squares will be described in the following sections.

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);
    }
  }
}
  • Previous Solve the problem of "It is not safe to rely on the system ’s timezone settings" in php
  • Next Analysis of the use of Java drawing mode