0% found this document useful (0 votes)
1 views3 pages

Graphics Color Fonts Java

The document explains how to work with Graphics, Color, and Fonts in Java AWT. It details the use of the Graphics class for drawing shapes, the Color class for setting colors, and the Font class for defining text styles. All drawing operations are performed within the paint(Graphics g) method of a Frame or Canvas.

Uploaded by

Vanisha Soni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Graphics Color Fonts Java

The document explains how to work with Graphics, Color, and Fonts in Java AWT. It details the use of the Graphics class for drawing shapes, the Color class for setting colors, and the Font class for defining text styles. All drawing operations are performed within the paint(Graphics g) method of a Frame or Canvas.

Uploaded by

Vanisha Soni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Working with Graphics, Color, and Fonts

in Java (AWT)
1. Graphics in AWT
The Graphics class in Java AWT is used to draw shapes like lines, rectangles, ovals, and text
on a component (like a Frame or Canvas). The paint() method is used to draw using
Graphics.

Example: Drawing Shapes

import [Link].*;

public class GraphicsExample extends Frame {


public void paint(Graphics g) {
[Link](50, 50, 200, 50); // Draw line
[Link](50, 70, 100, 50); // Draw rectangle
[Link](50, 130, 100, 50); // Draw oval
}

public static void main(String[] args) {


GraphicsExample f = new GraphicsExample();
[Link](300, 250);
[Link](true);
}
}

2. Color in AWT
The Color class in AWT is used to set the color for drawing shapes or text. You can use
predefined colors (like [Link]) or create custom colors.

Example: Using Colors

import [Link].*;

public class ColorExample extends Frame {


public void paint(Graphics g) {
[Link]([Link]);
[Link](50, 50, 100, 50); // Filled red rectangle
[Link]([Link]);
[Link]("Blue Text", 50, 130); // Text in blue
}

public static void main(String[] args) {


ColorExample f = new ColorExample();
[Link](300, 200);
[Link](true);
}
}

3. Fonts in AWT
The Font class is used to define the style and size of text drawn using Graphics. You can
create fonts using new Font(name, style, size).

Example: Setting Fonts

import [Link].*;

public class FontExample extends Frame {


public void paint(Graphics g) {
Font f = new Font("Arial", [Link], 20);
[Link](f);
[Link]("This is bold Arial", 50, 100);
}

public static void main(String[] args) {


FontExample f = new FontExample();
[Link](300, 150);
[Link](true);
}
}

Summary

- Use Graphics to draw shapes or text.


- Use Color to set drawing color.
- Use Font to change text style and size.
- All drawing happens inside the paint(Graphics g) method of a Frame or Canvas.

You might also like