Road Signs, Hard
From Progzoo
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.
test text
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.
.
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 < 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);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
Diversion Northtown
Notice how the black border is curved inside and out.
test text
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.
test text
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.
.
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<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);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text