# Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.

## Presentation on theme: "Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071."— Presentation transcript:

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)/height 2 (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 LabelsText fields Button

Solution – Code for the GUI 1. import java.awt.*; 2. import java.awt.event.*; 3. import javax.swing.*; 4. public class BMICalc extends JFrame implements ActionListener 5. { 6. private JTextField wtt, htt; 7. private JButton convert; 8. private JLabel wtl, htl, rsll; 9. public BMICalc() 10. { 11. super("Body Mass Index Calculator"); 12. setSize(500, 70); 13. Container cp = getContentPane(); 14. cp.setLayout(new FlowLayout()); 15.

Solution – Code for the GUI (contd) 16. wtt = new JTextField(5); 17. htt = new JTextField(5); 18. wtl = new JLabel("Weight (kg)"); 19. htl = new JLabel("Height (m)"); 20. rsll = new JLabel("Your BMI is:"); 21. convert = new JButton("Calculate"); 22. cp.add(wtl); 23. cp.add(wtt); 24. cp.add(htl); 25. cp.add(htt); 26. cp.add(convert); 27. cp.add(rsll); 28. convert.addActionListener(this); 29. show(); 30. }

Solution – Action Listener 31. public void actionPerformed(ActionEvent e) 32. { 33. double weight = Double.parseDouble(wtt.getText()); 34. double height = Double.parseDouble(htt.getText()); 35. double bmi = weight/(height*height); 36. double bmirounded = ((int) (bmi*1000))/1000.0; 37. rsll.setText("Your BMI is: " + bmirounded); 38. wtt.setText(""); 39. htt.setText(""); 40. } 41. 42. public static void main(String[] args) 43. { 44. new BMICalc(); 45. } 46. }

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: Grid Layout Flow Layout Border 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(); }

Download ppt "Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071."

Similar presentations