Need help with ratios!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xavo2007
    New Member
    • Feb 2007
    • 2

    Need help with ratios!!

    Hi there.

    I am required to produce the greek flag however it requires me to use ratios. Below is the excercise question which I was given.

    Make a class that extends JComponent, to draw the Greek flag, as shown in the Figure below.
    • The position and width of the white and blue sections should be approximately as shown in the Figure.

    • For this flag, the ratio of width to height is 11:8, so for example 220 pixels by 160. It should be drawn as large as possible, but keeping this ratio, whatever the size of the applet window. Any spare space (at the sides, top or bottom) can be drawn any colour you like.
    I've got the starting code:

    import javax.swing.JCo mponent;
    import java.awt.*;
    import java.awt.geom.E llipse2D;

    class GreekFlag extends JComponent
    {
    public void paintComponent( Graphics g)
    {
    Graphics2D g2 = (Graphics2D)g;
    int w = getWidth();
    int h = getHeight();
    double ratio = (double)w/(double)h;
    int flagHeight, flagWidth;
    if(ratio>11.0/8.0)
    {
    }
    else
    {
    }
    }
    }
    import javax.swing.JCo mponent;
    import javax.swing.JFr ame;

    public class MyFrameViewer
    {
    public static void main(String[] args)
    {
    JFrame frame = new JFrame();

    final int FRAME_WIDTH = 300;
    final int FRAME_HEIGHT = 400;

    frame.setSize(F RAME_WIDTH, FRAME_HEIGHT);
    frame.setTitle( "Template Viewer");
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

    GreekFlag component = new GreekFlag();
    frame.add(compo nent);

    frame.setVisibl e(true);
    }
    }
    and along with that I've managed to work out the actual design & colour of the flag. The code is shown below:

    static void drawFlag(Graphi cs2D g)
    {
    g.setStroke(new BasicStroke(40) );

    g.setColor(Colo r.blue);
    g.drawLine(0,20 +0*80,600,20+0* 80);
    g.drawLine(0,20 +1*80,600,20+1* 80);
    g.drawLine(0,20 +2*80,600,20+2* 80);

    g.setColor(Colo r.blue);
    g.fillRect(0,0, 200,200);
    g.setColor(Colo r.white);
    g.fillRect(0,80 ,200,40);
    g.fillRect(80,0 ,40,200);
    }
    Thats about it and thanks for reading my post. Can wait for the replies and helping me out inmaking this greek flag.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by xavo2007
    Hi there.

    I am required to produce the greek flag however it requires me to use ratios. Below is the excercise question which I was given.



    I've got the starting code:





    and along with that I've managed to work out the actual design & colour of the flag. The code is shown below:



    Thats about it and thanks for reading my post. Can wait for the replies and helping me out inmaking this greek flag.
    Well test your code see if it works. You have not specified exactly where you need help.

    Comment

    • xavo2007
      New Member
      • Feb 2007
      • 2

      #3
      Hi there.

      Thanks for the quick reply. Well i need help making the flag ratio the same

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by xavo2007
        Hi there.

        Thanks for the quick reply. Well i need help making the flag ratio the same
        If you have a width of w, then height is 8/11 * w

        That's all you need to do. Define a width of any size and set the hieght to be 8/11 of that width.

        Comment

        Working...