public class Font extends Object implements Serializable
Font object can be created using the above three parameters in the font constructor.
Font f = new Font(String fontName, int fontStyle, int fontSize);
If either of the above five are used in the object creation, then default Dialog, Plain and 12 size is executed.
public static final int PLAIN
public static final int BOLD
public static final int ITALIC
BOLD|ITALIC is a combination of bold and italic font. To underline the below fonts, use FontMetrics Class.
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //public class UsingFonts { public class SPlessons extends Frame { public void paint(Graphics g) { Font f = new Font("Monospaced", Font.ITALIC, 12); g.setFont(f); g.drawString("Font Particulars: " + g.getFont(), 15, 60); g.drawString("Font Name: " + f.getFontName(), 15, 100); g.drawString("Font Style: " + f.getStyle(), 15, 75); g.drawString("Font Size: " + f.getSize(), 15, 100); g.drawString("isBold(): " + f.isBold(), 15, 120); g.drawString("isItalic(): " + f.isItalic(), 15, 150); g.drawString("isPlain(): " + f.isItalic(), 15, 160); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } public static void main(String args[]) { Frame myFrame = new SPlessons(); myFrame.setSize(500, 250); myFrame.setVisible(true); } }
Output
public class Color extends Object implements Paint, Serializable
Syntax:Â Color c = new Color(int red, int green, int blue);
HSB Model:Â Hue, Saturation and Brightness forms the HSB Model. Color Constructor will be passed with the values ranging between 0.0f and 1.0f. If the value is not between the range, IllegalArgumentException is thrown.
Syntax:Â Color c = new Color(float red, float green, float blue);
public class UsingColors extends Frame { public void paint(Graphics g) { Color clr1 = new Color(200, 25, 100); g.setColor(clr1); g.drawString("Color set to graphics: " + g.getColor(), 50, 60); g.drawString("Red component: " + clr1.getRed(), 50, 80); g.drawString("Green component: " + clr1.getGreen(), 50, 100); g.drawString("Blue component: " + clr1.getBlue(), 50, 120); } public static void main(String args[]) { Frame f1 = new UsingColors(); f1.setSize(300, 200); f1.setVisible(true); } }
Output
Color | Red | Green | Blue | Hue | Saturation | Brightness |
---|---|---|---|---|---|---|
Black | 0 | 0 | 0 | 0 | 0 | 0 |
Blue | 0 | 0 | 255 | .666667 | 1 | 1 |
Cyan | 0 | 255 | 255 | .5 | 1 | 1 |
Green | 0 | 255 | 0 | .333333 | 1 | 1 |
Orange | 255 | 200 | 0 | .130719 | 1 | 1 |
Pink | 255 | 175 | 175 | 0 | 1 | 1 |
Red | 255 | 0 | 0 | 0 | 1 | 1 |
White | 255 | 255 | 255 | 0 | 0 | 1 |
Yellow | 255 | 255 | 0 | .166667 | 1 | 1 |