Difference between revisions of "Test"

From Progzoo
Jump to: navigation, search
Line 80: Line 80:
 
     g.translate(0,17);
 
     g.translate(0,17);
 
   }
 
   }
}
 
</answer>
 
</question>
 
 
==India==
 
<question height="204" width="306" imgOut="flag.png" className="Raster" title="India" rows='25'>
 
<p>The flag of India is 306 by 204. The orange, white and green stripes
 
are 306 by 67. The wheel has radius 30.
 
</p>
 
<p>
 
The flag of India includes a wheel with 24 spokes, the spokes are
 
2*pi/24 radians apart.</p>
 
<p>The code given draws only three of the spokes - we could copy and paste -
 
but it would be much better to use a loop.
 
</p>
 
<p>
 
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.
 
</p>
 
[[Image:flagindia.png|frame|Flag of India]]
 
<prog lang="java">
 
static void drawFlag(Graphics2D g){
 
  g.setRenderingHint(
 
    RenderingHints.KEY_ANTIALIASING,
 
    RenderingHints.VALUE_ANTIALIAS_ON);
 
  g.setColor(Color.orange);
 
  g.fillRect(0,0,306,67);
 
  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);
 
}
 
</prog>
 
<answer lang="java">
 
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.white);
 
  g.fillRect(0,67,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);
 
  for (int i=0;i&lt;24;i++){
 
    g.rotate(2*Math.PI/24);
 
    g.drawLine(0,0,0,30);
 
  }
 
}
 
</answer>
 
</question>
 
 
==Namibia==
 
<question height="200" width="300" imgOut="flag.png" className="Raster" title="Namibia" rows="25">
 
<p>
 
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).
 
</p>
 
<p>Notice that only two of the 12 triangles are drawn by the example
 
code. Also note that the colors are mixed up.
 
</p>
 
<p>
 
We can use the method g.rotate(Math.PI/6) to repeat the triangles of
 
the star-burst for Namibia.
 
</p>
 
*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
 
[[Image:flagnamibia.png|frame|Flag of Namibia]]
 
 
<prog lang="java">
 
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);
 
}
 
</prog>
 
<answer lang="java">
 
static void drawFlag(Graphics2D g){
 
  g.setRenderingHint(
 
    RenderingHints.KEY_ANTIALIASING,
 
    RenderingHints.VALUE_ANTIALIAS_ON);
 
  Polygon tri = new Polygon(
 
  new int[]{19,29,19},
 
  new int[]{ 5, 0,-5},3);
 
  g.setColor(Color.white);
 
  g.fillRect(0,0,300,200);
 
  g.setColor(Color.blue);
 
  g.fillPolygon(new Polygon(
 
    new int[]{0,240,0},
 
    new int[]{0,0,160},3));
 
  g.setColor(Color.green);
 
  g.fillPolygon(new Polygon(
 
    new int[]{60,300,300},
 
    new int[]{200,40,200},3));
 
  g.setStroke(new BasicStroke(42));
 
  g.setColor(Color.red);
 
  g.drawLine(0,200,300,0);
 
  g.translate(60,60);
 
  g.setColor(Color.yellow);
 
  for(int i=0;i&lt;12;i++){
 
    g.fillPolygon(tri);
 
    g.rotate(Math.PI/6);
 
  }
 
  g.setColor(Color.blue);
 
  g.fillOval(-20,-20,40,40);
 
  g.setColor(Color.yellow);
 
  g.fillOval(-16,-16,32,32);
 
 
}
 
}
 
</answer>
 
</answer>
 
</question>
 
</question>

Revision as of 16:21, 7 October 2008

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



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

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.



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