Road Signs, Medium
From Progzoo
Each of these road signs is worth 14 points.
Countdown 3
Notice that the rectangles are drawn from the centre and that the same sequence is used countDown times.
You can use a combination of translate and rotate to put the rectangles
in place.
Of course you can change the value of countDown to 1 or 2 or 3.
The blue rectangle is 130 by 230
The white rectangles are 116 by 20
The white rectangles are at an angle of 0.502 radians.
The white rectangles are 50 apart vertically.
test text
Lane Closure
Notice that we re-use the code for the arrow and the T by creating a method.
Try to avoid repeating code - you can use variables or methods to
repeat a shape.
.
static void drawFlag(Graphics2D g){
arrow(g,20,20);
arrow(g,60,20);
T(g,100,20);
T(g,140,20);
}
static void arrow(Graphics2D g,int x, int y){
g.setColor(Color.black);
g.translate(x,y);
g.fillPolygon(new int[]{0,15,15, 0,-15,-15},
new int[]{0,15,30,15, 30, 15},6);
g.fillRect(-5,5,10,80);
g.translate(-x,-y);
}
static void T(Graphics2D g,int x,int y){
g.translate(x,y);
g.setColor(Color.red);
g.fillRect(-15,40,30,15);
g.setColor(Color.black);
g.fillRect(-5,57,10,30);
g.translate(-x,-y);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
No Traffic Lights
Notice that we pass as parameters the things that are different about the three disks.
The disks are identical apart from the color and the position and so we pass color and x,y as parameters.
No traffic lights.
.
static void drawFlag(Graphics2D g){
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int h = 240;
int w = 130;
g.translate(20,20);
g.setColor(Color.black);
g.fillRect(0,0,w,h);
disk(g,w/2,50,new Color(240,23,51));
disk(g,w/2,h/2,Color.yellow);
disk(g,w/2,h-50,Color.green);
band(g,8,h-8,w-8,8);
g.setColor(Color.white);
g.setStroke(new BasicStroke(16));
g.drawRoundRect(0,0,w,h,40,40);
}
static void disk(Graphics2D g, int x, int y,Color c){
g.translate(x,y);
int r = 30; int b=3;
g.setColor(Color.white);
g.fillOval(-r-b,-r-b,r+r+b+b,r+r+b+b);
g.setColor(c);
g.fillOval(-r,-r,r+r,r+r);
g.translate(-x,-y);
}
static void band(Graphics2D g, int x1, int y1, int x2, int y2){
g.setColor(Color.white);
g.setStroke(new BasicStroke(33,BasicStroke.CAP_BUTT,0));
g.drawLine(x1,y1,x2,y2);
g.setStroke(new BasicStroke(29,BasicStroke.CAP_BUTT,0));
g.setColor(Color.red);
g.drawLine(x1,y1,x2,y2);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
Level Crossing
.
static void drawFlag(Graphics2D g){
//Red triangle
g.setColor(new Color(204,0,51));
g.setStroke(new BasicStroke(16,1,1));
g.drawPolygon(new int[]{16,189,103},
new int[]{173,173,24},3);
//Horizontal bars
g.setColor(Color.black);
g.fillRect(58,123,90,8);
g.fillRect(58,149,90,8);
//Picket width and total height
int w = 5; int r=50;
Polygon picket = new Polygon();
picket.addPoint(0,0);
picket.addPoint(w,w);
picket.addPoint(w,r);
picket.addPoint(-w,r);
picket.addPoint(-w,w);
g.translate(67,112);
for (int i=0;i<5;i++){
g.fillPolygon(picket);
g.translate(18,0);
}
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
Level Crossing without Barrier
Define a hexagon and fill it twice - at different angles. Then
fill two white rectangles on top.
.
static void drawFlag(Graphics2D g){
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.translate(140,60);
int a=120;int w=20;
g.setColor(Color.red);
doTwice(g,new Polygon(
new int[]{-w-a,-a,a,a+w,a,-a},
new int[]{0,w,w,0,-w,-w},6));
g.setColor(Color.white);
int r=12;
doTwice(g,new Polygon(
new int[]{r-a,a-r,a-r,r-a},
new int[]{w-r,w-r,r-w,r-w},4));
}
static void doTwice(Graphics2D g,Polygon p){
g.rotate(0.3);
g.fillPolygon(p);
g.rotate(-0.6);
g.fillPolygon(p);
g.rotate(0.3);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text