Code for an applet drawing example.
import java.awt.*;
import java.applet.*;
import java.util.*;
public class Drawing extends Applet {
public void paint(Graphics g) {
// using a more modern 2D graphics
Graphics2D g2 = (Graphics2D) g;
// get the size of the applet
// NOTE: NOT A GRAPHICS METHOD!
Dimension size = getSize();
// determine the size of the largest circle that fits into the
// the applet
int width = size.width;
int height = size.height;
int diameter;
if (width < height) diameter = width;
else diameter = height;
// set rendering hints to improve quality
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// set the background
g2.setColor(Color.white);
g2.fillRect(0,0,size.width,size.height);
Color [] cc = {Color.orange, Color.blue, Color.red,
Color.cyan, Color.yellow, Color.magenta,
Color.green, Color.pink};
// starting angles:
int [] angles = new int[8];
for (int i = 0; i < 8; ++i) {
angles[i] = i * 45;
}
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 8; ++j) {
g2.setColor(cc[j]);
g2.fillArc(0,0,diameter,diameter,angles[j],(45 * (9 - i))/9);
// reset the color for the next time:
cc[j] = cc[j].darker();
}
}
}
}
This is an example from CSci 1211 course.