I am working on a Java program to draw the Botswana flag. Here are my instructio
ID: 3552627 • Letter: I
Question
I am working on a Java program to draw the Botswana flag. Here are my instructions:
Write a Java program to produce the flag of Botswana. Make sure the shape is rectangular.The blue
hue is azure blue. Center the flag on the screen.
Here is what I have written:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Flag extends JComponent
{
public void flagComponent(Graphics g)
{
Color azure = new Color(0,127,255);
g.setColor(azure);
g.fillRect(100,100,90,22);
g.setColor(Color.WHITE);
g.fillRect(100,122,90,3);
g.setColor(Color.BLACK);
g.fillRect(100,125,90,10);
g.setColor(Color.WHITE);
g.fillRect(100,135,90,3);
g.setColor(azure);
g.fillRect(100,138,90,22);
}
}
class flagViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(290,260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent component = new Flag();
frame.add(component);
frame.setVisible(true);
}
}
It doesn't work. Where did I go wrong? Help!
Explanation / Answer
2 things:
your render method must be called internally by java. Java calls the paint(Graphics g) method whenver the component needs to be rendered. Renaming flagComponent to paint should suffice
Also, I believe main has to be inside the public class, so putting your main method inside your flag class would suffice. You do not need a separate class for flag viewer anyway
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.