Write a java program so the user can choose the dimensions of the flag and which
ID: 3593104 • Letter: W
Question
Write a java program so the user can choose the dimensions of the flag and which country's flag to be displayed.
Also, create a class country, which has the following properties: name, color1, color2, color3, and if the stripes are horizontal or vertical.You will need to create 6 instances of the country class.
The user should be able to select between 3 flags with vertical stripes (Italy, France, Romania) and 3 flags with horizontal stripes (Germany, Holland, Hungary), using the menubar.
The following is the code i currently have:
FlagComponent Class
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
@SuppressWarnings("serial")
public class FlagComponent extends JComponent
{
private int xLeft, yTop, width;
private Color c1;
private Color c2;
private Color c3;
public FlagComponent(int width, Color c1, Color c2, Color c3){
xLeft = 100;
yTop = 100;
this.width = width;
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
public void paintComponent(Graphics g)
{
xLeft = 100;
yTop = 100;
width = 90;
g.setColor(c1);
g.fillRect(xLeft, yTop, width / 3, width * 2 / 3);
g.setColor(c2);
g.fillRect(xLeft + 2 * width / 3, yTop, width / 3, width * 2 / 3);
//g.setColor(Color.WHITE);
//g.fillRect(xLeft + width/3, yTop, width /3, width * 2 / 3);
g.setColor(c3);
g.fillRect(xLeft + width / 3, yTop, xLeft + width / 3, width * 2 / 3);
//g.fillRect(xLeft + width / 3, yTop + width * 2 / 3, xLeft + width * 2 / 3);
//g.drawLine(130, 100, 160, 100);
//g.drawLine(130, 160, 160, 160);
}
}
FlagProgram Class
import java.awt.Color;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
@SuppressWarnings("serial")
public class FlagProgram extends JFrame
{
private JMenu dimensionMenu = new JMenu("Dimenions");
private JMenu countryMenu = new JMenu("Country");
private JMenuItem width100Item = new JMenuItem("100");
private JMenuItem width200Item = new JMenuItem("200");
private JMenuItem width500Item = new JMenuItem("500");
private JMenuItem italyItem = new JMenuItem("Italy");
private JMenuItem franceItem = new JMenuItem("France");
private JMenuItem romaniaItem = new JMenuItem("Romania");
@SuppressWarnings("unused")
public static void main(String[] args)
{
FlagProgram flagProgram= new FlagProgram();
}
public FlagProgram()
{
this.setSize(600, 800);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(dimensionMenu);
menuBar.add(countryMenu);
dimensionMenu.add(width100Item);
dimensionMenu.add(width200Item);
dimensionMenu.add(width500Item);
countryMenu.add(italyItem);
countryMenu.add(franceItem);
countryMenu.add(romaniaItem);
ActionListener listener = new FlagListener();
this.setVisible(true);
this.add(new FlagComponent(100, Color.BLUE, Color.WHITE, Color.RED));
}
public class FlagListener implements ActionListener()
{
public void actionPerformed(ActionEvent Event){
}
}
}
Explanation / Answer
Given below is the code for the question with output. Please don't forget to rate the answer if it helped. Thank you.
To indent code in eclipse, select code by pressing Ctrl+A and then Ctrl+i
Country.java
import java.awt.Color;
public class Country {
private String name;
private Color colors[];
private boolean isVertical;
public Country(String countryName, Color[] colors, boolean isVertical)
{
this.name = countryName;
this.colors = new Color[3];
for(int i = 0; i < 3; i++)
this.colors[i] = colors[i];
this.isVertical = isVertical;
}
public String getName()
{
return name;
}
public Color getColor(int index)
{
return colors[index];
}
public boolean isVertical()
{
return isVertical;
}
}
FlagPanel.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class FlagPanel extends JPanel{
private Country country;
private int width, height;
public FlagPanel(Country country, int width)
{
this.country = country;
setFlagWidth(width);
}
public void setFlagWidth(int width)
{
this.width = width;
this.height = 2 * width / 3;
setSize(width, height);
}
public void setCountry(Country country)
{
this.country = country;
}
@Override
public void paint(Graphics g) {
if(country.isVertical())
drawVerticalBars(g);
else
drawHorizontalBars(g);
}
private void drawVerticalBars(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int x, y, w, h;
x = 0;
y = 0;
w = width / 3;
h = height;
for(int i = 0; i < 3; i++)
{
g2.setColor(country.getColor(i));
g.fillRect(x, y, w, h);
x += w;
}
}
private void drawHorizontalBars(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int x, y, w, h;
x = 0;
y = 0;
w = width;
h = height / 3;
for(int i = 0; i < 3; i++)
{
g2.setColor(country.getColor(i));
g.fillRect(x, y, w, h);
y += h;
}
}
}
FlagProgram.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
@SuppressWarnings("serial")
public class FlagProgram extends JFrame
{
private JMenu dimensionMenu = new JMenu("Dimensions");
private JMenu countryMenu = new JMenu("Country");
private Country[] countries;
private FlagPanel flagPanel;
private JLabel lblName;
private void createCountries()
{
countries = new Country[6];
countries[0] = new Country("Italy", new Color[]{Color.green, Color.white, Color.red}, true);
countries[1] = new Country("France", new Color[]{Color.blue, Color.white, Color.red}, true);
countries[2] = new Country("Romania", new Color[]{Color.blue, Color.yellow, Color.red}, true);
countries[3] = new Country("Germany", new Color[]{Color.black, Color.red, Color.yellow}, false);
countries[4] = new Country("Holland", new Color[]{Color.red, Color.white, Color.blue}, false);
countries[5] = new Country("Hungary", new Color[]{Color.red, Color.white, Color.green}, false);
}
public FlagProgram()
{
this.setSize(600, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(dimensionMenu);
menuBar.add(countryMenu);
ActionListener sizeListener = new SizeListener();
int[] widths = {100, 200, 500};
for(int i = 0; i < widths.length; i++)
{
JMenuItem mi = new JMenuItem(widths[i]+"");
mi.setActionCommand(mi.getText());
mi.addActionListener(sizeListener);
dimensionMenu.add(mi);
}
createCountries();
ActionListener listener = new FlagListener();
for(int i = 0; i < countries.length;i++)
{
JMenuItem mi = new JMenuItem(countries[i].getName());
mi.addActionListener(listener);
mi.setActionCommand(countries[i].getName()); //set the action command which will be used to when click event
countryMenu.add(mi);
}
flagPanel = new FlagPanel(countries[0], 500);
lblName = new JLabel(countries[0].getName());
Container c = getContentPane();
c.setLayout(new BorderLayout(40, 40));
c.add(flagPanel, BorderLayout.CENTER);
c.add(lblName, BorderLayout.SOUTH);
this.setVisible(true);
}
private int indexOf(String countryName)
{
for(int i = 0; i < countries.length; i++)
{
if(countries[i].getName().equals(countryName))
return i;
}
return -1;
}
class FlagListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
int index = indexOf(e.getActionCommand());//get the country choose by menu item
flagPanel.setCountry(countries[index]);
flagPanel.repaint();
lblName.setText(countries[index].getName()); //show the country name
}
}
class SizeListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
int width = Integer.parseInt(e.getActionCommand());
flagPanel.setFlagWidth(width);
flagPanel.repaint();
}
}
public static void main(String[] args)
{
FlagProgram flagProgram= new FlagProgram();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.