Exercises:
1. Write a GUI application, YI_MyFlag.java and its JFrame class to draw your favorite flag.
- Write a GUI application, YI_Car.java and its JFrame class to draw a car like the one in the picture.
-
Write a GUI application, YI_BullEye.java and its JFrame class to draw the picture.
-
Write a GUI application, YI_OlympicRings.java and its JFrame class to draw famous rings.
How did Zach Yazdani combine the two classes?
You need to have both methods: paintComponent(Graphics g) and main in one file
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import javax.swing.JComponent; import javax.swing.JFrame; public class RectangleObject extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Construct a rectangle object Rectangle box = new Rectangle(5, 10, 20, 30); // draw the rectangle g2.draw(box); g2.draw(new Ellipse2D.Double(200,200,40,100)); g2.draw(new Line2D.Double(50,30,200,100)); g2.drawPolygon(new int [] {10,20,30}, new int [] {100,20,100}, 3); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300,400); frame.setTitle("A Rectanlge Object in a JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleObject aRectangle = new RectangleObject(); frame.add(aRectangle); frame.setVisible(true); } } //See attached Programs