📄 167-168.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:The Graphical User Interface</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=3//-->
<!--PAGES=167-168//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="164-167.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch04/169-172.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>In line 9, we store the mouse down event so that we can compute the relative mouse motion when the mouse is released.
</P>
<!-- CODE SNIP //-->
<PRE>
10. return super.handleEvent(e);
11. } // switch
</PRE>
<!-- END CODE SNIP //-->
<P>The invocation of the <I>processMouseUp</I> method causes a sequence of <I>if-then-else</I> methods to be fired:</P>
<!-- CODE SNIP //-->
<PRE>
1. public void processMouseUp(String object_name, int x, int y) {
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>rmove</I> point instance in line 2 is used to compute the relative mouse motion. The upside-down nature of the coordinate system imposed by the AWT designers requires that we negate the (<I>y-anchor.y</I>) displacement. This coordinate system was probably an unfortunate design choice.</P>
<!-- CODE SNIP //-->
<PRE>
2. point rmove = new point(x-anchor.x, anchor.y - y);
3. // rmove is the relative motion of the mouse.
</PRE>
<!-- END CODE SNIP //-->
<P>It is much faster to compare strings using a check between references. This approach is safe, because the strings were added to the <I>Choice</I> instance by reference. It is not safe to use this technique:</P>
<!-- CODE SNIP //-->
<PRE>
// if (object_name == “camera”) // <—- this is NOT safe
</PRE>
<!-- END CODE SNIP //-->
<P>Also, it is bad software engineering practice to embed string literals throughout the code, because typos can result in hard-to-find bugs:
</P>
<!-- CODE SNIP //-->
<PRE>
// if (object_name.equals(“camera”) // <—- this is bad engineering!
</PRE>
<!-- END CODE SNIP //-->
<P>The best way is to allow the compiler to check for type correctness. In this way, if there is a typo the compiler will emit a syntax error rather than require run-time debugging. Furthermore, the reference comparison using <I>==</I> is very fast.</P>
<!-- CODE //-->
<PRE>
4. if ( object_name== cameraStr) {
5. Camera c = (Camera) Geometry.camera;
6. c.auto_pan = ap_cb.getState();
7. c.change_config(rmove);
8. Geometry.compute_rays();
9. } else if ( object_name== laserStr) {
10. Laser l = (Laser) Geometry.laser;
11. l.change_config(rmove);
12. Geometry.compute_rays();
13. } else if ( object_name == gratingStr) {
14. Grating g = (Grating) Geometry.grating;
15. g.change_config(rmove);
16. Geometry.compute_rays();
17. } else if ( object_name == graphDLvsXStr)
18. make_graph_dl();
19. else if ( object_name == graphPvsXStr)
20. make_graph_p();
21. else if (object_name == laserAngleStr) {
22. Wedge w = (Wedge) Geometry.wedge;
23. w.change_config(rmove);
24. Geometry.compute_rays();
25. return;
26. } else if (object_name == allStr) {
27. Xform.rmove(rmove);
28. return;
29. }
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading55"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>This chapter introduced some of the basic Java classes needed to perform basic AWT programming. In the following chapters we will present a series of subclasses that extend the <I>Component</I> class.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="164-167.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch04/169-172.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 + -