Oop lecture9

201 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
201
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop lecture9

  1. 1. Lecture 9More on implementing Listeners Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. Add a combo box We will add an item change listener and print to console. See next page for code
  3. 3. public class ComboDemo extends JFrame { ComboDemo(){ super.setBounds(0, 0, 500, 400); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); super.setLayout(new FlowLayout()); final JTextField text = new JTextField("Select an item"); class MyItemListener implements ItemListener{ JComboBox combo; MyItemListener(JComboBox combo){ this.combo = combo; } public void itemStateChanged(ItemEvent e) { text.setText(combo.getSelectedItem().toString()); } } JComboBox combo = new JComboBox(new String[]{"Square", "Circle","Triangle"}); combo.addItemListener(new MyItemListener(combo)); super.add(combo); super.add(text); super.setVisible(true); } public static void main(String[] args){ new ComboDemo(); }}
  4. 4. Output
  5. 5. Overriding paint method of Componentpublic class OverridePaintDemo { public static void main(String[] args) { JFrame f = new JFrame(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new FlowLayout()); f.add(new MyFirstPanel()); f.setVisible(true); }}class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); }}
  6. 6. OutputWe want to add a circle to the panel above
  7. 7. public class OverridePaintDemo { public static void main(String[] args) { JFrame f = new JFrame(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new FlowLayout()); f.add(new MyFirstPanel()); f.setVisible(true); }}class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawOval(5, 5, 90, 90); }}
  8. 8. Output
  9. 9. Now we want to add a circle, a rectangle and a trianglepublic class OverridePaintDemo { public static void main(String[] args) { JFrame f = new Jframe(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); f.setLayout(new FlowLayout()); f.add(new MyFirstPanel()); f.add(new MySecondPanel()); f.add(new MyThirdPanel()); f.setVisible(true); }} More code next..
  10. 10. class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawOval(5, 5, 90, 90); }}class MySecondPanel extends JPanel{ MySecondPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawRect(10, 15, 80, 60); }} More code next..
  11. 11. class MyThirdPanel extends JPanel{ MyThirdPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawLine(10, 90, 90, 90); g.drawLine(10, 90, 90, 10); g.drawLine(90, 10, 90, 90); }}
  12. 12. More on overridingpublic class OverrideMethodDemo { public static void main(String[] args) { System.out.println("CGPA by class I: " + new I().getCgpa()); System.out.println("CGPA by class J: " + new J().getCgpa()); }}
  13. 13. class I{ int[] gpas = new int[]{3, 4, 2, 4, 5}; int calculateSum(){ int sum = 0; for (int i=0; i<gpas.length; i++) sum += gpas[i]; return sum; } public float getCgpa(){ int sum = calculateSum(); return sum/gpas.length; }}class J extends I{ public float getCgpa() { int sum = calculateSum(); return (float)sum / (float)gpas.length; }}
  14. 14. OutputCGPA by class I: 3.0CGPA by class J: 3.6

×