本文共 7669 字,大约阅读时间需要 25 分钟。
setChanged protected void setChanged() Marks this Observable object as having been changed; the hasChanged method will now return true. 这个方法,可以将一个Observable对象标记为可变化的对象。 |
notifyObservers public void notifyObservers( arg) If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed. Each observer has its update method called with two arguments: this observable object and the arg argument. Parameters: arg - any object. 这个方法,可以将任何Observable对象的更改消息传递给Observer对象,即将对象的变化消息传递观察者对象,以后由观察者对象表现被观察者的变化。 |
addObserver public void addObserver( o) Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set. The order in which notifications will be delivered to multiple observers is not specified. See the class comment. Parameters: o - an observer to be added. Throws: - if the parameter o is null. 这个方法可以在Observable对象上添加一个Observer对象,即由被观察者的对象为自己设定一个观察者对象,由该观察者对象负责处理自己的信息变化。 |
package com.libin.chapter15.obv; import java.util.*; public class BeingWatched extends Observable{ void counter( int period){ for (;period>=0;period--){ // 设置当前对象为可变化的对象 setChanged(); // 消息传递机制,通知观察者我要变化 notifyObservers( new Integer(period)); // 线程处理 try { Thread.sleep(1000); // 休眠 1 秒钟,再运行 } catch (InterruptedException e){ System. out .println( "sleep interrupted" ); } } } } |
update void update( o, arg) This method is called whenever the observed object is changed. An application calls an Observable object's notifyObservers method to have all the object's observers notified of the change. Parameters: o - the observable object. arg - an argument passed to the notifyObservers method. 这个方法是在任何被观察的对象发生信息变化时被JAVA系统自动调用的。但是需要注意,只有在被观察的对象上添加了观察者对象,并且被观察者对象将自己设置为可变对象后,调用了 notifyObserver(s)方法后,update()方法才会被调用。 |
package com.libin.chapter15.obv; import java.util.*; import java.util.Observable; public class Watcher implements Observer{ //用来表现模型对象的变化过程 public void update(Observable obj,Object arg){ System.out.println("update() called,count is: "+((Integer)arg).intValue()); } } |
package com.libin.chapter15.obv; import java.util.*; public class ObserverDemo{ public static void main(String args[]){ BeingWatched observed= new BeingWatched(); Watcher observing= new Watcher(); /*Add the observing to the list of observers for observed object. */ // 在模型对象上添加观察者对象 observed.addObserver(observing); // 模型对象调用自己的业务处理方法 observed.counter(10); } } |
|
package com.libin.chapter19.exec1; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; public class MyFrame extends JFrame{ Container content; MyViewer view; public MyFrame(){ this.setTitle("一个基础的绘图程序测试"); this.setBounds(100,150,400,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); content=this.getContentPane(); view=new MyViewer(); content.add(view,BorderLayout.CENTER); this.setVisible(true); } } |
package com.libin.chapter19.exec1; import java.util.Iterator; import java.util.LinkedList; import java.util.Observable; import com.libin.chapter19.demo3.Element; import java.awt.geom.*; public class MyModel extends Observable{ public boolean remove(Line2D.Double line) { boolean removed = elementList .remove(line); if (removed) { setChanged(); notifyObservers(line.getBounds()); } return removed; } public void add(Line2D.Double line) { elementList .add(line); setChanged(); notifyObservers(line.getBounds()); } public Iterator getIterator() { return elementList .listIterator(); } // 基于链表结构实现的集合类 protected LinkedList elementList = new LinkedList(); } |
package com.libin.chapter19.exec1; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.geom.Line2D; import java.text.DateFormat; import java.util.Iterator; import java.util.Observable; import java.util.Observer; import javax.swing.JComponent; import javax.swing.event.MouseInputAdapter; public class MyViewer extends JComponent implements Observer { MyModel model = new MyModel(); public MyViewer() { MyMouserHandler handler= new MyMouserHandler(); this .addMouseListener(handler); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D)g; // Get a Java 2D device context Iterator elements = model .getIterator(); Line2D.Double element; // Stores an element while (elements.hasNext()) { // Go through the list element = (Line2D.Double)elements.next(); // Get the next element g2D.setPaint(Color. red ); // Set the element color // 绘制最终的形状 g2D.draw(element); // Draw its shape } } public void update(Observable o, Object obj) { if ((Line2D.Double)obj== null ){ repaint(); } } public class MyMouserHandler extends MouseInputAdapter { private Point start ; // Stores cursor position on press private Point last ; // Stores cursor position on drag private Line2D.Double tempElement ; // Stores a temporary element private boolean button1Down = false ; // Flag for button 1 state private Graphics2D g2D = null ; public void mousePressed(MouseEvent e) { start = e.getPoint(); // Save the cursor position in start if ( button1Down = (e.getButton() == MouseEvent. BUTTON1 )) { System. out .println( " 鼠标左键被按下 " ); g2D = (Graphics2D) getGraphics(); g2D .setPaint(Color. blue ); } } public void mouseReleased(MouseEvent e) { last = e.getPoint(); tempElement = createElement( start , last ); g2D .draw( tempElement ); if ( button1Down = (e.getButton() == MouseEvent. BUTTON1 )) { button1Down = false ; // Reset the button 1 flag System. out .println( " 鼠标左键已经被释放 " ); if ( tempElement != null ) { model .add( tempElement ); // Add element to the model tempElement = null ; // No temporary now stored } // 释放绘图对象,清理绘图环境 if ( g2D != null ) { // If there's a graphics context g2D .dispose(); // ...release the resource g2D = null ; // Set field to null } start = last = null ; // Remove the points } } private Line2D.Double createElement(Point start, Point end) { return new Line2D.Double(start, end); // We should never get to here } } } |
package com.libin.chapter19.exec1; public class MyApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyApp app= new MyApp(); app.init(); } public void init(){ MyFrame myFrame= new MyFrame(); } } |
转载地址:http://vavdi.baihongyu.com/