- This lab’s purpose is to any country’s flag of your choice but I chose Liberia’s flag.
- Liberia’s Flag
To Download This Code:
Lines 1-6: The header for this code: name, date, purpose, inputs, and outputs
Lines 8-9: Importing two libraries: Swing(graphics) and awt (user interface).
Lines 10-13: Typical statements for a Java program. Line 10 is the class name and line 12 is the main statement that Eclipse will run from. This is also the class that eclipse will run as the main driver because of the main statement.
Line 14: Creates a new JFrame called f. JFrame is like a window on your computer.
Lines 15-17: Three essential things to a JFrame: visibility, size, and exit. I set the visibility to JFrame true so that user can see it. Size is 800px width and 400px height. Defaultcloseoperation is always the same statement and is used so that the window has a close option on your computer
Lines 18-19:Created a JPanel called p and set it’s background to white. Also a panel is what we will be using to draw our flag and not the frame.
Lines 20: Just adds the panel to the JFrame.
Lines 1-2: Importing two libraries: Swing(graphics) and awt (user interface).
Line 4: This is the class name and it has extends because the JPanel in the main driver will be calling this class to draw the object.
Lines 7-8: Calls the drawing method and it has the repaint method because it will painting from the method below onto the JPanel.
Lines 11-13: Standard statements used to paint objects; and Line 13 sets the currently being used as red. g is also an object that the Java library already contains.
Line 15-20: .fill creates a rectangle and then fills it in with the current color (red). Each statement draws and fills a rectangle at certain locations (first two numbers) and the size of the rectangle is the last two numbers. These red rectangles create the red stripes for the Liberia Flag.
Lines 23-24: Sets the color to blue and then creates the blue rectangle in the top let corner of the Liberia Flag.
Lines 27-40: Points of the Star Source: here
Lines 27-30: Declaring the width, height, and x/y position of the star in my Liberia Flag.
Lines 31-40: These equations are used to find the points on the star.
Lines 42-59: Drawing the Star Source: here
Lines 42-43: Creating the Star object and setting the new color being used to white for the star.
Lines 43-53: Adding points to the star to draw the star. Adding points by designating x,y coordinate. To draw this polygon (star), the program will go to the first point and then draw to the next statement’s point and continue to do so until there are no more points.
Lines 54-59: Filling in the drawn star.
Line 60: Setting the color being used as gray.
Line 61: Drawing a rectangle and NOT filling it in around the window size (800,400) to present a flag.