We can repeat a statement or block of statements with a
for loop. The simplest for loops take the following form:
The control variable i takes every value 0,1,2..9 in turn. We can use the value of i inside the loop if required.
Bahrain
The flag of Bahrain has a five irregular pentagons on a red background.
Each pentagon can be represented by five points such as (0,0), (64,0)
(100,15) (64,31) and (0,31).
Flag of Bahrain
test text
Qatar
Consider the neighbouring state Qatar.
9 pillars, each pillar is 17 pixels below the previous.
The five coordinates of the top pillar are (0,0) (84,0) (125,9) (84,17), (0,17)
Use RGB(128,0,0) for the less jaunty red.
Flag of Qatar
The need for a loop becomes even more obvious for this flag.
test text
India
The flag of India is 306 by 204. The orange, white and green stripes
are 306 by 68. The wheel has radius 30.
The flag of India includes a wheel with 24 spokes, the spokes are
2*pi/24 radians apart.
The code given draws only three of the spokes - we could copy and paste -
but it would be much better to use a loop.
We have not really done India justice here - the actual flag is much more
elegant, the spokes taper and the rim includes decoration.
Nevertheless - we apologize to the sub-continent and move on.
Flag of India
.
static void drawFlag(Graphics2D g)
{
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.orange);
g.fillRect(0, 0,306,68);
g.setColor(Color.green);
g.fillRect(0,134,306,68);
g.setColor(Color.blue);
g.setStroke(new BasicStroke(2,0,0));
g.drawOval(123,72,60,60);
g.translate(153,102);
g.rotate(2*Math.PI/24);
g.drawLine(0,0,0,30);
g.rotate(2*Math.PI/24);
g.drawLine(0,0,0,30);
g.rotate(2*Math.PI/24);
g.drawLine(0,0,0,30);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
Namibia
Each the rightmost triangle has coordinates (19,5) (29,0) (19,-5)
relative to the centre of the star burst - which is at (60,60).
Notice that only two of the 12 triangles are drawn by the example
code. Also note that the colors are mixed up.
We can use the method g.rotate(Math.PI/6) to repeat the triangles of
the star-burst for Namibia.
The yellow circle has radius 16
The triangles are partially covered by a blue circle of radius 20
The whole flag is 300 by 200
You must set the white part of the flag white
Flag of Namibia
.
static void drawFlag(Graphics2D g)
{
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.green);
g.fillPolygon(new Polygon(
new int[]{0,240,0},
new int[]{0,0,160},3));
g.setColor(Color.red);
g.fillPolygon(new Polygon(
new int[]{60,300,300},
new int[]{200,40,200},3));
g.setStroke(new BasicStroke(42));
g.setColor(Color.blue);
g.drawLine(0,200,300,0);
g.translate(60,60);
g.setColor(Color.black);
Polygon tri = new Polygon(
new int[]{19,29,19},
new int[]{ 5, 0,-5},3);
g.fillPolygon(tri);
g.rotate(Math.PI/6);
g.fillPolygon(tri);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
European Union
Flag of European Union
We need the trigonometry functions cos and sin to draw flags
in a circle.
There are 12 stars
The flag is 250 by 180
The stars centers are 64 from the center
The coordinates at 30o from the x-axis (East) are 64*cos(30), 64*sin(30).
Each star has radius 12.5
Look at the original program
g.setColor(Color.yellow);
for (int i=0;i<9;i++){
double a = Math.PI*2*i/9;
g.translate( 100*Math.cos(a), 100*Math.sin(a));
g.fillPolygon(star);
g.translate(-100*Math.cos(a),-100*Math.sin(a));
}
The circle of stars is currently centered at the origin.
You can move this by adding a translate call after the setColor line
g.translate(100,100);
The number of stars filled is 9; you want 12
Change this number in the for line
Change this number in the calculation of the angle a
The radius of the circle of stars is 100; you want it to be 64
The number 100 shows up in the translate methods four times
The sin and cos functions in Java are called Math.sin
and Math.cos
. They
expect radians as input. 360o is
2*Math.PI
in radians.
.
static void drawFlag(Graphics2D g)
{
Polygon star = new Polygon(
new int[]{0 ,3 ,12,5 ,8,0,-8,-5,-12,-3},
new int[]{-13,-4,-4,2 ,11,5,11,2,-4,-4},10);
g.setColor(Color.blue);
g.fillRect(0,0,500,350);
g.setColor(Color.yellow);
for (int i=0;i<9;i++)
{
double a = Math.PI*2*i/9;
g.translate( 100*Math.cos(a), 100*Math.sin(a));
g.fillPolygon(star);
g.translate(-100*Math.cos(a),-100*Math.sin(a));
}
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text