home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / code / ch11.txt < prev   
Text File  |  1998-12-14  |  13KB  |  556 lines

  1. Copy the file GetInetFile.class from the CD-ROM (in the directory for material from Chapter 11)
  2.  to your directory.
  3.  
  4.  
  5.  
  6.  
  7.  
  8. Lines.java:
  9.  
  10. import java.applet.Applet;
  11. import java.awt.*;
  12.  
  13. /**
  14.  * class LineColors holds 24 color values
  15.  */
  16. class LineColors {
  17.  
  18. /**
  19.  * color[] array holds the colors to be used
  20.  */
  21. Color color[];
  22.  
  23. /**
  24.  * class constructor
  25.  * initializes the color array using an arbitrary algorithm
  26.  */
  27. public LineColors () {
  28.  
  29.     color = new Color[24];
  30.     int i, rgb;
  31.  
  32.     rgb = 0xff;
  33.     for (i=0; i<24; i+=1) {
  34.         color[i] = new Color (rgb);
  35.         rgb <<= 1;
  36.         if ((rgb & 0x1000000) != 0) {
  37.             rgb |= 1;
  38.             rgb &= 0xffffff;
  39.         }
  40.     }
  41. }
  42. }
  43.  
  44. /**
  45.  * class describing one line segment
  46.  */
  47. class Segment {
  48.  
  49. /*
  50.  * x1, y1 - starting coordinates for this segment
  51.  * x2, y2 - ending coordinates for this segment
  52.  * dx1,...dy2 - velocities for the endpoints
  53.  * whichcolor - the current index into color array
  54.  * width, height - width and height of bounding panel
  55.  * LC - instance of LineColors class
  56.  */
  57. double x1, y1, x2, y2;
  58. double dx1, dy1, dx2, dy2;
  59. int whichcolor, width, height;
  60. LineColors LC; 
  61.  
  62. /**
  63.  * class constructor
  64.  * initialize endpoints and velocities to random values
  65.  * @param w - width of bounding panel
  66.  * @param h - height of bounding panel
  67.  * @param c - starting color index
  68.  * @param lc - instance of LineColors class
  69.  */
  70. public Segment (int w, int h, int c, LineColors lc) {
  71.  
  72.     whichcolor = c;
  73.     width = w;
  74.     height = h;
  75.     LC = lc;
  76.     x1 = (double) w * Math.random ();
  77.     y1 = (double) h * Math.random ();
  78.     x2 = (double) w * Math.random ();
  79.     y2 = (double) h * Math.random ();
  80.  
  81.     dx1 = 5 - 10 * Math.random ();
  82.     dy1 = 5 - 10 * Math.random ();
  83.     dx2 = 5 - 10 * Math.random ();
  84.     dy2 = 5 - 10 * Math.random ();
  85. }
  86.  
  87. /*
  88.  * increment color index
  89.  * calculate the next endpoint position for this segment
  90.  */
  91. void compute () {
  92.  
  93.     whichcolor += 1;
  94.     whichcolor %= 24;
  95.  
  96.     x1 += dx1;
  97.     y1 += dy1;
  98.     x2 += dx2;
  99.     y2 += dy2;
  100.  
  101.     if (x1 < 0 || x1 > width) dx1 = -dx1;
  102.     if (y1 < 0 || y1 > height) dy1 = -dy1;
  103.     if (x2 < 0 || x2 > width) dx2 = -dx2;
  104.     if (y2 < 0 || y2 > height) dy2 = -dy2;
  105. }
  106.  
  107. /**
  108.  * draw the line segment using the current color
  109.  * @param g - destination graphics object
  110.  */
  111. void paint (Graphics g) {
  112.  
  113.     g.setColor (LC.color [whichcolor]);
  114.     g.drawLine ((int) x1, (int) y1, (int) x2, (int) y2);
  115. }
  116. }
  117.  
  118. /**
  119.  * The applet/application proper
  120.  */
  121. public class Lines extends Applet {
  122.  
  123. /*
  124.  * Nlines - number of line segments to be displayed
  125.  * lines - array of instances of Segment class
  126.  * LC - instance of LineColors class
  127.  */
  128. int width,height;
  129. final int NLines = 4;
  130. Segment lines[] = new Segment[NLines];
  131. LineColors LC = new LineColors ();
  132.  
  133. /**
  134.  * init is called when the applet is loaded
  135.  * save the width and height
  136.  * create instances of Segment class
  137.  */
  138. public void init () {
  139.  
  140.     setLayout(null);
  141.     width = 300;
  142.     height = 300;
  143.     setSize(width, height);
  144.  
  145.     int i;
  146.     for (i=0; i<NLines; i+=1)
  147.         lines[i] = new Segment (width, height, (2*i) % 24, LC);
  148. }
  149.  
  150. /**
  151.  * recompute the next endpoint coordinates for each line
  152.  * invoke paint() method for each line
  153.  * call repaint() to force painting 50ms later
  154.  * @param g - destination graphics object
  155.  */
  156. public void paint (Graphics g) {
  157.  
  158.     int i;
  159.     for (i=0; i<NLines; i+=1) {
  160.         lines[i].compute ();
  161.         lines[i].paint (g);
  162.     }
  163.     repaint (50); 
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. ReadProperties.java:
  174.  
  175. import java.lang.*;
  176.  
  177. class ReadProperties {
  178.  
  179.     public static void main(String[] args) {
  180.  
  181.         String str;
  182.  
  183.         try {
  184.  
  185.             str = System.getProperty("os.name", "not specified");
  186. System.out.println("  OS name: " + str);
  187.  
  188.             str = System.getProperty("java.version", "not specified");
  189. System.out.println("  JVM Version: " + str);
  190.  
  191.             str = System.getProperty("user.home", "not specified");
  192. System.out.println("  user.home: " + str);
  193.  
  194.             str = System.getProperty("java.home", "not specified");
  195. System.out.println("  java.home: " + str);
  196.  
  197.         } catch (Exception e) {
  198.             System.err.println("Caught exception " + e.toString());
  199. }
  200.     }
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. WarningLetterSet.java:
  209.  
  210. import java.util.*;
  211.  
  212. public class WarningLetterSet
  213. {
  214.     public static void main(String args[])
  215.     {
  216.         // Create the lists of failing students
  217.         String[] French = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
  218.  
  219.         String[] Algebra = {"Alan", "Amy", "Jeremy", "Helen", "Alexi"};
  220.  
  221.         String[] History = {"Adel", "Aaron", "Amy", "James", "Alice"};
  222.  
  223.         // Create the set to hold the failing students
  224.         Set LettersHome = new HashSet();
  225.  
  226.         //Add the failing students from each class to the set
  227.         for (int i=0; i< French.length; i++)
  228.             LettersHome.add(French[i]);
  229.  
  230.         for (int j=0; j< Algebra.length; j++)
  231.             LettersHome.add(Algebra[j]);
  232.  
  233.         for (int k=0; k< History.length; k++)
  234.             LettersHome.add(History[k]);
  235.  
  236.         //Print out the number of letters to be sent and the recipient list
  237.         System.out.println(LettersHome.size()+" letters must be sent to: "+LettersHome);
  238.  
  239.     }//Main
  240. }//Warning Letters
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. WarningLetterList.java:
  249.  
  250. import java.util.*;
  251.  
  252. public class WarningLetterList
  253. {
  254.     public static void main(String args[])
  255.     {
  256.         // Create the lists of failing students
  257.         String[] French = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
  258.  
  259.         String[] Algebra = {"Alan", "Amy", "Jeremy", "Helen", "Alexi"};
  260.  
  261.         String[] History = {"Adel", "Aaron", "Amy", "James", "Alice"};
  262.  
  263.         // Create the set to hold the failing students
  264.         List LettersHome = new ArrayList();
  265.  
  266.         //Add the failing students from each class to the set
  267.         for (int i=0; i< French.length; i++)
  268.             LettersHome.add(French[i]);
  269.  
  270.         for (int j=0; j< Algebra.length; j++)
  271.             LettersHome.add(Algebra[j]);
  272.  
  273.         for (int k=0; k< History.length; k++)
  274.             LettersHome.add(History[k]);
  275.  
  276.         Collections.sort(LettersHome);
  277.  
  278.         //Print out the number of letters to be sent 
  279. // and the recipient list
  280. System.out.println(LettersHome.size()+" letters must be sent to: "+LettersHome);
  281.  
  282.     }//Main
  283. }//Warning Letters
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292. StudentIDMap.java:
  293.  
  294. import java.util.*;
  295.  
  296. public class StudentIDMap
  297. {
  298.     public static void main(String args[])
  299.     {
  300.         // Create the lists of failing students
  301.         String[] SNames = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
  302.         String[] ID = {"123456789", "259879864", "988456997", "984876453", "9768696"};
  303.  
  304.         // Create the set to hold the failing students
  305.         Map IDMap = new HashMap();
  306.  
  307.         //Add the failing students from each class to the set
  308.         for (int i=0; i< SNames.length; i++)
  309.             IDMap.put(ID[i],SNames[i]);
  310.  
  311.  
  312.         //Print out the student list
  313.         System.out.println(IDMap.size()+" Students entered: ");
  314.         System.out.println(IDMap);
  315.  
  316.     }//Main
  317. }//StudentIDMap
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325. PrintGraphics.java:
  326.  
  327. import java.awt.*;
  328. import java.awt.event.*;
  329.  
  330. public class PrintGraphics extends Frame implements ActionListener
  331. {
  332.     PrintCanvas canvas1;
  333.  
  334.     public PrintGraphics()
  335.     {
  336.         super("PrintGraphics");
  337.         canvas1 = new PrintCanvas();
  338.         add("Center", canvas1);
  339.  
  340.         Button b = new Button( "Print");
  341.         b.setActionCommand("print");
  342.         b.addActionListener(this);
  343.         add("South",b);
  344.  
  345.         pack();
  346.  
  347.     }//constructor
  348.  
  349.     public void actionPerformed(ActionEvent e)
  350.     {
  351.         String cmd = e.getActionCommand();
  352.         if (cmd.equals("print"))
  353.         {
  354.             PrintJob pjob = getToolkit().getPrintJob(this, "PrintGraphics", null);
  355. if (pjob != null)
  356.             {
  357.                 Graphics pg = pjob.getGraphics();
  358.  
  359.                 if (pg != null)
  360.                 {
  361.                     canvas1.printAll(pg);
  362.                     pg.dispose();
  363.                 }//if
  364.                 pjob.end();
  365.             }//if
  366.  
  367.         }//if
  368.  
  369.     }//actionPerformed
  370.  
  371.     public static void main(String args[])
  372.     {
  373.         PrintGraphics test = new PrintGraphics();
  374.         test.addWindowListener(new WindowCloser());
  375.         test.show();
  376.  
  377.     }
  378. }//class PrintGraphics
  379.  
  380. class PrintCanvas extends Canvas
  381. {
  382.     public Dimension getPreferredSize()
  383.     {
  384.         return new Dimension(200,200);
  385.     }
  386.  
  387.     public void paint(Graphics g)
  388.     {
  389.         Rectangle r = getBounds();
  390.  
  391.         g.setColor(Color.yellow);
  392.         g.fillRect(0,0, r.width, r.height);
  393.  
  394.         g.setColor(Color.blue);
  395.         g.drawString("Hello, World", 100, 100);
  396.  
  397.         g.setColor(Color.red);
  398.         g.drawLine(0,100,100,0);
  399.         g.fillOval(135,140,15,15);
  400.     }
  401.  
  402. }//class PrintCanvas
  403.  
  404. class WindowCloser extends WindowAdapter
  405. {
  406.     public void windowClosing(WindowEvent e)
  407.     {
  408.         Window win = e.getWindow();
  409.         win.setVisible(false);
  410.         win.dispose();
  411.         System.exit(0);
  412.     }//windowClosing
  413. }//class WindowCloser
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421. TwoDArcs2.java:
  422.  
  423. import java.awt.*;
  424. import java.awt.event.*;
  425. import java.awt.geom.*;
  426. import java.awt.image.BufferedImage;
  427.  
  428. public class TwoDArcs2 extends Canvas {
  429.  
  430.     private int w, h, x, y;
  431.     //Declare the Arc objects
  432.     private Arc2D.Float arc1 = new Arc2D.Float(Arc2D.CHORD);
  433.     private Arc2D.Float arc2 = new Arc2D.Float(Arc2D.OPEN);
  434.     private Arc2D.Float arc3 = new Arc2D.Float(Arc2D.PIE);
  435.  
  436.     //Buffering support
  437.     private BufferedImage offImg; 
  438.  
  439.  
  440.     public TwoDArcs2() {
  441.         setBackground(Color.white);
  442.     }
  443.  
  444.  
  445.     public void drawArcs(Graphics2D g2)
  446.     {
  447.  
  448.     //Set the width of the outline
  449.  
  450.       g2.setStroke(new BasicStroke(5.0f));
  451.  
  452.  
  453.      //Draw the chord
  454.       arc1.setFrame(140,30, 67, 46);
  455.       arc1.setAngleStart(45);
  456.       arc1.setAngleExtent(270);
  457.       g2.setColor(Color.blue);
  458.       g2.draw(arc1);
  459.       g2.setColor(Color.gray);
  460.       g2.fill(arc1);
  461.       g2.setColor(Color.black);
  462.       g2.drawString("Arc2D.CHORD", 140, 20);
  463.  
  464. //Draw the open arc
  465.       arc2.setFrame(140,100, 67, 46);
  466.       arc2.setAngleStart(45);
  467.       arc2.setAngleExtent(270);
  468.       g2.setColor(Color.gray);
  469.       g2.draw(arc2);
  470.       g2.setColor(Color.green);
  471.       g2.fill(arc2);
  472.       g2.setColor(Color.black);
  473.       g2.drawString("Arc2D.OPEN", 140, 90);
  474.  
  475. //Draw the pie chart
  476.       arc3.setFrame(140, 200, 67, 46);
  477.       arc3.setAngleStart(45);
  478.       arc3.setAngleExtent(270);
  479.       g2.setColor(Color.gray);
  480.       g2.draw(arc3);
  481.       g2.setColor(Color.red);
  482.       g2.fill(arc3);
  483.       g2.setColor(Color.black);
  484.       g2.drawString("Arc2D.PIE",140, 190); 
  485.    }
  486.  
  487.  
  488.     public Graphics2D createDemoGraphics2D(Graphics g) {
  489.         Graphics2D g2 = null;
  490.  
  491.         if (offImg == null || offImg.getWidth() != w ||
  492.                         offImg.getHeight() != h) {
  493.             offImg = (BufferedImage) createImage(w, h);
  494. }
  495.  
  496.         if (offImg != null) {
  497.             g2 = offImg.createGraphics();
  498.             g2.setBackground(getBackground());
  499.         }
  500.  
  501.         // .. set attributes ..
  502.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  503.                             RenderingHints.VALUE_ANTIALIAS_ON);
  504.  
  505.         // .. clear canvas ..
  506.         g2.clearRect(0, 0, w, h);
  507.  
  508.         return g2;
  509.     }
  510.  
  511.  
  512.     public void paint(Graphics g) {
  513.  
  514.         Dimension d = getSize();
  515.         w = d.width;
  516.         h = d.height;
  517.         //System.out.println(w * 2);
  518.         //System.out.println(h);
  519.  
  520.  
  521.         if (w <= 0 || h <= 0)
  522.             return;
  523.  
  524.         Graphics2D g2 = createDemoGraphics2D(g);
  525.         drawArcs(g2);
  526.         g2.dispose();
  527.  
  528.         if (offImg != null && isShowing())  {
  529.             g.drawImage(offImg, 0, 0, this);
  530.         }
  531.  
  532. }
  533.  
  534.     public static void main(String argv[]) {
  535.         final TwoDArcs2 demo = new TwoDArcs2();
  536.         WindowListener l = new WindowAdapter()
  537.         {
  538.             public void windowClosing(WindowEvent e){System.exit(0);}
  539. };
  540.         Frame f = new Frame("Java2D Arcs");
  541.         f.addWindowListener(l);
  542.         f.add("Center", demo);
  543.         f.pack();
  544.         f.setSize(400,300);
  545.         f.show();
  546.     }
  547. }
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.