The flags of many of the world’s nations consist of 3 colored bars displayed eit
ID: 3574736 • Letter: T
Question
The flags of many of the world’s nations consist of 3 colored bars displayed either vertically or horizontally; two examples are shown below: flag of France flag of Hungary Choose a style (either the French or the Hungarian) and write a Java program that can draw the flags of three nations that use that (vertical or horizontal) three-color style in a window when the user clicks a button with the country’s name; the window illustrated below (with fake country names, other than France) is an example of what you are trying to create: Your flag should change size with the window – in other words, don’t just use g.fillRect() calls – instead, set up JPanels and set their background colors
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener
{
JButton btn1,btn2,btn3;
ImageIcon image;
private JLabel label;
String picPath;
MyFrame()
{
setVisible(true);
setSize(300,300);
setTitle("First Frame");
getContentPane().setBackground(Color.cyan);
setLayout(new FlowLayout());
btn1=new JButton("France");
btn1.addActionListener(this);
add(btn1);
btn2=new JButton("Italy");
btn2.addActionListener(this);
add(btn2);
btn3=new JButton("Fenwick");
btn3.addActionListener(this);
add(btn3);
label = new JLabel(image);
add (label);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
//getContentPane().setBackground(Color.red);
image = new ImageIcon(getClass().getResource("D:\Study \JAVA\france.jpg"));
label.setIcon(image);
}
if(e.getSource()==btn2)
{
//getContentPane().setBackground(Color.green);
image = new ImageIcon(getClass().getResource("D:\Study \JAVA\Italy.jpg"));
label.setIcon((new javax.swing.ImageIcon(getClass().getResource("/image/MickeyMouse.jpg"))));
label.setIcon(image);
}
if(e.getSource()==btn3)
{
//getContentPane().setBackground(Color.black);
image = new ImageIcon(getClass().getResource("D:\Study \JAVA\Fenwick.jpg"));
label.setIcon((new javax.swing.ImageIcon(getClass().getResource("/image/MickeyMouse.jpg"))));
label.setIcon(image);
}
}
}
public class flagTest
{
public static void main(String args[])
{
new MyFrame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.