📄 160-164.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=160-164//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="155-160.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="164-167.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading46"></A><FONT COLOR="#000077">Adding Four Border Scroll Bars</FONT></H4>
<P>In this section we show how to construct the four border scroll bars shown in Figure 3.8, the digital oscilloscope generator. An oscilloscope is a test instrument used to graph wave forms. The digital oscilloscope is intended to have some of the features of the traditional instrument (which can typically be purchased at significant cost). The following code illustrates how to add scroll bars to the <I>AudioFrame</I> (which is used to display the digital oscilloscope):</P>
<!-- CODE SNIP //-->
<PRE>
1. public class AudioFrame extends ClosableFrame {
2. ...
</PRE>
<!-- END CODE SNIP //-->
<P>In lines 3-6 we create the horizontal and vertical scroll bars (bottom, top, left, and right).
</P>
<!-- CODE SNIP //-->
<PRE>
3. Scrollbar sbHorzBottom = new Scrollbar(Scrollbar.HORIZONTAL);
4. Scrollbar sbHorzTop = new Scrollbar(Scrollbar.HORIZONTAL);
5. Scrollbar sbVertLeft = new Scrollbar(Scrollbar.VERTICAL);
6. Scrollbar sbVertRight = new Scrollbar(Scrollbar.VERTICAL);
</PRE>
<!-- END CODE SNIP //-->
<P>For our digital oscilloscope, we want <I>sbVertLeft</I> to translate the wave form vertically. The scroll bar <I>sbVertRight</I> will scale the wave form in height. The scale factors for the wave form are given in millivolts per division. We want divisions in millivolts to reflect the steps of the physical instrument (from 5 volts to 1 millivolt). These steps become our y-scale factors. We also want the x-scale factors to alter the number of seconds used per division. To reflect the physical instrument, we proceed in steps from 0.05 microseconds per division to 5 seconds per division. This technique models the steps and range of a circa 1969 dual-trace 150-Mhz bandwidth Tektronix model 7704 portable oscilloscope. The 7704 weighs more than 50 pounds and is portable by virtue of having two handles.</P>
<!-- CODE //-->
<PRE>
7. private int dx = 0;
8. private int oldDx = 0;
9. private int dy = 0;
10. private int oldDy = 0;
11. private final double xScaleFactors[] = {50000, 25000, 10000, 5000,
2500, 1000,
12. 250, 100, 50, 25, 10,
13. 2.5, 1, .5, .25, .1,
14. .025, .01, .005, .0025, .001,
15. .0005};
16. private final String xSFLabels[] = {“0.05 u”, “0.1 u”, “0.25 u”,
17. “0.5 u”, “1 u”, “2.5 u”,
18. “5 u”, “10 u”, “25 u”,
19. “50 u”, “100 u”, “250 u”,
20. “500 u”, “1 m”, “2.5 m”,
21. “5 m”, “10 m”, “25 m”,
22. “50 m”, “100 m”, “250 m”,
23. “500 m”, “1 “, “2.5 “, “5 “};
24. private final int xsfStartIndex = 14;
25. private double xScaleFactor = xScaleFactors[xsfStartIndex];
26. private double oldXScaleFactor = xScaleFactors[xsfStartIndex];
27. private String xSFLabel = new String(xSFLabels[xsfStartIndex]);
28. private final double yScaleFactors[] = {500, 200, 100, 50, 20, 10,
29. 2, 1, .5, .2, .1};
30. private final String ySFLabels[] = {“1 m”, “2.5 m”, “5 m”, “10 m”, “25
m”, “50 m”,
31. “100 m”, “250 m”, “500 m”, “1 “, “2.5 “, “5 “};
32. private final int ysfStartIndex = 8;
33. private double yScaleFactor = 1;
34. private double oldYScaleFactor = 1;
35. private String ySFLabel = new String(ySFLabels[ysfStartIndex]);
36. ...
37. public boolean handleEvent(Event e) {
38. ...
</PRE>
<!-- END CODE //-->
<P>During the handling of the event, we can check to see whether the event target contains the same reference as one of the components.
</P>
<!-- CODE SNIP //-->
<PRE>
39. if(e.target == sbHorzTop) {
40. int i = sbHorzTop.getValue();
41. xScaleFactor = xScaleFactors[i];
</PRE>
<!-- END CODE SNIP //-->
<P>For line 42, we check to see whether the scale factor has changed. If it has not changed, we do not call <I>repaint</I>, a function that is computationally expensive.</P>
<!-- CODE SNIP //-->
<PRE>
42. if (xScaleFactor != oldXScaleFactor) {
43. xSFLabel = xSFLabels[i];
44. repaint();
45. oldXScaleFactor = xScaleFactor;
46. }
47. return true;
48. }
</PRE>
<!-- END CODE SNIP //-->
<P>To make the scroll bars border the <I>AudioFrame</I>, we use a layout manager called <I>BorderLayout</I>:</P>
<!-- CODE //-->
<PRE>
1. public AudioFrame(String name) {
2. ...
3. setLayout(new BorderLayout());
4. add(“North”, sbHorzTop);
5. add(“South”, sbHorzBottom);
6. add(“West”, sbVertLeft);
7. add(“East”, sbVertRight);
8. // openAudioStream will set the fileName
9. // and audioStream variables.
10. openAudioStream();
11. // Set top scroll bar characteristics
12. sbHorzTop.setValues(xsfStartIndex, 0, 0, 24);
13. ...
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading47"></A><FONT COLOR="#000077">The Label Class</FONT></H3>
<P><I>Label</I> is a subclass of the <I>Component</I> class and is generally added to a container. Labels typically are not generators of useful events. A label can be updated dynamically to reflect the state of an underlying variable. For example <I>GUI.java</I> file in DiffCAD has both static and dynamic labels. Dynamic labels are discussed in a later section.</P>
<H4 ALIGN="LEFT"><A NAME="Heading48"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class Label extends Component {
public static final int LEFT
public static final int CENTER
public static final int RIGHT
public Label()
public Label(String label)
public Label(String label, int alignment)
public synchronized void addNotify()
public int getAlignment()
public void setAlignment(int alignment)
public String getText()
public void setText(String label)
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading49"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose the following variables are predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
Label l;
String name;
int alignment;
</PRE>
<!-- END CODE SNIP //-->
<P>Alignment is set to one of the following:
</P>
<!-- CODE SNIP //-->
<PRE>
Label.LEFT, Label.RIGHT, Label.CENTER.
public class Label extends Component {
</PRE>
<!-- END CODE SNIP //-->
<P>To construct an empty label, use this code:
</P>
<!-- CODE SNIP //-->
<PRE>
l = new Label();
</PRE>
<!-- END CODE SNIP //-->
<P>To construct a named label:
</P>
<!-- CODE SNIP //-->
<PRE>
l = new Label(name);
</PRE>
<!-- END CODE SNIP //-->
<P>To construct a named label with known alignment:
</P>
<!-- CODE SNIP //-->
<PRE>
l = new Label(name, alignment);
</PRE>
<!-- END CODE SNIP //-->
<P>To make the label peer
</P>
<!-- CODE SNIP //-->
<PRE>
l.addNotify();
</PRE>
<!-- END CODE SNIP //-->
<P>To get and set alignment:
</P>
<!-- CODE SNIP //-->
<PRE>
alignment = l.getAlignment();
l.setAlignment(alignment);
</PRE>
<!-- END CODE SNIP //-->
<P>To get and set the string that the label contains:
</P>
<!-- CODE SNIP //-->
<PRE>
name = l.getText();
l.setText(name);
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading50"></A><FONT COLOR="#000077">Adding Labels to Frames</FONT></H4>
<P>To add labels to a frame, you need only invoke the following:
</P>
<!-- CODE SNIP //-->
<PRE>
add(new Label(“Grid:”));
</PRE>
<!-- END CODE SNIP //-->
<P>No event handler code is required. Also, any extension of the <I>Container</I> class can add components.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="155-160.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="164-167.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 + -