Difference between revisions of "Road Signs, Hard"

From Progzoo
Jump to: navigation, search
(Turpin's Crossing)
 
Line 9: Line 9:
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<prog lang="java">static void drawFlag(Graphics2D g){
 
<prog lang="java">static void drawFlag(Graphics2D g){
  g.setRenderingHint(
 
    RenderingHints.KEY_ANTIALIASING,
 
    RenderingHints.VALUE_ANTIALIAS_ON);
 
  g.translate(50,200);
 
//snow poly
 
    Polygon snow= new Polygon(
 
      new int[]{0,0,4,4,7,12,12,4,4},
 
      new int[]{0,35,32,28,30,29,24,19,0},9);
 
//snow
 
  g.translate(100,-60);
 
  g.setColor(Color.black);
 
  for (int i=0;i&lt;6;i++){
 
    for (int j=0;j&lt;2;j++){
 
      g.fillPolygon(snow);
 
      g.scale(-1,1);
 
    }
 
    g.rotate(Math.PI/3);
 
  }
 
  g.translate(-80,44);
 
 
//tri
 
  g.setColor(new Color(225,0,21));
 
  g.setStroke(new BasicStroke(15,
 
        BasicStroke.CAP_ROUND,
 
        BasicStroke.JOIN_ROUND));
 
  Polygon tri= new Polygon(
 
  new int[]{0,160,80},
 
  new int[]{0,0,-(int)(160*Math.sqrt(3)/2)},3);
 
g.drawPolygon(tri);
 
 
}</prog>
 
}</prog>
 
</question>
 
</question>
Line 51: Line 22:
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<prog lang="java">static void drawFlag(Graphics2D g){
 
<prog lang="java">static void drawFlag(Graphics2D g){
  g.setRenderingHint(
 
    RenderingHints.KEY_ANTIALIASING,
 
    RenderingHints.VALUE_ANTIALIAS_ON);
 
 
  g.setColor(Color.white);
 
  g.fillRect(0,0,160,250);
 
 
  g.translate(80,80);
 
  g.setColor(Color.black);
 
  g.fillRoundRect(-70, -70, 140, 225, 15, 15);
 
  g.setColor(Color.white);
 
  g.fillRoundRect(-66, -66, 132, 132, 7, 7);
 
  g.fillRoundRect(-66, 70, 132, 81, 7, 7);
 
 
  g.setColor(new Color(163,165,170));
 
  int radius = 50;
 
  g.setStroke(new BasicStroke(11));
 
  g.drawOval(-radius,-radius,2*radius,2*radius);
 
  int x = (int)(radius/Math.sqrt(2));
 
  g.drawLine(-x,-x,x,x);
 
 
  g.setColor(Color.black);
 
  g.setStroke(new BasicStroke(2.25f));
 
  g.clipRect(-66, -66, 132, 132);
 
  g.rotate(Math.toRadians(-45));
 
  for (int i = 0; i &lt; 5; i++)
 
    g.drawLine(-187, 5*i-10, 187, 5*i-10);
 
  g.rotate(Math.toRadians(45));
 
  g.setClip(null);
 
 
  try{
 
    g.setFont(Font.createFont(Font.TRUETYPE_FONT,
 
      new java.io.FileInputStream("../../transporth.ttf")));
 
  }catch (Exception e){}
 
 
  drawCenteredString("Zone",0,105,28f,g);
 
  drawCenteredString("ENDS",0,142,33f,g);
 
}
 
 
static void drawCenteredString(String s, int x, int y,
 
      float fontSize, Graphics2D g) {
 
  g.setFont(g.getFont().deriveFont(fontSize));
 
  int width = g.getFontMetrics(g.getFont())
 
              .stringWidth(s);
 
  g.drawString(s, x-width/2, y);
 
 
}</prog>
 
}</prog>
 
</question>
 
</question>
Line 120: Line 46:
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<prog lang="java">static void drawFlag(Graphics2D g){
 
<prog lang="java">static void drawFlag(Graphics2D g){
  g.setRenderingHint(
 
    RenderingHints.KEY_ANTIALIASING,
 
    RenderingHints.VALUE_ANTIALIAS_ON);
 
  int n=70;
 
  double sf = 50.0;
 
  double w = 3.0;
 
  double sp = 70.0;
 
  double yf = 2.5;
 
  int [] xs = new int[n];
 
  int [] ys = new int[n];
 
  for (int i=0;i&lt;n;i++){
 
    xs[i] = (int)(sf*i*sp/n);
 
    ys[i] = (int)(sf*yf*Math.cos(2*Math.PI*w*i/(n-1)));
 
  }
 
  g.setStroke(new BasicStroke((int)(sf*3.2)));
 
  g.setColor(Color.red);
 
  g.translate(10,40);
 
  g.scale(1/sf,-1/sf);
 
  g.drawPolyline(xs,ys,n);
 
  g.translate(0,sf*8);
 
  g.drawPolyline(xs,ys,n);
 
}
 
 
</prog>
 
</prog>
 
</question>
 
</question>
Line 156: Line 60:
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<shell lang="java" className="Flag" import="shells.xml#raster"/>
 
<prog lang="java">
 
<prog lang="java">
static void drawFlag(Graphics2D g){
 
  g.setRenderingHint(
 
    RenderingHints.KEY_ANTIALIASING,
 
    RenderingHints.VALUE_ANTIALIAS_ON);
 
Font f;
 
  try{
 
    f = Font.createFont(Font.TRUETYPE_FONT,
 
      new java.io.FileInputStream("../../transporth.ttf"));
 
    f = f.deriveFont(25.0f);
 
    g.setFont(f);
 
  }catch (Exception e){}
 
 
  //BackGround
 
  g.setColor(Color.white);
 
  g.fillRect(0,0,600,600);
 
  g.translate(20,20);
 
  g.setColor(new Color(0,154,64));
 
  g.fillRoundRect(0,0,300,365,20,20);
 
 
  //Boxes
 
  g.setStroke(new BasicStroke(5));
 
  g.setColor(Color.white);
 
  g.drawRoundRect(0,0,300,365,20,20);
 
  g.drawRoundRect(0,0,300,70,20,20);
 
  g.drawRoundRect(0,70,300,90,20,20);
 
  g.drawRoundRect(0,160,300,55,20,20);
 
  g.drawRoundRect(0,215,300,150,20,20);
 
  g.setStroke(new BasicStroke(1));
 
  g.setColor(new Color(0,154,64));
 
  g.drawRoundRect(-2,-2,305,370,20,20);
 
 
  //A 92
 
  g.setStroke(new BasicStroke(2));
 
  g.setColor(new Color(0,111,187));
 
  g.fillRoundRect(192,117,90,34,20,20);
 
  g.setColor(Color.white);
 
  g.drawRoundRect(192,117,90,34,10,10);
 
 
 
  //Thornton
 
  g.fillRoundRect(90,168,195,40,10,10);
 
  g.setColor(Color.black);
 
 
  //Dysart 
 
  g.setColor(Color.white);
 
  g.fillRoundRect(15,225,195,40,10,10);
 
 
 
  //railway
 
  g.setColor(new Color(125,72,0));
 
  g.fillRoundRect(15,275,170,80,10,10);
 
  g.setColor(Color.white);
 
  g.setStroke(new BasicStroke(2));
 
  g.drawRoundRect(15,275,170,80,10,10);
 
 
  //Strings
 
  g.setColor(Color.black);
 
  g.drawString("Glenrothes A 910",95,202);
 
  g.drawString("Dundee A 92",25,255);
 
  g.setColor(Color.yellow);
 
  g.drawString("A 92",230,106);
 
  g.setColor(Color.white);
 
  g.drawString("CHAPEL",80,35);
 
  g.drawString("INTERCHANGE",45,60);
 
  g.drawString("Kirkcaldy",65,105);
 
  g.drawString("Cluny",65,150);
 
  g.drawString("(A 92)",195,148); 
 
  g.drawString("Steam",90,310);
 
  g.drawString("railway",85,340);
 
 
 
  //arrows
 
  Polygon arrow = new Polygon(
 
    new int[]{0,15,15, 5, 5,-5,-5,-15,-15},
 
    new int[]{0,15,30,20,63,63,20, 30, 15},9);
 
  g.translate(30,85);
 
  g.fillPolygon(arrow);
 
  g.translate(-30,-85);
 
 
  g.translate(15,188);
 
  g.rotate(-2*Math.PI/4);
 
  g.fillPolygon(arrow);
 
  g.rotate(2*Math.PI/4);
 
  g.translate(-15,-188);
 
 
  g.translate(280,290);
 
  g.rotate(2*Math.PI/4);
 
  g.fillPolygon(arrow);
 
  g.rotate(-2*Math.PI/4);
 
  g.translate(-280,-290);
 
 
  //train
 
  Polygon train= new Polygon(
 
    new int[]{0,10,10,32,32,40,40,45,45,50,50,5,5,-5,-5,0,0,5,5,0},
 
    new int[]{0,0,15,15,2,2,15,15,30,30,35,35,40,40,15,15,30,30,5,5},20);
 
  g.translate(30,295);
 
  g.fillPolygon(train);
 
  g.translate(-30,-295);
 
  g.translate(38,328);
 
  for (int i=0;i&lt;3;i++){
 
    g.fillOval(0,0,10,10);
 
    g.translate(13,0);
 
  }
 
  g.translate(-79,-328);
 
  g.translate(50,305);
 
  g.fillOval(0,0,10,10);
 
  g.translate(-50,-305);
 
  g.translate(29,334);
 
  g.fillOval(0,0,6,6);   
 
  g.translate(-29,-334);
 
  g.translate(75,310);
 
  g.fillOval(0,0,6,15);
 
  g.translate(-75,-334);
 
  g.translate(62,320);
 
  g.fillRect(0,0,12,1);
 
}
 
 
</prog>
 
</prog>
 
</question>
 
</question>

Latest revision as of 09:13, 30 September 2011

Each of these road signs is worth 20 points.

Ice

  • Based on work by ASHLEY HAZLE

Ashley's original version includes a 16 point polygon painted 6 times. This variation paints half that polygon 12 times.

Roadice.png


[Font] [Default] [Show] [Resize] [History] [Profile]

Zone ENDS

  • Solution based on Kevin Smith's solution.

You can calculate the width of the text using the stringWidth method of the FontMetrics class. You can use this figure to calculate where to place text that should be in the centre.

Roadzoneends.png


[Font] [Default] [Show] [Resize] [History] [Profile]

Diversion Northtown

Notice how the black border is curved inside and out.

Roaddiversionnorthtown.png



[Font] [Default] [Show] [Resize] [History] [Profile]

Swing Bridge

Making a wavy line is hard work. The cosine function can be used to set up the coordinate array. The drawPolyline function only works with integers which will result in a "lumpy" wave usless you "scale up" as we do here with sf.

Roadswingbridge.png


[Font] [Default] [Show] [Resize] [History] [Profile]

Turpin's Crossing

This model answer was provided by Stuart Blows

You are not asked to do this because it is easy.

You are asked to do it because it is hard.

Roadturpin.png


[Font] [Default] [Show] [Resize] [History] [Profile]