Blog about (Web-) Programming, Graphics and Games.

24.08.2011

Drawing with Java

For Wloom I needed a transparent radar background image. Since I am not used to a vector-paint-programm, I 'drew' the image with java, by programming following small program. At first it creates an image, then it draws the radar shapes and saves everything as transparent png. Ingame the radar will look like this.
import java.awt.*;

import java.awt.image.*;

public final class Drawer {
public static void main(String [] args) {
try {
BufferedImage i=new BufferedImage(300,150,BufferedImage.TYPE_INT_ARGB);
Graphics2D g=i.createGraphics();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(new Color(0,0,0,100));
g.fillArc(10,5,280,280,0,180);

g.setColor(Color.black);
g.setStroke(new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
int r=140;g.drawArc(150-r,150-r-5,r*2,r*2,0,180);
g.drawLine(10,145,290,145);
g.drawLine(150,5,150,145);
g.setStroke(new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
r=95;g.drawArc(150-r,150-r-5,r*2,r*2,0,180);
r=45;g.drawArc(150-r,150-r-5,r*2,r*2,0,180);
g.drawLine(51,46,150,145);
g.drawLine(249,46,150,145);

g.setColor(new Color(100,200,0));
g.setStroke(new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
g.drawArc(10,5,280,280,0,180);
g.drawLine(10,145,290,145);
g.drawLine(150,5,150,145);
g.setStroke(new BasicStroke(2,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
r=95;g.drawArc(150-r,150-r-5,r*2,r*2,0,180);
r=45;g.drawArc(150-r,150-r-5,r*2,r*2,0,180);
g.drawLine(51,46,150,145);
g.drawLine(249,46,150,145);

g.dispose();
javax.imageio.ImageIO.write(i,"png",new java.io.File("radar.png"));
} catch (Exception e) { e.printStackTrace(); }
}
}

Keine Kommentare: