home *** CD-ROM | disk | FTP | other *** search
- Copy the file GetInetFile.class from the CD-ROM (in the directory for material from Chapter 11)
- to your directory.
-
-
-
-
-
- Lines.java:
-
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * class LineColors holds 24 color values
- */
- class LineColors {
-
- /**
- * color[] array holds the colors to be used
- */
- Color color[];
-
- /**
- * class constructor
- * initializes the color array using an arbitrary algorithm
- */
- public LineColors () {
-
- color = new Color[24];
- int i, rgb;
-
- rgb = 0xff;
- for (i=0; i<24; i+=1) {
- color[i] = new Color (rgb);
- rgb <<= 1;
- if ((rgb & 0x1000000) != 0) {
- rgb |= 1;
- rgb &= 0xffffff;
- }
- }
- }
- }
-
- /**
- * class describing one line segment
- */
- class Segment {
-
- /*
- * x1, y1 - starting coordinates for this segment
- * x2, y2 - ending coordinates for this segment
- * dx1,...dy2 - velocities for the endpoints
- * whichcolor - the current index into color array
- * width, height - width and height of bounding panel
- * LC - instance of LineColors class
- */
- double x1, y1, x2, y2;
- double dx1, dy1, dx2, dy2;
- int whichcolor, width, height;
- LineColors LC;
-
- /**
- * class constructor
- * initialize endpoints and velocities to random values
- * @param w - width of bounding panel
- * @param h - height of bounding panel
- * @param c - starting color index
- * @param lc - instance of LineColors class
- */
- public Segment (int w, int h, int c, LineColors lc) {
-
- whichcolor = c;
- width = w;
- height = h;
- LC = lc;
- x1 = (double) w * Math.random ();
- y1 = (double) h * Math.random ();
- x2 = (double) w * Math.random ();
- y2 = (double) h * Math.random ();
-
- dx1 = 5 - 10 * Math.random ();
- dy1 = 5 - 10 * Math.random ();
- dx2 = 5 - 10 * Math.random ();
- dy2 = 5 - 10 * Math.random ();
- }
-
- /*
- * increment color index
- * calculate the next endpoint position for this segment
- */
- void compute () {
-
- whichcolor += 1;
- whichcolor %= 24;
-
- x1 += dx1;
- y1 += dy1;
- x2 += dx2;
- y2 += dy2;
-
- if (x1 < 0 || x1 > width) dx1 = -dx1;
- if (y1 < 0 || y1 > height) dy1 = -dy1;
- if (x2 < 0 || x2 > width) dx2 = -dx2;
- if (y2 < 0 || y2 > height) dy2 = -dy2;
- }
-
- /**
- * draw the line segment using the current color
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- g.setColor (LC.color [whichcolor]);
- g.drawLine ((int) x1, (int) y1, (int) x2, (int) y2);
- }
- }
-
- /**
- * The applet/application proper
- */
- public class Lines extends Applet {
-
- /*
- * Nlines - number of line segments to be displayed
- * lines - array of instances of Segment class
- * LC - instance of LineColors class
- */
- int width,height;
- final int NLines = 4;
- Segment lines[] = new Segment[NLines];
- LineColors LC = new LineColors ();
-
- /**
- * init is called when the applet is loaded
- * save the width and height
- * create instances of Segment class
- */
- public void init () {
-
- setLayout(null);
- width = 300;
- height = 300;
- setSize(width, height);
-
- int i;
- for (i=0; i<NLines; i+=1)
- lines[i] = new Segment (width, height, (2*i) % 24, LC);
- }
-
- /**
- * recompute the next endpoint coordinates for each line
- * invoke paint() method for each line
- * call repaint() to force painting 50ms later
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int i;
- for (i=0; i<NLines; i+=1) {
- lines[i].compute ();
- lines[i].paint (g);
- }
- repaint (50);
- }
-
-
-
-
-
-
-
-
- ReadProperties.java:
-
- import java.lang.*;
-
- class ReadProperties {
-
- public static void main(String[] args) {
-
- String str;
-
- try {
-
- str = System.getProperty("os.name", "not specified");
- System.out.println(" OS name: " + str);
-
- str = System.getProperty("java.version", "not specified");
- System.out.println(" JVM Version: " + str);
-
- str = System.getProperty("user.home", "not specified");
- System.out.println(" user.home: " + str);
-
- str = System.getProperty("java.home", "not specified");
- System.out.println(" java.home: " + str);
-
- } catch (Exception e) {
- System.err.println("Caught exception " + e.toString());
- }
- }
- }
-
-
-
-
-
-
- WarningLetterSet.java:
-
- import java.util.*;
-
- public class WarningLetterSet
- {
- public static void main(String args[])
- {
- // Create the lists of failing students
- String[] French = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
-
- String[] Algebra = {"Alan", "Amy", "Jeremy", "Helen", "Alexi"};
-
- String[] History = {"Adel", "Aaron", "Amy", "James", "Alice"};
-
- // Create the set to hold the failing students
- Set LettersHome = new HashSet();
-
- //Add the failing students from each class to the set
- for (int i=0; i< French.length; i++)
- LettersHome.add(French[i]);
-
- for (int j=0; j< Algebra.length; j++)
- LettersHome.add(Algebra[j]);
-
- for (int k=0; k< History.length; k++)
- LettersHome.add(History[k]);
-
- //Print out the number of letters to be sent and the recipient list
- System.out.println(LettersHome.size()+" letters must be sent to: "+LettersHome);
-
- }//Main
- }//Warning Letters
-
-
-
-
-
-
-
- WarningLetterList.java:
-
- import java.util.*;
-
- public class WarningLetterList
- {
- public static void main(String args[])
- {
- // Create the lists of failing students
- String[] French = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
-
- String[] Algebra = {"Alan", "Amy", "Jeremy", "Helen", "Alexi"};
-
- String[] History = {"Adel", "Aaron", "Amy", "James", "Alice"};
-
- // Create the set to hold the failing students
- List LettersHome = new ArrayList();
-
- //Add the failing students from each class to the set
- for (int i=0; i< French.length; i++)
- LettersHome.add(French[i]);
-
- for (int j=0; j< Algebra.length; j++)
- LettersHome.add(Algebra[j]);
-
- for (int k=0; k< History.length; k++)
- LettersHome.add(History[k]);
-
- Collections.sort(LettersHome);
-
- //Print out the number of letters to be sent
- // and the recipient list
- System.out.println(LettersHome.size()+" letters must be sent to: "+LettersHome);
-
- }//Main
- }//Warning Letters
-
-
-
-
-
-
-
-
- StudentIDMap.java:
-
- import java.util.*;
-
- public class StudentIDMap
- {
- public static void main(String args[])
- {
- // Create the lists of failing students
- String[] SNames = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
- String[] ID = {"123456789", "259879864", "988456997", "984876453", "9768696"};
-
- // Create the set to hold the failing students
- Map IDMap = new HashMap();
-
- //Add the failing students from each class to the set
- for (int i=0; i< SNames.length; i++)
- IDMap.put(ID[i],SNames[i]);
-
-
- //Print out the student list
- System.out.println(IDMap.size()+" Students entered: ");
- System.out.println(IDMap);
-
- }//Main
- }//StudentIDMap
-
-
-
-
-
-
-
- PrintGraphics.java:
-
- import java.awt.*;
- import java.awt.event.*;
-
- public class PrintGraphics extends Frame implements ActionListener
- {
- PrintCanvas canvas1;
-
- public PrintGraphics()
- {
- super("PrintGraphics");
- canvas1 = new PrintCanvas();
- add("Center", canvas1);
-
- Button b = new Button( "Print");
- b.setActionCommand("print");
- b.addActionListener(this);
- add("South",b);
-
- pack();
-
- }//constructor
-
- public void actionPerformed(ActionEvent e)
- {
- String cmd = e.getActionCommand();
- if (cmd.equals("print"))
- {
- PrintJob pjob = getToolkit().getPrintJob(this, "PrintGraphics", null);
- if (pjob != null)
- {
- Graphics pg = pjob.getGraphics();
-
- if (pg != null)
- {
- canvas1.printAll(pg);
- pg.dispose();
- }//if
- pjob.end();
- }//if
-
- }//if
-
- }//actionPerformed
-
- public static void main(String args[])
- {
- PrintGraphics test = new PrintGraphics();
- test.addWindowListener(new WindowCloser());
- test.show();
-
- }
- }//class PrintGraphics
-
- class PrintCanvas extends Canvas
- {
- public Dimension getPreferredSize()
- {
- return new Dimension(200,200);
- }
-
- public void paint(Graphics g)
- {
- Rectangle r = getBounds();
-
- g.setColor(Color.yellow);
- g.fillRect(0,0, r.width, r.height);
-
- g.setColor(Color.blue);
- g.drawString("Hello, World", 100, 100);
-
- g.setColor(Color.red);
- g.drawLine(0,100,100,0);
- g.fillOval(135,140,15,15);
- }
-
- }//class PrintCanvas
-
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }//windowClosing
- }//class WindowCloser
-
-
-
-
-
-
-
- TwoDArcs2.java:
-
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.*;
- import java.awt.image.BufferedImage;
-
- public class TwoDArcs2 extends Canvas {
-
- private int w, h, x, y;
- //Declare the Arc objects
- private Arc2D.Float arc1 = new Arc2D.Float(Arc2D.CHORD);
- private Arc2D.Float arc2 = new Arc2D.Float(Arc2D.OPEN);
- private Arc2D.Float arc3 = new Arc2D.Float(Arc2D.PIE);
-
- //Buffering support
- private BufferedImage offImg;
-
-
- public TwoDArcs2() {
- setBackground(Color.white);
- }
-
-
- public void drawArcs(Graphics2D g2)
- {
-
- //Set the width of the outline
-
- g2.setStroke(new BasicStroke(5.0f));
-
-
- //Draw the chord
- arc1.setFrame(140,30, 67, 46);
- arc1.setAngleStart(45);
- arc1.setAngleExtent(270);
- g2.setColor(Color.blue);
- g2.draw(arc1);
- g2.setColor(Color.gray);
- g2.fill(arc1);
- g2.setColor(Color.black);
- g2.drawString("Arc2D.CHORD", 140, 20);
-
- //Draw the open arc
- arc2.setFrame(140,100, 67, 46);
- arc2.setAngleStart(45);
- arc2.setAngleExtent(270);
- g2.setColor(Color.gray);
- g2.draw(arc2);
- g2.setColor(Color.green);
- g2.fill(arc2);
- g2.setColor(Color.black);
- g2.drawString("Arc2D.OPEN", 140, 90);
-
- //Draw the pie chart
- arc3.setFrame(140, 200, 67, 46);
- arc3.setAngleStart(45);
- arc3.setAngleExtent(270);
- g2.setColor(Color.gray);
- g2.draw(arc3);
- g2.setColor(Color.red);
- g2.fill(arc3);
- g2.setColor(Color.black);
- g2.drawString("Arc2D.PIE",140, 190);
- }
-
-
- public Graphics2D createDemoGraphics2D(Graphics g) {
- Graphics2D g2 = null;
-
- if (offImg == null || offImg.getWidth() != w ||
- offImg.getHeight() != h) {
- offImg = (BufferedImage) createImage(w, h);
- }
-
- if (offImg != null) {
- g2 = offImg.createGraphics();
- g2.setBackground(getBackground());
- }
-
- // .. set attributes ..
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
-
- // .. clear canvas ..
- g2.clearRect(0, 0, w, h);
-
- return g2;
- }
-
-
- public void paint(Graphics g) {
-
- Dimension d = getSize();
- w = d.width;
- h = d.height;
- //System.out.println(w * 2);
- //System.out.println(h);
-
-
- if (w <= 0 || h <= 0)
- return;
-
- Graphics2D g2 = createDemoGraphics2D(g);
- drawArcs(g2);
- g2.dispose();
-
- if (offImg != null && isShowing()) {
- g.drawImage(offImg, 0, 0, this);
- }
-
- }
-
- public static void main(String argv[]) {
- final TwoDArcs2 demo = new TwoDArcs2();
- WindowListener l = new WindowAdapter()
- {
- public void windowClosing(WindowEvent e){System.exit(0);}
- };
- Frame f = new Frame("Java2D Arcs");
- f.addWindowListener(l);
- f.add("Center", demo);
- f.pack();
- f.setSize(400,300);
- f.show();
- }
- }
-
-
-
-
-
-
-
-
-