
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
public class ItalianFlag
{
private int xLeft;
private int yTop;
private int width;
public ItalianFlag(int x, int y, int aWidth)
{
xLeft = x;
yTop = y;
width = aWidth;
}
public void draw(Graphics2D g2)
{
Rectangle leftRectangle = new Rectangle(
xLeft, yTop,
width / 3, width * 2 / 3);
Rectangle rightRectangle = new Rectangle(
xLeft + 2 * width / 3, yTop,
width / 3, width * 2 / 3);
Line2D.Double topLine = new Line2D.Double(
xLeft + width / 3, yTop,
xLeft + width * 2 / 3, yTop);
Line2D.Double bottomLine = new Line2D.Double(
xLeft + width / 3, yTop + width * 2 / 3,
xLeft + width * 2 / 3, yTop + width * 2 / 3);
g2.setColor(Color.GREEN);
g2.fill(leftRectangle);
g2.setColor(Color.RED);
g2.fill(rightRectangle);
g2.setColor(Color.BLACK);
g2.draw(topLine);
g2.draw(bottomLine);
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class ItalianFlagComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
ItalianFlag flag = new ItalianFlag(100, 100, 90);
flag.draw(g2);
}
}
import javax.swing.JFrame;
public class ItalianFlagViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ItalianFlagComponent component = new ItalianFlagComponent();
frame.add(component);
frame.setVisible(true);
}
}