Create a Method to Draw Similar Shapes

From Progzoo
Jump to navigation Jump to search
Flag of South Korea

This page shows you step by step how to draw the four trigrams on the flag of South Korea.

You can take advantage of the similarity between these. You can create the single method drawTriGram(g,angle,t1,t2,t3) This method can be called four times do draw each of the trigrams.

drawTrigram(g, 30,true ,false,true );
drawTrigram(g,210,false,true ,false);
  • The first trigram, in the top right is at 30° from the horizontal. The rectangles are broken, solid and broken so we supply true,false,true.
  • The second trigram, in the top left is at 150° from the horizontal. The rectangles are all solid so we supply true,true,true.

Fill Four Boxes

Four Boxes

  • The method fillBoxes(g,angle) can be used to fill a box at the specified angle
static void drawBox(Graphics2D g,int angle){

Notice the following sequence:

  • Rotate by the given angle (you need to translate degrees into radians)
  • Translate out by 100
  • Fill a rectangle
  • Reverse the translation
  • Reverse the rotation

Add two more calls to drawBoxes so that four are drawn.

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

Fill Triples of Boxes

Four Triple Boxes You can change the method so that it draws three boxes instead of one.

Change the fillBoxes methods so that three are drawn each time.

  • Translate 90,0
  • Fill a rectangle
  • Translate 10,0
  • Fill a rectangle
  • Translate -110,0 to undo all three translations

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

Some boxes are broken

Four Trigrams You can add three boolean parameters. If true is sent then the box is "broken".

Change the method brokenBoxes.

  • if the parameter isBroken is true the draw two boxes
  • else draw a single large box

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