From 2a378ba9b5edda1a7d1868e6746e914ffbb7a224 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Wed, 3 May 2006 22:38:56 +0000 Subject: [PATCH] Otra interfaz mas --- src/AngCanvas.java | 34 ++++++++++++++++++++++++++ src/AngGraf.java | 59 ++++++++++++++++++++++++++++++++++++++++++++++ src/Angulo.java | 11 ++++++++- 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/AngCanvas.java create mode 100644 src/AngGraf.java diff --git a/src/AngCanvas.java b/src/AngCanvas.java new file mode 100644 index 0000000..20b61a1 --- /dev/null +++ b/src/AngCanvas.java @@ -0,0 +1,34 @@ + +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +public class AngCanvas extends Canvas implements Observer +{ + private int radianes; + + public AngCanvas (Angulo g) { + radianes = g.getGrados (); + } + + public void paint(Graphics g) { + g.setColor(Color.BLACK); + g.fillRect(0, 0, 400, 400); + g.setColor( Color.RED ); + g.fillArc(100, 100, 200 , 200, 0, radianes); + System.out.println (radianes); + } + + public Dimension getPreferredSize(){ + return new Dimension (400, 400); + } + + public void update(Observable obs, Object obj) + { + if (obs instanceof Angulo) { + radianes = ((Angulo)obs).getGrados (); + } + repaint (); + } +} + diff --git a/src/AngGraf.java b/src/AngGraf.java new file mode 100644 index 0000000..698954a --- /dev/null +++ b/src/AngGraf.java @@ -0,0 +1,59 @@ + +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +public class AngGraf extends Frame implements Observer +{ + private Panel panel; + private Button boton; + private Label label; + private AngCanvas canvas; + private Angulo angulo; + + public AngGraf (Angulo f) + { + angulo = f; + + panel = new Panel (); + label = new Label(f.toString (), Label.CENTER); + boton = new Button ("Mitad!"); + canvas = new AngCanvas (f); + + panel.setLayout (new BorderLayout ()); + panel.add (label, BorderLayout.NORTH); + panel.add (canvas, BorderLayout.CENTER); + panel.add (boton, BorderLayout.SOUTH); + + add (panel, BorderLayout.CENTER ); + setSize (500, 300); + setVisible (true); + + f.addObserver (this); + f.addObserver (canvas); + } + + public boolean action (Event evt, Object obj) { + if (evt.target.equals(boton)) { + angulo.fromRadianes ( angulo.toRadianes () / 2); + } + + return true; + } + + public void update(Observable obs, Object obj) + { + label.setText (obs.toString ()); + } + + static public void main (String[] args) { + Angulo modelo = null; + try { + modelo = new Angulo (359, 0, 0); + } + catch (Exception e) { + } + + AngGraf vista = new AngGraf (modelo); + } +} diff --git a/src/Angulo.java b/src/Angulo.java index 34e24cc..61a8d44 100644 --- a/src/Angulo.java +++ b/src/Angulo.java @@ -1,5 +1,6 @@ +import java.util.*; -public class Angulo implements OperAng, ComparaAng { +public class Angulo extends Observable implements OperAng, ComparaAng { private int signo; private int grados; @@ -27,19 +28,27 @@ public class Angulo implements OperAng, ComparaAng { signo = -1; else signo = 1; + setChanged (); + notifyObservers (); } public void setGrados(int g) { setSigno(g); grados = Math.abs(g % 360); + setChanged (); + notifyObservers (); } public void setMinutos(int m) { minutos = Math.abs(m % 60); + setChanged (); + notifyObservers (); } public void setSegundos(int s) { segundos = Math.abs(s % 60); + setChanged (); + notifyObservers (); } public Angulo() { -- 2.43.0