Assessment 1 2009 Answers

From Progzoo
Jump to: navigation, search

import java.awt.image.BufferedImage; import java.awt.*; import java.io.File; import javax.imageio.ImageIO; public class SA{

static void drawFlag(Graphics2D g){

int gw = 36;int h=140;int w=210;
int ax = (int)(gw/Math.sin(Math.toRadians(35.0)))/2;
int by = (int)(gw/Math.cos(Math.toRadians(35.0)))/2;
int cx = (int)(h/Math.tan(Math.toRadians(35.0))/2);
int dx = (int)(cx*(h/(h-gw)));
Polygon tr = new Polygon(new int[]{0,cx,w,w},new int[]{0,h/2,h/2,0},4);
Polygon br = new Polygon(new int[]{0,cx,w,w},new int[]{h,h/2,h/2,h},4);
Polygon cl = new Polygon(new int[]{0,cx,0},new int[]{0,h/2,h},3);
g.setColor(new Color(222,0,8));
g.fillPolygon(tr);
g.setColor(new Color(24,24,123));
g.fillPolygon(br);
g.setColor(new Color(0,0,0));
g.fillPolygon(cl);
g.setStroke( new BasicStroke(35,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
g.setColor(new Color(0,66,8));
g.drawLine(0,0,cx,h/2);
g.drawLine(0,h,cx,h/2);
g.drawLine(w,h/2,cx,h/2);
g.setStroke(new BasicStroke(6,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER));
g.setColor(Color.white);
Polygon lu = new Polygon(new int[]{ax,dx,w,dx},

new int[]{0,h/2-gw/2,h/2-gw/2,h/2-gw/2},4);

Polygon ld = new Polygon(new int[]{ax,dx,w,dx},

new int[]{h,h/2+gw/2,h/2+gw/2,h/2+gw/2},4);

g.drawPolygon(lu);
g.drawPolygon(ld);
Polygon ml = new Polygon(new int[]{0,cx-ax,0,cx-ax},new int[]{by,h/2,h-by,h/2},4);
g.setColor(new Color(255,239,8));
g.drawPolygon(ml);

}

public static void main(String argv []){

BufferedImage image = new BufferedImage(210,140, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); drawFlag(g); g.dispose(); try { File out = new File("flag.png"); ImageIO.write(image, "png", out); }catch(Exception e){ System.err.println(e); }

}

}