Lecture 26 - Graphics and Painting
Lecture 26 - Graphics and Painting
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.
import javax.swing.*;
import java.awt.*;
2
import java.awt.geom.*
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).
5
getGreen() Retrieves the green component of a Color object as an
integer (0-255).
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.
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.
label.setFont(font);
● These methods allow you to change the font dynamically based on user
interaction or other program logic.
7
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);