From 00e985a089435f038cdd782db9060f30c45f87c4 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Thu, 11 May 2006 00:35:21 +0000 Subject: [PATCH] MVC en 4.3 --- src/Cantadores.java | 20 +++++++++++++-- src/FrameCantores.java | 58 ++++++++++++++++++++++++++++++++++++++++++ src/Sorteo.java | 7 ++++- 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/FrameCantores.java diff --git a/src/Cantadores.java b/src/Cantadores.java index 376d0e7..3511800 100644 --- a/src/Cantadores.java +++ b/src/Cantadores.java @@ -1,19 +1,35 @@ import java.util.*; -public class Cantadores implements Observer +public class Cantadores extends Observable implements Observer { String nombre; + int n; + public Cantadores (String s) { nombre = s; } + public int getLastN () + { + return n; + } + + public String getNombre () + { + return nombre; + } + public void update(Observable obs, Object obj) { if (obs instanceof Sorteo) { Sorteo s = (Sorteo)obs; - if ((s.getN()>=40) && (s.getN()<=49)) + if ((s.getN()>=40) && (s.getN()<=49)) { System.out.println ("Canto "+nombre+" "+s.getN()); + n = s.getN(); + setChanged (); + notifyObservers (); + } } else { System.out.println ("E"); } diff --git a/src/FrameCantores.java b/src/FrameCantores.java new file mode 100644 index 0000000..e9be0f5 --- /dev/null +++ b/src/FrameCantores.java @@ -0,0 +1,58 @@ + +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +public class FrameCantores extends Panel implements Observer +{ + private Label nombre; + private Label valor; + + public FrameCantores (Cantadores c) + { + nombre = new Label (c.getNombre ()); + valor = new Label ("No canto nada"); + + setLayout (new BorderLayout ()); + add (nombre, BorderLayout.CENTER); + add (valor, BorderLayout.SOUTH); + + setSize (50, 100); + setVisible (true); + + c.addObserver (this); + } + + public void update(Observable obs, Object obj) + { + if (obs instanceof Cantadores) { + valor.setText ((new Integer (((Cantadores)obs).getLastN ())).toString ()); + } + } + + static public void main (String [] args) + { + Sorteo s = new Sorteo (); + Cantadores c1 = new Cantadores ("Pablo"); + Cantadores c2 = new Cantadores ("Mariela"); + Cantadores c3 = new Cantadores ("Angela"); + s.addObserver (c1); + s.addObserver (c2); + s.addObserver (c3); + + FrameCantores f1 = new FrameCantores (c1); + FrameCantores f2 = new FrameCantores (c2); + FrameCantores f3 = new FrameCantores (c3); + + Frame p = new Frame (); + p.setLayout (new BorderLayout ()); + p.add (f1, BorderLayout.WEST); + p.add (f2, BorderLayout.CENTER); + p.add (f3, BorderLayout.EAST); + + p.setSize (500, 100); + p.setVisible (true); + s.Go (); + } +} + diff --git a/src/Sorteo.java b/src/Sorteo.java index 2b2741c..14a2a41 100644 --- a/src/Sorteo.java +++ b/src/Sorteo.java @@ -1,6 +1,7 @@ import java.util.*; - +import java.lang.Thread; + public class Sorteo extends Observable { Random r = new Random (); @@ -22,6 +23,10 @@ public class Sorteo extends Observable n = r.nextInt (100); setChanged (); notifyObservers (); + try { + Thread.sleep (500); + } + catch (Exception e) {} } } } -- 2.43.0