110 likes | 231 Views
Problem Solving 6 GUIs and Event Handling. ICS-201 Introduction to Computing II Semester 071. Constructing a GUI. Consider the following problem: Construct a GUI as follows: The purpose of the GUI is to calculate the body mass index (BMI) of a person. The formula to calculate the BMI is
E N D
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071
Constructing a GUI • Consider the following problem: Construct a GUI as follows: The purpose of the GUI is to calculate the body mass index (BMI) of a person. The formula to calculate the BMI is BMI = weight(in kg)/height2 (in metres)
Solution – Components in the GUI Here we find that the GUI has the following components: The layout of the components is Flow Layout Button Labels Text fields
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BMICalc extends JFrame implements ActionListener { private JTextField wtt, htt; private JButton convert; private JLabel wtl, htl, rsll; public BMICalc() { super("Body Mass Index Calculator"); setSize(500, 70); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); Solution – Code for the GUI
Solution – Code for the GUI (contd) • wtt = new JTextField(5); • htt = new JTextField(5); • wtl = new JLabel("Weight (kg)"); • htl = new JLabel("Height (m)"); • rsll = new JLabel("Your BMI is:"); • convert = new JButton("Calculate"); • cp.add(wtl); • cp.add(wtt); • cp.add(htl); • cp.add(htt); • cp.add(convert); • cp.add(rsll); • convert.addActionListener(this); • show(); • }
public void actionPerformed(ActionEvent e) { double weight = Double.parseDouble(wtt.getText()); double height = Double.parseDouble(htt.getText()); double bmi = weight/(height*height); double bmirounded = ((int) (bmi*1000))/1000.0; rsll.setText("Your BMI is: " + bmirounded); wtt.setText(""); htt.setText(""); } public static void main(String[] args) { new BMICalc(); } } Solution – Action Listener
Problem – Using Layout Managers Construct a GUI that divides a frame into appropriate areas as shown in the following GUI. Use Border and Grid Layouts.
Solution Solution in attached zip file. Note in the solution that: Border Layout Grid Layout Flow Layout
Problem – Drawing Shapes Write a program that constructs the following figure along with associated labels.
Solution public class PieChart extends Frame { public PieChart() { super("Simple Pie Chart Drawing"); setSize(300, 300); setResizable(false); show(); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setFont(new Font("Arial", Font.ITALIC, 12)); Arc2D.Double slice1 = new Arc2D.Double(50, 50, 150, 150, 0, 90, 2); g2.setColor(Color.red); g2.draw(slice1); g2.fill(slice1); g2.drawString("25% Revenue", 170, 60);
Solution – contd. Arc2D.Double slice2 = new Arc2D.Double(50, 50, 150, 150, 90, 45, 2); g2.setColor(Color.blue); g2.draw(slice2); g2.fill(slice2); g2.drawString("12.5% Sales", 40, 50); Arc2D.Double slice3 = new Arc2D.Double(50, 50, 150, 150, 135, 135, 2); g2.setColor(Color.magenta); g2.draw(slice3); g2.fill(slice3); g2.drawString("37.5% Profits", 30, 220); Arc2D.Double slice4 = new Arc2D.Double(50, 50, 150, 150, 270, 90, 2); g2.setColor(Color.green); g2.draw(slice4); g2.fill(slice4); g2.drawString("25% Expenditures", 180, 200); } public static void main(String[] args) { PieChart p = new PieChart(); } }