📄 310-315.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:An Introduction to Image Processing</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=7//-->
<!--PAGES=310-315//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="307-310.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="315-319.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>There is a directed flow of information between an instance of a class that extends <I>Observable</I> and the instance of the class that implements <I>Observer</I>. The <I>Observable</I> instance keeps an instance of a vector that lists all the <I>Observer</I> instances that have registered an interest in the state of the <I>Observable</I> instance. Whenever the <I>Observable</I> instance changes, it broadcasts an update message to each of the <I>Observer</I> instances that have registered. This relationship is shown in Figure 7.2.</P>
<!-- CODE SNIP //-->
<PRE>
<I>p</I><SUB>i</SUB> = ith image pixel
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/07-02.jpg',255,240 )"><IMG SRC="images/07-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-02.jpg',255,240)"><FONT COLOR="#000077"><B>Figure 7.2</B></FONT></A> The Observable uses the update method to transmit data to the Observer.</P>
<P>A class that implements the <I>Observer</I> interface supports an <I>update</I> method that takes an <I>Object</I>-typed argument.</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Interface Summary</FONT></H4>
<!-- CODE SNIP //-->
<PRE>
package java.util;
public interface Observer {
void update(Observable o, Object arg);
}
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">The Observable Class</FONT></H3>
<P>The <I>Observable</I> class resides in the <I>java.util</I> package. An instance of the <I>Observable</I> class uses an instance of a list of the <I>Observer</I> instances that have registered themselves. The list is called an <I>ObserverList</I> and resides invisibly within the <I>java.util</I> package.</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
package java.util;
public class Observable {
public synchronized void addObserver(Observer o)
public synchronized void deleteObserver(Observer o)
public void notifyObservers()
public synchronized void notifyObservers(Object arg)
public synchronized void deleteObservers()
public synchronized boolean hasChanged()
public synchronized int countObservers()
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">The NamedObservable Class</FONT></H4>
<P>The <I>NamedObservable</I> class provides a mechanism to associate a <I>String</I> instance with the <I>Observable</I> instance.</P>
<!-- CODE //-->
<PRE>
package observers;
import java.util.*;
// The NamedObservable is just like an Observable
// except that it has a name property associated with
// every Observable instance.
public abstract class NamedObservable extends Observable {
private String name;
public synchronized void setName(String nm) {
name = nm;
}
public synchronized String getName() {
return name;
}
}
</PRE>
<!-- END CODE //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>The <I>get</I> and <I>set</I> methods in the <I>NamedObservable</I> class are synchronized to prevent contention problems during multithreaded operation. It is common to see class variables, such as the <I>name</I> string, as being accessed only through synchronized methods. This is the reason for declaring the <I>name</I> string private.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">The ObservableDouble and Observer-Observable Example</FONT></H4>
<P>The DiffCAD program depends on the model-view paradigm and so has a package called <I>observers</I>. One of the classes in the <I>observers</I> package is <I>ObservableDouble</I>.</P>
<P>When the <I>setValue</I> method is invoked on an instance of the <I>ObservableDouble</I> class, the <I>setChanged</I> method is invoked and the <I>notifyObservers</I> method causes an <I>update</I> method to be broadcast to all the interested <I>Observer</I> instances.</P>
<!-- CODE //-->
<PRE>
package observers;
import java.util.*;
public class ObservableDouble extends NamedObservable {
// The value of interest
private double value;
public ObservableDouble(double newValue, String nm) {
value = newValue;
setName(nm);
}
public synchronized void setValue(double newValue) {
if (newValue != value) {
value = newValue;
super.setChanged();
super.notifyObservers();
}
}
public synchronized double getValue() {
return value;
}
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">DoubleDialog and IntDialog</FONT></H4>
<P>In the <I>observers</I> package are the <I>DoubleDialog</I> and <I>IntDialog</I> classes. When an instance of <I>IntDialog</I> is made, a display is generated like the one shown in Figure 7.3.</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/07-03.jpg',230,63 )"><IMG SRC="images/07-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-03.jpg',230,63)"><FONT COLOR="#000077"><B>Figure 7.3</B></FONT></A> IntDialog with an embedded <I>Observable</I> on display.
</P>
<P>The value of the <I>int</I> is initially used for the display in the text field. When the <I>int</I> is altered by the user, the <I>setValue</I> method causes the <I>observers</I> to be updated, dynamically altering the text field in the dialog box. The <I>IntDialog</I> class follows. (The <I>DoubleDialog</I> is almost the same except that it contains an <I>ObservableDouble</I> rather than an <I>ObservableInt</I>.)</P>
<!-- CODE //-->
<PRE>
package observers;
import java.awt.*;
import java.applet.*;
import java.io.*;
import gui.*;
public class IntDialog extends ClosableFrame {
Label label1;
IntTextField textfield;
Button okButton;
Button cancelButton;
ObservableInt i;
private static int offset = 25;
public IntDialog(
String dialog_title,
String label,
ObservableInt sample_int) {
super(dialog_title);
init( dialog_title, label);
set(sample_int);
setForeground(Color.white);
setBackground(Color.white);
}
void set(ObservableInt sample_int) {
i = sample_int;
textfield = new IntTextField(i);
add("Right",textfield);
textfield.reshape(89, 25, 64, 16);
cancelButton = new Button("Cancel");
add(cancelButton);
cancelButton.reshape(13, 175, 88, 28);
okButton = new Button("OK");
add(okButton);
okButton.reshape(130, 175, 63, 29);
pack();
show();
}
public void init(String dialog_title, String label) {
setLayout(new FlowLayout());
resize(250, 250);
reshape(offset, offset,
250, 250);
offset = offset + 15;
setResizable(false);
//Initialize components
label1 = new Label(label);
add("Left", label1);
label1.reshape(5, 19, 86, 23);
}
void ok() {
System.out.println("OK!");
String s = textfield.getText();
Integer i_Integer = new Integer(i.getValue());
// default values
try {
i_Integer = Integer.valueOf(s);
i.setValue(i_Integer.intValue());
}
catch (NumberFormatException e) {
System.out.println(
"IntDialog:ER! Not a number, using defaults");
// pick a reasonable default
}
return;
}
private void cancel() {
System.out.println("Cancel!");
}
public boolean handleEvent(Event event) {
if (Evt.match(event, `o', okButton))
ok();
else if (Evt.match(event, `k', cancelButton))
cancel();
return super.handleEvent(event);
}
}
</PRE>
<!-- END CODE //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>We realize that the user may want to make the <I>cancel</I> method perform a task other than print “Cancel.” This would be a good spot to send an application-specific message or restore a value.<HR></FONT>
</BLOCKQUOTE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="307-310.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="315-319.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -