Color and Font
By default every Graphics call draws in black using whatever font the platform picked. Two
classes from java.awt - Color and Font - let you change both, and
you set them on the Graphics object itself so every subsequent drawing call picks them up.
java.awt.Color
Color objects are immutable. Build one from red/green/blue components (each 0-255), or use
one of the ready-made constants:
Color purple = new Color(128, 0, 128); // r, g, b
Color blue = Color.BLUE; // a built-in constant
Common constants: Color.RED, Color.BLUE, Color.GREEN, Color.BLACK, Color.WHITE, Color.YELLOW, Color.GRAY.
To actually use a color, call setColor() on the Graphics object before drawing - it stays in effect for every draw call after it, until changed again:
g.setColor(Color.RED);
g.fillOval(100, 60, 40, 40); // drawn in red
java.awt.Font
A Font bundles a family name, a style, and a point size. Style is one of the constants
Font.PLAIN, Font.BOLD, Font.ITALIC - or the two combined with a
bitwise OR:
Font titleFont = new Font("Arial", Font.BOLD, 18);
Font emphasis = new Font("Arial", Font.BOLD | Font.ITALIC, 14);
Apply it with setFont(), the same way you apply a color:
g.setFont(titleFont);
g.drawString("Welcome to PaintApplet!", 20, 30);
| Call | Effect |
|---|---|
g.setColor(Color c) | All later draw/fill calls use this color, until changed again |
g.setFont(Font f) | All later drawString calls use this font, until changed again |
Extending PaintApplet: A Title Font and a Cycling Color
Add two fields - a currentColor used for the shapes, and a titleFont used for
the heading text - and set them at the top of paint(). Then add a small cycleColor()
method that steps currentColor through red, green, and blue:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class PaintApplet extends Applet {
private Color currentColor = Color.BLUE;
private Font titleFont = new Font("Arial", Font.BOLD, 18);
public void init() {
setBackground(Color.WHITE);
}
public void paint(Graphics g) {
g.setFont(titleFont);
g.setColor(Color.BLACK);
g.drawString("Welcome to PaintApplet!", 20, 30);
g.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
g.setColor(currentColor);
drawShapes(g);
}
private void drawShapes(Graphics g) {
g.drawLine(20, 50, 120, 50);
g.drawRect(20, 60, 60, 40);
g.fillOval(100, 60, 40, 40);
int[] xPoints = {200, 230, 260};
int[] yPoints = {100, 60, 100};
g.drawPolygon(xPoints, yPoints, 3);
}
public void cycleColor() {
if (currentColor == Color.BLUE) currentColor = Color.RED;
else if (currentColor == Color.RED) currentColor = Color.GREEN;
else currentColor = Color.BLUE;
repaint();
}
}
Why repaint(), Not paint()
cycleColor() ends with a call to repaint(), not paint(). You almost
never call paint() directly. repaint() asks AWT to schedule a
redraw as soon as it can, batching it in with the browser's/appletviewer's own redraw cycle - which is
exactly what you want, since the windowing system may also need to repaint the applet for reasons that
have nothing to do with your code (a window was moved over it, it was resized, and so on). Calling
paint() yourself would draw once, immediately, and skip that coordination entirely.
PaintApplet into
an actual drawing pad the user can click and drag on.