Chapter 10: Applets

The exercises here are taken from my forthcoming book, The Java Developer's Resource.

Quiz

  1. What would be the problems with writing a telnet program as an applet?

    A telnet program needs to be able to connect to arbitrary hosts on the Internet. The applet security manager prevents used by Netscape and the Applet Viewer prevents that.

  2. What would be the biggest problems with trying to write a word processor as an applet?

    A word processing program needs to read files from and save files to the local hard drive. The applet security manager prevents used by Netscape and the Applet Viewer prevents that.

Exercises

  1. Modify Program 10.2 so that it does not need AppletHeight or AppletWidth variables or their equivalents. Hint: you won't need to import java.awt.Dimension either.

    The trick is to use size().height and size(0.width directly in the call to drawString.

    //Print out the applet's size
    
    import java.applet.Applet;    
    import java.awt.Graphics; 
    
    
    public class getSizeApplet extends Applet {
    
      public void paint(Graphics g) {
        
        g.drawString("This applet is " + size().height + 
          " pixels high by " + size().width + " pixels wide.", 
          15, size().height/2);
        
      }
    
    }
    
  2. Assume that each letter occupies a box fifteen pixels wide by fifteen pixels high. Write an applet which takes a single letter as a parameter and fills the available space with that letter.
    import java.applet.Applet;    
    import java.awt.Graphics; 
    
    
    public class letterFillApplet extends Applet {
    
      String theLetter;
    
      public void init() {
      
        String s = getParameter("Letter");
        theLetter = s.substring(0, 1);
      
      }
    
      public void paint(Graphics g) {
        
        for (int i = 0; i < size().width; i += 15) {
          for (int j = 0; j < size().height; j += 15) {
            g.drawString(theLetter, i, j);
          }
        }
        
      }
    
    }
    
  3. The PoetryApplet handles at most 100 lines because that is all the space that's been allotted to the poem array. Adjust the PoetryApplet so that it includes a numlines parameter that will set the size of the poem array at runtime.

    import java.applet.Applet;    
    import java.awt.Graphics; 
                 
    public class PoetryApplet extends Applet {
    
      String[] poem;
      int numlines;
    
      public void init() {
    
        int i;
        String nextline;
        
        try {
          String s = getParameter("numlines");
          if (s == null) numlines = 101;
          else numlines = Integer.parseInt(s);
        }
        catch (NumberFormatException e) {
          numlines = 101;
        }
        
        poem = new String[numlines];
    
        for (i = 1; i <= numlines; i++) {
          nextline = getParameter("Line" + i);
          if (nextline == null) break;
          poem[i] = nextline;
        }
        numlines = i-1;
        
      }
      
      public void paint(Graphics g) {
      
        int y = 15;
      
        for (int i=1; i <= numlines; i++) {
          g.drawString(poem[i], 5, y);    
          y += 15;
        }
        
      }
      
    } 
  4. Change the PoetryApplet to use a Vector rather than an array so that it will truly handle an arbitrary number of parameters.

    import java.applet.Applet;    
    import java.awt.Graphics;
    import java.util.Vector; 
    import java.util.Enumeration; 
                 
    public class PoetryApplet extends Applet {
    
      Vector poem;
    
      public void init() {
    
        int i = 1;
        String nextline;
        poem = new Vector();
    
        while ((nextline = getParameter("Line" + i++)) != null) {
           poem.addElement(nextline);
        }
        
      }
      
      public void paint(Graphics g) {
      
        int y = 15;
      
        Enumeration e = poem.elements();
        while (e.hasMoreElements()) {
          g.drawString((String) e.nextElement(), 5, y);    
          y += 15;
        }
        
      }
      
    } 
    


[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified August 22, 1996