SiepCanvas
.GraphicCounter
et le SiepCanvas
import java.awt.* ; import java.awt.event.*; class Siep extends Frame { Siep (String title, int n) { super(title) ; Counter c = new Counter(n) ; GraphicCounter gc = new GraphicCounter(c) ; SiepCanvas sc = new SiepCanvas(c) ; Button quit = new QuitButton () ; add("North", gc) ; add("Center",sc) ; add("South", quit) ; setSize (450, 480); setLocation(100,100) ; } public static void main (String[] args) { int n = 3 ; if (args.length > 0) n = Integer.parseInt(args[0]) ; new Siep("Sierpinsky", n).show() ; } } |
class OtherCounter extends Counter { private Siep s ; OtherCounter(int n, Siep s) { super(n) ; this.s = s ; } void incr() { super.incr() ; s.sc.repaint() } void decr() { super.decr() ; s.sc.repaint() } } |
Siep
ainsi :
class Siep extends Frame { SiepCanvas sc ; Siep (String title, int n) { … Counter c = new OtherCounter(n, this) ; GraphicCounter gc = new GraphicCounter(c) ; sc = new SiepCanvas(c) ; … } } |
ActionListener
.
Une classe Siep.
import java.awt.* ; import java.awt.event.*; class Siep extends Frame { Siep (String title, int n) { super(title) ; Counter c = new Counter(n) ; GraphicCounter gc = new GraphicCounter(c) ; SiepCanvas sc = new SiepCanvas(c) ; Button quit = new QuitButton () ; add("North", gc) ; add("Center",sc) ; add("South", quit) ; gc.addActionListener (new RepaintListener (sc)) ; setSize (450, 480); setLocation(100,100) ; } public static void main (String[] args) { int n = 3 ; if (args.length > 0) n = Integer.parseInt(args[0]) ; new Siep("Sierpinsky", n).show() ; } } class RepaintListener implements ActionListener { Component comp ; RepaintListener(Component comp) { this.comp = comp ; } public void actionPerformed(ActionEvent e) { comp.repaint() ; } } |