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

BufferedImagedraw(int[] pixels, int width, int height)
http://stackoverflow.com/questions/4386446/problem-using-imageio-write-jpg-file/4388542#4388542

I needed to make all this stuff with RGB masks and the color model because otherwise pixels wouldn't get drawn correctly.

DataBuffer buffer = new DataBufferInt(pixels, width * height);
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
BufferedImage image = new BufferedImage(RGB_OPAQUE, raster, false, null);
return image;
voiddrawAndFill(Graphics g, Shape s, Color draw, Color fill)
draw And Fill
Graphics2D g2 = (Graphics2D) g;
g2.setColor(fill);
g2.fill(s);
g2.setColor(draw);
g2.draw(s);
voiddrawAngledLine(Graphics g, int x, int y, double angle, int length)
draws a line starting at x,y, and going in the direction of the angle
int endX = (int) (Math.cos(angle) * length) + x;
int endY = (int) (Math.sin(angle) * length) + y;
g.drawLine(x, y, endX, endY);
voiddrawAt(final Graphics2D g, final double x, final double y, final Shape s)
draw At
g.translate(x, y);
g.draw(s);
g.translate(-x, -y);
voiddrawAutoCompleteMarker(Graphics2D g2, int x, int y, int iconSize)
draw Auto Complete Marker
int spacing = 2;
Rectangle2D marker = new Rectangle2D.Double();
marker.setFrame(x + spacing - iconSize, y + spacing - iconSize, 2 * (iconSize - spacing),
        2 * (iconSize - spacing));
g2.setPaint(Color.BLACK);
g2.fill(marker);
voiddrawBeamsplit(Graphics g, int midx, int midy, Color cup, Color cdown, Color cright, boolean showMirror, boolean isDichroic)
draw Beamsplit
int boxR = 15;
g.setColor(Color.lightGray);
g.fillRect(midx - boxR, midy - boxR, boxR * 2, boxR * 2);
float[] carr = new float[3];
if (cright != null) {
    cright.getColorComponents(carr);
    for (int i = -beamR; i < beamR; i++) {
        float weight = fmin1((beamR - Math.abs(i)) / (double) beamR);
...
voiddrawBeamVariableRadiusVertical(Graphics g, Color c, int midx, int y1, int y2, int r1, int r2)
draw Beam Variable Radius Vertical
float[] carr = c.getRGBColorComponents(null);
for (int y = y1; y < y2; y++) {
    double fact = (y - y1) / (double) (y2 - y1);
    int hereBeamR = (int) ((1 - fact) * r1 + fact * r2);
    for (int i = -hereBeamR; i < hereBeamR; i++) {
        double weight = 1 - Math.abs(i) / (double) hereBeamR;
        g.setColor(new Color(carr[0], carr[1], carr[2], (float) weight));
        g.drawLine(midx + i, y, midx + i, y);
...
voiddrawBoundary(Graphics2D g2d, Rectangle bounds, Point2D.Double[] pts)
draw Boundary
Color smokey = new Color(128, 128, 128, 128);
drawBoundary(g2d, bounds, pts, smokey, true);
voiddrawBoxOrBlockChar(Graphics g, int x, int y, int bi, char c, int charWidth, int charHeight)
draw Box Or Block Char
int x2 = (x + (charWidth / 2));
int y2 = (y + (charHeight / 2));
int xx = (x + charWidth);
int yy = (y + charHeight);
final int[] map = { 0x05, 0x0f, 0x50, 0xf0, 
        0x05, 0x0f, 0x50, 0xf0, 
        0x05, 0x0f, 0x50, 0xf0, 
        0x44, 0x4c, 0xc4, 0xcc, 
...
voiddrawBubbleHead(Graphics2D g, Point2D headPosition, double orientation, double size, Color color, Stroke stroke)
draw Bubble Head
double size2 = size / 2;
double x = headPosition.getX() - size2 * Math.cos(orientation);
double y = headPosition.getY() - size2 * Math.sin(orientation);
AffineTransform arrowTransform = new AffineTransform();
arrowTransform.translate(x, y);
Shape shape = new Ellipse2D.Double(-size2, -size2, size, size);
Shape transformedShape = arrowTransform.createTransformedShape(shape);
g.setColor(Color.WHITE);
...