Ch03g Graphics
Ch03g Graphics
Supplement 3G
Graphics
Graphical objects
We will draw graphics in Java using 3 kinds of objects:
DrawingPanel: A window on the screen.
Not part of Java; provided by the authors. See class web
site.
DrawingPanel
"Canvas" objects that represents windows/drawing
surfaces
To create a window:
DrawingPanel name = new DrawingPanel(width, height);
Example:
DrawingPanel panel = new DrawingPanel(300, 200);
Graphics
"Pen" or "paint brush" objects to draw lines and shapes
Access it by calling getGraphics on your DrawingPanel.
Graphics g = panel.getGraphics();
Coordinate system
Each (x, y) position is a pixel ("picture element").
Position (0, 0) is at the window's top-left corner.
x increases rightward and the y increases downward.
x+
(200, 100)
y+
Graphics methods
Method name
Description
g.drawString(text, x, y);
g.setColor(Color);
Color
Specified as predefined Color class constants:
Color.CONSTANT_NAME
where CONSTANT_NAME is one of:
BLACK, BLUE,
CYAN,
GREEN, LIGHT_GRAY,
PINK,
RED,
WHITE,
DARK_GRAY, GRAY,
MAGENTA,
ORANGE,
YELLOW
Using colors
Pass a Color to Graphics object's setColor method
Subsequent shapes will be drawn in the new color.
g.setColor(Color.BLACK);
g.fillRect(10, 30, 100, 50);
g.drawLine(20, 0, 10, 30);
g.setColor(Color.RED);
g.fillOval(60, 40, 40, 70);
Outlined shapes
To draw a colored shape with an outline, first fill it,
then draw the same shape in the outline color.
import java.awt.*;
Superimposing shapes
When 2 shapes occupy the same pixels, the last drawn
"wins."
import java.awt.*;
public class Car {
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(200, 100);
panel.setBackground(Color.LIGHT_GRAY);
Graphics g = panel.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(10, 30, 100, 50);
g.setColor(Color.RED);
g.fillOval(20, 70, 20, 20);
g.fillOval(80, 70, 20, 20);
g.setColor(Color.CYAN);
g.fillRect(80, 40, 30, 20);
11
Zero-based loops
Beginning at 0 and using < can make coordinates easier.
DrawingPanel panel = new DrawingPanel(150, 140);
Graphics g = panel.getGraphics();
// horizontal line of 5 20x20 rectangles starting
// at (11, 18); x increases by 20 each time
for (int i = 0; i < 5; i++) {
g.drawRect(11 + 20 * i, 18, 20, 20);
}
14
// cyan background
g.setColor(Color.WHITE);
g.drawString("BJP", 70, 55);
16
18
19
20
Resizable solution,
cont'd.
...
// Draws a book of the given size at the given position.
public static void drawBook(Graphics g, int x, int y, int size) {
g.setColor(Color.CYAN);
g.fillRect(x, y, size, size);
// cyan background
g.setColor(Color.WHITE);
// white "bjp" text
g.drawString("BJP", x + size/2, y + size/5);
g.setColor(new Color(191, 118, 73));
for (int i = 0; i < 10; i++) {
//
g.fillRect(x,
//
y + size/10 * i,
//
size/10 * (i + 1), //
size/10 - 1);
//
}
orange "bricks"
x
y
width
height
}
}
21
Polygon
Objects that represent arbitrary shapes
Add points to a Polygon using its addPoint(x, y)
method.
Example:
DrawingPanel p = new DrawingPanel(100, 100);
Graphics g = p.getGraphics();
g.setColor(Color.GREEN);
Polygon poly = new Polygon();
poly.addPoint(10, 90);
poly.addPoint(50, 10);
poly.addPoint(90, 90);
g.fillPolygon(poly);
22
DrawingPanel methods
panel.clear();
Erases any shapes that are drawn on the drawing panel.
panel.setWidth(width);
panel.setHeight(height);
panel.setSize(width, height);
Changes the drawing panel's size to the given value(s).
panel.save(filename);
Saves the image on the panel to the given file (String).
panel.sleep(ms);
Pauses the drawing for the given number of milliseconds.
23