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

voiddrawBubbles(Graphics g, int nCode)
draw Bubbles
int width = g.getClipBounds().width;
int height = g.getClipBounds().height;
g.setColor(Color.BLUE);
g.fillRect(1, 1, width - 2, height - 2);
Random r = new Random(nCode);
int nBubbles = r.nextInt(3) + 2;
for (int i = 0; i < nBubbles; i++) {
    int xB = (int) (r.nextDouble() * width);
...
voiddrawChar(char c, int x, int y, Graphics g)
draw Char
BufferedImage img = null;
switch (c) {
case ' ':
    img = GUI_GLYPH_CHARS[0];
    break;
case '\u0263':
    img = GUI_GLYPH_CHARS[1];
    break;
...
intdrawChars(JComponent c, Graphics g, char[] data, int offset, int length, int x, int y)
The following draw functions have the same semantic as the Graphics methods with the same names.
if (length <= 0) { 
    return x;
int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
if (isPrinting(g)) {
    Graphics2D g2d = getGraphics2D(g);
    if (g2d != null) {
        FontRenderContext deviceFontRenderContext = g2d.getFontRenderContext();
...
voiddrawCheck(Graphics g, int x, int y)
draw Check
g.translate(x, y);
g.drawLine(3, 5, 3, 5);
g.fillRect(3, 6, 2, 2);
g.drawLine(4, 8, 9, 3);
g.drawLine(5, 8, 9, 4);
g.drawLine(5, 9, 9, 5);
g.translate(-x, -y);
voiddrawCheckerPattern(Graphics g_, int checkerSize)
draw Checker Pattern
Graphics2D g = (Graphics2D) g_;
g.setColor(new Color(150, 150, 200));
Rectangle clipBounds = g.getClipBounds();
g.fillRect(clipBounds.x, clipBounds.y, clipBounds.width, clipBounds.height);
g.setColor(Color.GRAY);
for (int i = floor(clipBounds.x, checkerSize); i < clipBounds.width + clipBounds.x; i += checkerSize)
    for (int j = floor(clipBounds.y, checkerSize); j < clipBounds.height + clipBounds.y; j += checkerSize)
        if (((i + j) / checkerSize) % 2 == 0)
...
voiddrawColors(Color[] colors, Graphics g, int x1, int y1, int x2, int y2, int direction)
draw Colors
for (int a = 0; a < colors.length; a++) {
    g.setColor(colors[colors.length - a - 1]);
    if (direction == SwingConstants.SOUTH) {
        g.drawLine(x1, y1 - a, x2, y2 - a);
    } else if (direction == SwingConstants.NORTH) {
        g.drawLine(x1, y1 + a, x2, y2 + a);
    } else if (direction == SwingConstants.EAST) {
        g.drawLine(x1 - a, y1, x2 - a, y2);
...
voiddrawCoordinateAxes(Graphics2D g, Component comp)
draws a pair of coordinate axes through the origin.
AffineTransform at = g.getTransform();
AffineTransform bt = new AffineTransform();
try {
    bt = at.createInverse();
} catch (Exception e) {
    System.out.println("Non-invertible transform");
Container c = comp.getParent();
...
voiddrawCross(Graphics2D g2d, int x, int y, int size)
draw Cross
g2d.drawLine(x, y, x + size, y + size);
g2d.drawLine(x, y + size, x + size, y);
voiddrawCrosshatch(Graphics2D g, Color color, int left, int right, int top, int height, int spacing, int verticalOffset)
draw Crosshatch
g = (Graphics2D) g.create();
g.clipRect(left + 1, top + 1, right - left - 1, height - 1);
g.setColor(color);
int x = left - height + 1 - verticalOffset, bottom = top + height;
int count = 0;
while (x < right) {
    g.drawLine(x, bottom - 1, x + height, top + 1);
    x += spacing;
...
voiddrawCube(Graphics g, int x, int y, int w, int h, int d, float fac)
draw Cube
int dx = (int) ((float) d * fac);
int dy = (int) ((float) d * (1 - fac));
Color color = g.getColor();
g.fillRect(x, y, w + 1, h + 1);
g.setColor(color.darker());
Polygon p1 = makeParalRect(x + w, y + h, x + w, y, x + w + dx, y + h - dy);
Polygon p2 = makeParalRect(x, y, x + dx, y - dy, x + w, y);
g.fillPolygon(p1);
...