oop lecture9

15
More on implementing Listeners Lecture 9 Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria

Upload: shahriar-robbani

Post on 26-May-2015

127 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Oop lecture9

More on implementing Listeners

Lecture 9

Object Oriented ProgrammingEastern University, Dhaka

Md. Raihan Kibria

Page 2: Oop lecture9

Add a combo box

We will add an item change listener and print to console.

See next page for code

Page 3: Oop lecture9

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(); }}

Page 4: Oop lecture9

Output

Page 5: Oop lecture9

Overriding paint method of Component

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)); }}

Page 6: Oop lecture9

Output

We want to add a circle to the panel above

Page 7: Oop lecture9

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); }}

Page 8: Oop lecture9

Output

Page 9: Oop lecture9

Now we want to add a circle, a rectangle and a triangle

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.add(new MySecondPanel()); f.add(new MyThirdPanel());

f.setVisible(true); }}

More code next..

Page 10: Oop lecture9

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..

Page 11: Oop lecture9

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); }}

Page 12: Oop lecture9
Page 13: Oop lecture9

More on overriding

public 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()); }}

Page 14: Oop lecture9

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; }}

Page 15: Oop lecture9

Output

CGPA by class I: 3.0CGPA by class J: 3.6