0% found this document useful (0 votes)
3 views

Lecture 26 - Graphics and Painting

The document provides an overview of Java's coordinate system, graphic contexts, and graphic objects essential for rendering graphics in GUI applications. It explains how to use the Graphics class for drawing shapes and colors, along with methods for manipulating colors and fonts. Additionally, it covers creating and customizing font objects and rendering settings for improved text display.

Uploaded by

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

Lecture 26 - Graphics and Painting

The document provides an overview of Java's coordinate system, graphic contexts, and graphic objects essential for rendering graphics in GUI applications. It explains how to use the Graphics class for drawing shapes and colors, along with methods for manipulating colors and fonts. Additionally, it covers creating and customizing font objects and rendering settings for improved text display.

Uploaded by

alfredjoso847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

The Coordinate system

● Before delving into drawing in Java, it is essential to grasp the concept of


Java's coordinate system.
● This system serves as a framework for identifying all possible points on the
screen.
● By default, the upper-left corner of a GUI component, such as an applet or a
window, is designated as the coordinates (0, 0).
● A coordinate pair comprises an x-coordinate (representing the horizontal
position) and a y-coordinate (representing the vertical position).
● The x-coordinate indicates the horizontal distance from the upper-left corner,
moving towards the right.
● Conversely, the y-coordinate signifies the vertical distance from the upper-left
corner, moving downwards.
● The x-axis encompasses all horizontal coordinates, while the y-axis
encompasses all vertical coordinates.

Graphic Contexts and Graphic Objects


● In Java, graphic contexts and graphic objects provide a means to render and
manipulate graphical elements on a graphical user interface (GUI).

1
1. Graphic Contexts:
○ A graphic context represents the drawing environment or canvas on
which graphical operations can be performed.
○ It provides a set of methods and properties for controlling the
attributes and behavior of graphics rendering.
○ In Java, the `Graphics` class is commonly used as a graphic context for
rendering 2D graphics in AWT (Abstract Window Toolkit) and Swing.

2. Graphic Objects:
○ Graphic objects are the fundamental building blocks used to create
graphical elements on the GUI.
○ These objects encapsulate the visual properties and behavior of
individual graphical components such as lines, shapes, images, and
text.
○ Examples of graphic objects include `Line2D`, `Rectangle`, `Ellipse2D`,
and `Image`.
○ These are contained within the sub-package awt.geom therefore you
need to import java.awt.geom.* before you can use them
○ Graphic objects can be instantiated and manipulated within the
graphic context to define the appearance and arrangement of
graphical elements.

● The typical workflow involves obtaining a graphic context, such as an


instance of the `Graphics` class, from a GUI component (e.g., `JPanel` or
`Canvas`).
● Then, graphic objects are created and drawn onto the graphic context using
its provided methods, such as `drawLine()`, `fillRect()`, or `drawImage()`.
● These methods allow you to specify attributes like position, size, color, and
style to customize the appearance of the drawn elements.

import javax.swing.*;
import java.awt.*;

2
import java.awt.geom.*

public class GraphicExample extends JPanel {


@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Get the graphic context


Graphics2D g2d = (Graphics2D) g;

// Create and draw a line


Line2D line = new Line2D.Double(50, 50, 200, 200);
g2d.setColor(Color.RED);
g2d.draw(line);

// Create and draw a rectangle


Rectangle rectangle = new Rectangle(250, 100, 150, 100);
g2d.setColor(Color.BLUE);
g2d.fill(rectangle);
}

public static void main(String[] args) {


JFrame frame = new JFrame("Graphic Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.add(new GraphicExample());
frame.setVisible(true);
}
}

3
● In this example, the `paintComponent()` method is overridden to perform
custom drawing on the graphic context (`Graphics2D`).
● Two graphic objects, a line and a rectangle, are created using `Line2D` and
`Rectangle` respectively.
● The `Graphics2D` methods `setColor()` and `draw()` are used to set the color
and draw the line, while `setColor()` and `fill()` are used to set the color and fill
the rectangle.
● When the GUI component is rendered, the `paintComponent()` method is
automatically called, and the defined graphics operations are executed within
the provided graphic context, resulting in the display of the line and the filled
rectangle on the GUI.

Color Controls
● Colors play a significant role in enhancing the visual appeal of a program and
conveying meaning.
● Take, for instance, a traffic light that utilizes different colored lights: red for
stop, yellow for caution, and green for go.

4
● In Java, the Color class provides methods and constants for manipulating
colors within a program.
● Each color is constructed using three components: red, green, and blue,
collectively known as RGB values.
● These components can be specified as integers ranging from 0 to 255 or as
floating-point values ranging from 0.0 to 1.0. The first component represents
the amount of red, the second represents the amount of green, and the third
represents the amount of blue.
● A higher RGB value signifies a greater intensity of that specific color. Java
allows programmers to choose from a vast range of approximately 16.7
million colors (256 × 256 × 256).
● However, it's important to note that not all computer systems are capable of
displaying the entire spectrum of these colors.
● Below is a table of Color methods and color-related graphics methods

Method Description

Color(int r, int g, int b) Creates a new Color object with RGB values specified as
integers (0-255).

Color(float r, float g, float Creates a new Color object with RGB values specified as
b) floats (0.0-1.0).

Color(int rgb) Creates a new Color object with an RGB value specified
as an integer.

Color(float r, float g, float Creates a new Color object with RGB and alpha
b, float alpha) (transparency) values specified as floats (0.0-1.0).

getColor() Retrieves the RGB value of a Color object as an integer.

getRed() Retrieves the red component of a Color object as an


integer (0-255).

5
getGreen() Retrieves the green component of a Color object as an
integer (0-255).

getBlue() Retrieves the blue component of a Color object as an


integer (0-255).

getRGB() Retrieves the RGB value of a Color object as an integer.

brighter() Creates a new Color object that is a brighter shade of the


original color.

darker() Creates a new Color object that is a darker shade of the


original color.

Font control
● Fonts determine the typeface, size, style, and other attributes of the text
displayed on the screen.
● To control fonts in Java GUI programming, you typically work with the `Font`
class from the `java.awt` package.

Creating a Font Object


● You can create a `Font` object by specifying the desired typeface, style, and
size.
● The `Font` class provides several constructors to accommodate different
variations.
● Class Font’s constructor takes three arguments—the font name, font style and
font

Font font = new Font("Arial", Font.BOLD, 16);

Setting the Font of a Component

6
● Once you have a `Font` object, you can apply it to a graphical component,
such as a label, button, or text field.
● Most GUI components in Java have a `setFont()` method that allows you to
set the font.

JLabel label = new JLabel("Hello, World!");

label.setFont(font);

Adjusting Font Attributes


● The `Font` class provides various methods to modify the attributes of a font.
Some commonly used methods include:
○ deriveFont(float size): Creates a new `Font` object with the specified
size.
○ deriveFont(int style): Creates a new `Font` object with the specified
style (e.g., `Font.BOLD`, `Font.ITALIC`).
○ deriveFont(int style, float size): Creates a new `Font` object with the
specified style and size.
○ isBold(), isItalic(), isPlain(): Checks if the font has the specified attribute.

● These methods allow you to change the font dynamically based on user
interaction or other program logic.

Customizing Font Rendering


● In addition to specifying the basic font attributes, you can also customize font
rendering, such as antialiasing and fractional metrics, for smoother text
display.
● This is achieved using the `Graphics2D` class and its associated rendering
hints.

Graphics2D g2d = (Graphics2D) graphics; // Assuming you have a Graphics


object

7
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

You might also like