📄 ch6.htm
字号:
<BR> String fieldParam;<BR> String selectParam;<BR> // Add first row of fieldand display image...<BR> if (((fieldParam = getParameter("field1"))!= null) &&<BR> ((selectParam= getParameter("image1")) != null) ) {<BR> button[0] = createCatalogButton(fieldParam,data[0]);<BR> drawing[0] = new SelectionCanvas(this,<BR> selectParam,getDocumentBase());<BR> button[0].resize(150,100);<BR> drawing[0].resize(250,100);<BR> } // end if<BR> else {<BR> getAppletContext().showStatus("Invalidparameter");<BR> button[0] = null;<BR> }<BR><BR> // Add second row of fieldand display image...<BR> if (((fieldParam = getParameter("field2"))!= null) &&<BR> ((selectParam= getParameter("image2")) != null) ) {<BR> button[1] = createCatalogButton(fieldParam,data[1]);<BR> drawing[1] = new SelectionCanvas(this,<BR> selectParam,getDocumentBase());<BR> drawing[1].resize(250,100);<BR> button[1].resize(150,100);<BR> } // end if<BR> else {<BR> getAppletContext().showStatus("Invalidparameter");<BR> button[1] = null;<BR> }<BR><BR> // Add third row of fieldand display image...<BR> if (((fieldParam = getParameter("field3"))!= null) &&<BR> ((selectParam= getParameter("image3")) != null) ) {<BR> button[2] = createCatalogButton(fieldParam,data[2]);<BR> drawing[2] = new SelectionCanvas(this,<BR> selectParam,getDocumentBase());<BR> button[2].resize(150,100);<BR> drawing[2].resize(250,100);<BR> } // end if<BR> else {<BR> getAppletContext().showStatus("Invalidparameter");<BR> button[2] = null;<BR> }<BR> }<BR><BR> // Load additional data from URL...<BR> void loadURLData() {<BR> // Get path to data from parameter...<BR> String dataPath = getParameter("data");<BR> if (dataPath == null) {<BR> System.out.println("Nodata variable found");<BR> return;<BR> } // end if<BR> // Create URL for data...<BR> URL u;<BR> try {<BR> u = new URL(getDocumentBase(),dataPath);<BR> }<BR> catch (MalformedURLException e) {<BR> System.out.println("BadData URL");<BR> return;<BR> }<BR><BR> // Now load the data by opening up a stream<BR> // to the URL...<BR> try {<BR> DataInputStream dis = new DataInputStream(<BR> new BufferedInputStream(<BR> u.openStream()) );<BR> // Read only the first three lines...<BR> int i;<BR> for (i = 0; i < 3; ++i) {<BR> data[i]= dis.readLine();<BR> } // end for<BR> }<BR> catch (IOException e) {<BR> System.out.println("URLread error");<BR> }<BR> }<BR><BR> // Update message sent when repainting is needed...<BR> // Prevent paint from getting cleared out...<BR> public void update(Graphics g) {<BR> paint(g);<BR> }<BR><BR> // Repaint all the canvas components...<BR> public synchronized void paint(Graphics g) {<BR> int i,x,y;<BR> Dimension dm;<BR> int defHeight = 150;<BR><BR> // Go through the buttons...<BR> for (x = y = i = 0; i < 3; ++i) {<BR> if (button[i] != null) {<BR> button[i].paint(g,x,y);<BR> dm = button[i].size();<BR> x += dm.width;<BR> drawing[i].paint(g,x,y);<BR> x = 0;<BR> y += dm.height;<BR> }<BR> else<BR> y += defHeight;<BR> } // end for<BR> }<BR><BR> public boolean mouseDown(Event ev, int x, int y) {<BR> // See if you clicked on anyof the buttons...<BR> for (int i = 0; i < 3;++i) {<BR> if ((button[i]!= null) && (button[i].inside(x,y)) ) {<BR> System.out.println("HitButton " + i);<BR> //Link to the button's selected field...<BR> button[i].select();<BR> break;<BR> } //end if<BR> }<BR> return true;<BR> };<BR><BR> // Parse a parameter and create catalog field...<BR> CatalogButton createCatalogButton(String param,Stringdata) {<BR> // Set up defaults...<BR> String fieldName = "";<BR> String imageName = "";<BR> String style = "MEDIUM";<BR> String link = getDocumentBase().toString();<BR> // Parse out the string...<BR> StringTokenizer s = new StringTokenizer(param,",");<BR> if (s.hasMoreTokens()) {<BR> fieldName = s.nextToken();<BR> if (s.hasMoreTokens()) {<BR> imageName = s.nextToken();<BR> if (s.hasMoreTokens()) {<BR> style =s.nextToken();<BR> if (s.hasMoreTokens()){<BR> link= s.nextToken();<BR> }<BR> } // end style if<BR> } // end image if<BR> } // end field if<BR> // Figure out the style. Convert it allto uppercase...<BR> style = style.toUpperCase();<BR> int styleType;<BR> if (style.equals("MEDIUM"))<BR> styleType= MEDIUM_STYLE;<BR> else if (style.equals("SMALL"))<BR> styleType= SMALL_STYLE;<BR> else if (style.equals("LARGE"))<BR> styleType= LARGE_STYLE;<BR> else<BR> styleType= DEFAULT_STYLE;<BR><BR> // Create button according to these parameters...<BR> return new CatalogButton(this,<BR> imageName,fieldName,<BR> styleFont[styleType],getDocumentBase(),<BR> link,data,dataFont);<BR> }<BR>}</TT></BLOCKQUOTE><HR><H3><A NAME="TheCatalogButtonClass">The CatalogButton Class</A></H3><P>The CatalogButton class is used to represent the choices you canmake for each applet screen. It appears on the left-hand sideof the applet and goes to another URL when the button is selected.This URL will represent another page in the catalog.<P>The CatalogButton constructor takes as input the main and subtext fields, the corresponding fonts, the background image, anda URL and relative path used to construct the URL the button willlink to. One of the key things about the class is that its Imageobject is loaded through the MediaLoader class (described shortly)and not through the applet's <TT>getImage()</TT>method. This approach takes advantage of the caching techniquesthat will be developed in the MediaLoader throughout this chapter.<P>The <TT>paint()</TT> method is invokedby the Catalog class and passed the x-y coordinate where the imageshould be painted on the applet. It draws the image by using the<TT>drawImage()</TT> method. It thendraws a title field in the center of the button. The FontMetricsare used to center the text. Finally, if an additional data fieldexists, it is drawn underneath the title in a smaller font.<P>If the button is selected, the CatalogButton object changes itstext to white by setting an internal state variable and forcinga repaint. It then creates a URL object out of the base URL andits relative path. This URL represents the new catalog page inwhich to link. If the URL is successfully created, it links tothe new page with the <TT>showDocument()</TT>method. If there is an error, then a message is displayed on thebrowser's status bar.<HR><BLOCKQUOTE><B>Listing 6.8. The CatalogButton class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>import java.awt.*;<BR>import java.lang.*;<BR>import java.net.*;<BR>import java.applet.*;<BR><BR>// This class represents a button on the current page.<BR>// It represents the lefthand element of a kiosk selection<BR>// that has a background<BR>// image and text describing what the button represents.<BR>// The field has a link to the Web page it should go to<BR>// if it is selected...<BR>// Parent of button is responsible for sizing and setting<BR>// correct position...<BR>public class CatalogButton extends Canvas {<BR> String text; // What to display on left side...<BR> String data; // Additional data<BR> URL URLBase; // The base URL to link from...<BR> String link; // Where to link to relative tobase...<BR> Image img; // Background image...<BR> Font f; // Font to pain
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -