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

voidpaintOval(int x, int y, Color c, int size, Graphics g)
paint Oval
g.setColor(c);
Point pos = getMapPos(x, y);
g.fillOval(pos.x - (size / 2), pos.y - (size / 2), size, size);
voidpaintPoint(java.awt.Graphics g2, int x, int y)
paint Point
g2.fillRect(x, y, 1, 1);
voidpaintRectCompartment(Graphics g, Dimension size, int x, int y, Color fillColor, Color strokeColor)

Paints the rectangle compartment at given coordinate with given fill color and stroke color.

g.setColor(fillColor);
g.fillRect(x, y, size.width, size.height);
g.setColor(strokeColor);
g.drawRect(x, y, size.width, size.height);
voidpaintRectShadow(Graphics g, int x, int y, int width, int height)
Draw a shadow shaped as a Rectangle with smooth edges with a depth of 5 if possible.
paintRectShadow(g, x, y, width, height, 5);
voidpaintShape(Shape shape, Graphics2D g2d, Color colorStroke, Stroke stroke, Color colorFill, double downsample)
paint Shape
if (colorFill != null) {
    g2d.setColor(colorFill);
    g2d.fill(shape);
if (colorStroke != null) {
    if (stroke != null)
        g2d.setStroke(stroke);
    g2d.setColor(colorStroke);
...
voidpaintSprite(Graphics g, int x, int y, int[][] sprite, Color[] colors)
Paint the given sprite.
for (int i = 0; i < sprite.length; i++) {
    for (int j = 0; j < sprite[i].length; j++) {
        if (sprite[i][j] == 0)
            continue;
        g.setColor(colors[sprite[i][j] - 1]);
        g.drawLine(x + i, y + j, x + i, y + j);
Graphics2DprepareGraphics(Graphics g)
prepare Graphics
Graphics2D g2 = (Graphics2D) g;
Map rhints = (Map) (Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); 
if (rhints == null && Boolean.getBoolean("swing.aatext")) { 
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
} else if (rhints != null) {
    g2.addRenderingHints(rhints);
return g2;
...
voidprintAll(java.awt.Graphics2D g2, java.awt.Component component)
print All
if (component != null) {
    int x = component.getX();
    int y = component.getY();
    g2.translate(x, y);
    try {
        java.awt.Shape prevClip = g2.getClip();
        try {
            java.awt.Component c = getViewportViewIfNecessary(component);
...
voidraiseOval(Graphics2D g2, Rectangle r, Color foreColor)
draw an image in a rectangle
Color background = g2.getBackground();
Color oldColor = g2.getColor();
Ellipse2D e = new Ellipse2D.Double(r.getX(), r.getY(), r.getWidth(), r.getHeight());
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Paint oldPaint = g2.getPaint();
GradientPaint gp = new GradientPaint(r.x, r.y, foreColor, r.width, r.height, background, true);
g2.setPaint(gp);
g2.fill(e);
...
voidraiseRect(Graphics2D g2, Rectangle r, Color foreColor)
draw an image in a rectangle
Color oldColor = g2.getColor();
g2.setColor(foreColor);
g2.fill3DRect(r.x, r.y, r.width, r.height, true);
g2.setColor(oldColor);