Java Utililty Methods Graphics Draw

List of utility methods to do Graphics Draw

Description

The list of methods to do Graphics Draw are organized into topic(s).

Method

voiddrawPaintedShape(Graphics2D graphics, Shape shape, Paint paint, Rectangle2D paintBounds, Stroke stroke)
Draws a filled Shape with the specified Paint object.
if (shape == null) {
    return;
if (stroke == null) {
    stroke = graphics.getStroke();
shape = stroke.createStrokedShape(shape);
fillPaintedShape(graphics, shape, paint, paintBounds);
...
voiddrawPowerScaleLabel(Graphics g, int base, int power, int x, int y, boolean yAxisP)

Draws a scale label of the form of the base base take to the power of power with the power displayed as a superscript of the base using the graphics in the Graphics object g.

if (g == null) {
    return;
String baseString = Integer.toString(base);
String powerString = Integer.toString(power);
FontMetrics metrics = g.getFontMetrics();
int fontHeight = metrics.getHeight();
int baseWidth = metrics.stringWidth(baseString);
...
voiddrawProgressBar(Graphics2D g, final int x, final int y, final int width, final int height, final Color main, final Color progress, final int alpha, final int percentage)
draw Progress Bar
g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
final GradientPaint base = new GradientPaint(x, y, new Color(200, 200, 200, alpha), x, y + height, main);
final GradientPaint overlay = new GradientPaint(x, y, new Color(200, 200, 200, alpha), x, y + height,
        progress);
if (height > width) {
    g.setPaint(base);
    g.fillRect(x, y, width, height);
    g.setPaint(overlay);
...
voiddrawRainbow(Graphics2D g2, int x, int y, int Width, int Height, int Mode)
draw Rainbow
Vector Colors = new Vector();
GradientPaint gp;
switch (Mode) {
case 1:
    Colors.add(new Color(255, 000, 000));
    Colors.add(new Color(255, 255, 000));
    Colors.add(new Color(000, 255, 000));
    Colors.add(new Color(000, 255, 255));
...
voiddrawRequiredMarker(Graphics2D g2, int x, int y, int iconSize)
draw Required Marker
Font font = new Font("Serif", Font.BOLD, 2 * iconSize);
g2.setFont(font);
g2.drawString("!", x - iconSize + 2, y + iconSize - 2);
voiddrawRTriangle(Graphics g, Color color, int x, int y, int r)
draw R Triangle
int x1 = x;
int y1 = y + r;
int x2 = x - (int) (r * Math.cos(Math.PI / 6.0));
int y2 = y - (int) (r * Math.sin(Math.PI / 6.0));
int x3 = x + (int) (r * Math.cos(Math.PI / 6.0));
int y3 = y - (int) (r * Math.sin(Math.PI / 6.0));
int[] xpos = new int[3];
xpos[0] = x1;
...
voiddrawScaleTick(Graphics g, int x, int y, boolean yAxisP, int length)

Draws a scale tick of length length from (x, y) to the left if horizontalP is true or down if horizontalP is false using the graphics in the Graphics2D object g.

if (g == null) {
    return;
if (yAxisP) {
    g.drawLine(x, y, x - length, y);
} else {
    g.drawLine(x, y, x, y + length);
voiddrawScrollBar(Graphics g, int which, int direction, int x, int y, int fmWidth, int fmHeight, Color fg, Color bg)
draw Scroll Bar
Color oldColor = g.getColor(); 
if (which == INSET) {
    g.setColor(bg);
    g.fillRect(x, y, fmWidth, fmHeight);
    g.setColor(fg);
    g.drawLine(x, y, x, y + fmHeight);
    g.drawLine(x + fmWidth - 1, y, x + fmWidth - 1, y + fmHeight);
if (which == RAISED) {
    g.setColor(bg);
    g.fillRect(x, y, fmWidth, fmHeight);
    g.setColor(fg);
    g.drawLine(x, y, x, y + fmHeight);
    g.drawLine(x + fmWidth - 1, y, x + fmWidth - 1, y + fmHeight);
if (direction == 1) {
    g.setColor(fg.brighter());
    g.drawLine(x + (fmWidth / 2), y + 2, x + 2, y + fmHeight - 4);
    g.setColor(fg.darker());
    g.drawLine(x + (fmWidth / 2), y + 2, x + fmWidth - 2, y + fmHeight - 4);
    g.drawLine(x + 2, y + fmHeight - 4, x + fmWidth - 2, y + fmHeight - 4);
    g.setColor(fg);
    g.drawLine(x, y, x + fmWidth - 1, y);
    g.drawLine(x, y + fmHeight - 1, x + fmWidth - 1, y + fmHeight - 1);
if (direction == 2) {
    g.setColor(fg.brighter());
    g.drawLine(x + (fmWidth / 2), y + fmHeight - 4, x + 2, y + 2);
    g.drawLine(x + 2, y + 2, x + fmWidth - 2, y + 2);
    g.setColor(fg.darker());
    g.drawLine(x + (fmWidth / 2), y + fmHeight - 4, x + fmWidth - 2, y + 2);
    g.setColor(fg);
    g.drawLine(x, y, x + fmWidth, y);
    g.drawLine(x, y + fmHeight - 1, x + fmWidth, y + fmHeight - 1);
if (direction == 3) {
    g.setColor(fg);
    g.fillRect(x + 2, y, fmWidth - 4, fmHeight);
g.setColor(oldColor);
voiddrawSelectionBox(Graphics g)
draw Selection Box
int width = g.getClipBounds().width;
int height = g.getClipBounds().height;
g.setColor(Color.GREEN);
g.drawRect(3, 3, width - 6, height - 6);
voiddrawSelectionPoint(Graphics g, Point p)
draw Selection Point
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(boldStroke);
g2.setPaint(selectionColor);
Shape s = new Ellipse2D.Double(p.x - 5, p.y - 5, 10, 10);
g2.draw(s);