Java Drawing Strings Colors Fonts

Java Drawing Strings Colors Fonts

Summary: By the end of this tutorial "Java Drawing Strings Colors Fonts", you will come to know to draw strings in different colors and fonts in graphics.

In the "Drawing Strings" program, the strings are drawn in default black color and with font Dialog. Now, let us redraw the strings with beautiful colors and fonts to make them more attractive. Here, we use two classes java.awt.Color and java.awt.Font.

Program on Java Drawing Strings Colors Fonts
import java.awt.*;   
public class AttractiveStrings extends Frame
{			
  String s1, s2, s3;
  Color clr1, clr2, clr3;
  Font f1, f2, f3;

  public AttractiveStrings() 
  {                // converting String reference variables to objects	    
    s1 = "way2java.com";
    s2 = "Hyderabad";
    s3 = "India";
                                      // Color reference variables to objects  
    clr1 = new Color(200, 50, 100);  
    clr2 = new Color(180, 150, 200);
    clr3 = new Color(0.82f, 0.1f, 0.6f);      	
                                      // Font reference variables to objects
    f1 = new Font("DialogInput", Font.BOLD, 20);
    f2 = new Font("SansSerif", Font.ITALIC, 22);
    f3 = new Font("Monospaced", Font.BOLD + Font.ITALIC, 25); 

    setTitle("Attractive Strings"); 
    setSize(425,375);    
    setVisible(true);  
  }
  public void paint(Graphics g)
  {
    g.drawString(s1, 50, 60);  
                                  // modifying color and font to clr1 and f1
    g.setColor(clr1);     
    g.setFont(f1); 
    g.drawString(s2, 50, 90);
                                            // modifying color and font to clr2 and f2 
    g.setColor(clr2);
    g.setFont(f2);
    g.drawString(s3, 50, 120);  
                                            // modifying color and font to clr3 and f3
    g.setColor(clr3);
    g.setFont(f3);
    g.drawString(s1, 50, 160); 
                                            // using anonymous Color and Font objects 
    g.setColor(new Color(225, 100, 80));
    g.setFont(new Font("SansSerif", Font.PLAIN, 22));
    g.drawString("S.Nageswara Rao", 50, 200);
                                            // using one of 13 predefined colors
    g.setColor(Color.blue);      		
    g.drawString("Best wishes of the day", 50, 230);
  }                                    
  public static void main(String args[])
  {
    new AttractiveStrings();
  }
}


Java Drawing Strings Colors Fonts
Output screen of AttractiveStrings.java of Java Drawing Strings Colors Fonts

In the global position, reference variables are created of three strings (s1, s2 and s3), three colors (clr1, clr2 and clr3) and three fonts (f1, f2 and f3) and are converted into objects in the constructor.

g.drawString(s1, 50, 60);

The string s1 is drawn in default black color and default font Dialog, plain, 12.

g.setColor(clr1);
g.setFont(f1);
g.drawString(s2, 50, 90);

setColor() and setFont() are the methods of Graphics class with which drawing's color and font can be changed. The setColor() takes a Color object as parameter and setFont() takes a Font object as parameter. Now whatever graphics are drawn, like string s2, are painted in color clr1 and font f1 until they are changed.

g.setColor(new Color(225, 100, 80));
g.setFont(new Font("SansSerif", Font.PLAIN, 22));

The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code.

In the above statements, anonymous objects of Color and Font are created and passed to setColor() and setFont() methods. Usage of anonymous objects saves memory on the client machine. In AWT, we use very frequently anonymous objects. Observe, in the constructor we created an anonymous object of AttractiveStrings class and called the constructor.

Note: Using the color range outside 0 to 255 throws IllegalArgumentException.

2 thoughts on “Java Drawing Strings Colors Fonts”

Leave a Comment

Your email address will not be published.