home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jre / HelloWorld.java < prev    next >
Encoding:
Java Source  |  1997-08-17  |  5.2 KB  |  220 lines

  1. package jre.demo;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.*;
  5. import java.awt.event.*;
  6. import java.net.URL;
  7. import java.io.*;
  8.  
  9. public class HelloWorld extends Frame 
  10. implements ImageObserver, ActionListener, WindowListener {
  11.  
  12.     Button OKbutton = new Button("OK");
  13.     MenuItem about = new MenuItem("About...");
  14.     static final int HEIGHT = 500;
  15.     static final int WIDTH = 500;
  16.     boolean imageHosed = false;
  17.  
  18.     /* The about Dialog and OK button */
  19.     Button aboutOK;
  20.     Dialog aboutD;
  21.  
  22.     Image img;
  23.  
  24.     public void repaint() {
  25.     super.repaint();
  26.     showImage();
  27.     }
  28.  
  29.     public void repaint(long tm) {
  30.     super.repaint(tm);
  31.     showImage();
  32.     }
  33.  
  34.     public void repaint(int x, int y, int w, int h) {
  35.     super.repaint(x, y, w, h);
  36.     showImage();
  37.     }
  38.  
  39.     public void repaint(long tm, int x, int y, int w, int h) {
  40.     super.repaint(tm, x, y, w, h);
  41.     showImage();
  42.     }
  43.  
  44.     public void update(Graphics g) {
  45.     super.update(g);
  46.     showImage();
  47.     }
  48.  
  49.     public void paint(Graphics g) {
  50.     super.paint(g);
  51.     showImage();
  52.     }
  53.  
  54.     public /* */ HelloWorld(String title) {
  55.         super(title);
  56.     setLayout(new BorderLayout(10, 10));
  57.     //    setLayout(new FlowLayout());
  58.     setResizable(false);
  59.     setBackground(Color.darkGray);
  60.     OKbutton.addActionListener(this);
  61.     addWindowListener(this);
  62.     //    OKbutton.setSize(100, 50);
  63.     Panel p = new Panel();
  64.     p.add(OKbutton);
  65.     MenuBar mb = new MenuBar();
  66.     //    mb.setBackground(Color.lightGray);
  67.     Menu me = new Menu("Help");
  68.     //    me.setBackground(Color.lightGray);
  69.     //    about.setBackground(Color.lightGray);
  70.     me.add(about);
  71.     about.addActionListener(this);
  72.     mb.add(me);
  73.     setMenuBar(mb);
  74.     add(p, BorderLayout.SOUTH);
  75.     pack();
  76.     }
  77.  
  78.     static void p(String s) {
  79.     System.out.println(s);
  80.     }
  81.  
  82.     public boolean imageUpdate(Image img, int info,
  83.     int x, int y, int w, int h) {
  84.     Graphics g = getGraphics();
  85.     if (g != null) {
  86.         g.drawImage(img, 20, 20, WIDTH - 100, HEIGHT - 200, this);
  87.     }
  88.     return (info & (ALLBITS | ERROR | ABORT)) == 0;
  89.     }
  90.     
  91.     /** ActionListener method. */
  92.  
  93.     public void actionPerformed(ActionEvent e) {
  94.     Object src = e.getSource();
  95.     if (src == OKbutton) {
  96.         System.exit(0);
  97.     } else if (src == about) {
  98.         aboutD = new Dialog(this, "About", true);
  99.         aboutD.setSize(200, 200);
  100.         TextArea txt = new TextArea("", 30, 8,
  101.                    TextArea.SCROLLBARS_NONE);
  102.         txt.setBackground(Color.lightGray);
  103.         txt.setEditable(false);
  104.         txt.setText("Hello World, using the\n"+
  105.             "minimal Java\nRuntime Environment.\n"+
  106.             "\nv. 1.0\n\nDave Brown");
  107.         aboutD.add("Center", txt);
  108.         Panel p = new Panel();
  109.         p.setBackground(Color.lightGray);
  110.         aboutOK = new Button("OK");
  111.         p.add(aboutOK);
  112.         aboutOK.addActionListener(this);
  113.         //        aboutD.add(aboutOK);
  114.         aboutD.add("South", p);
  115.         Point po = getLocation();
  116.         aboutD.setLocation(po.x + WIDTH + 5, po.y);
  117.         aboutD.setVisible(true);
  118.     } else if (src == aboutOK && aboutOK != null) {
  119.         aboutD.dispose();
  120.         aboutD = null;
  121.         aboutOK = null;
  122.     }
  123.     }
  124.  
  125.     /** WindowListener methods */
  126.     
  127.     public void windowActivated(WindowEvent e) {
  128.     setVisible(true);
  129.     showImage();
  130.     }
  131.  
  132.     public void windowClosed(WindowEvent e) {}
  133.  
  134.     public void windowClosing(WindowEvent e) {}
  135.  
  136.     public void windowDeactivated(WindowEvent e) {
  137.     showImage();
  138.     }
  139.  
  140.     public void windowDeiconified(WindowEvent e) {
  141.     showImage();
  142.     }
  143.     
  144.     public void windowIconified(WindowEvent e) { }
  145.     
  146.     public void windowOpened(WindowEvent e) {}
  147.  
  148.     void loadImage() throws IOException {
  149.     URL imgURL;
  150.  
  151.     String jh = System.getProperty("java.home");
  152.     String file = jh + File.separator+"lib"+
  153.         File.separator+"smooch.gif";
  154.     // see if it's there
  155.     if (!(new File(file)).exists()) {
  156.         throw new FileNotFoundException(file);
  157.     }
  158.     imgURL = new URL("file", "", file);
  159.     ImageProducer prod = (ImageProducer)imgURL.getContent();
  160.     img = Toolkit.getDefaultToolkit().createImage(prod);
  161.     }
  162.  
  163.     void showErrorAndExit(Exception e) {
  164.     String jh = System.getProperty("java.home");
  165.     String file = jh + File.separator+"lib"+
  166.         File.separator+"smooch.gif";
  167.     TextArea txt = new TextArea("", 30, 20);
  168.     txt.setBackground(Color.lightGray);
  169.     txt.setEditable(false);
  170.     OutputStream os = new ByteArrayOutputStream();
  171.     PrintStream ps = new PrintStream(os);
  172.     e.printStackTrace(ps);
  173.     txt.setText("Can't load image from:\n"+
  174.             file+":\n\n"+os.toString());
  175.     add("Center", txt);
  176.     setTitle("ERROR");
  177.     }
  178.  
  179.     synchronized void showImage() {
  180.     if (imageHosed) {
  181.         return;
  182.     }
  183.     if (img == null) {
  184.         try {
  185.         loadImage();
  186.         } catch (IOException e) {
  187.         imageHosed = true;
  188.         System.err.println("can't load image: " + e);
  189.         e.printStackTrace();
  190.         showErrorAndExit(e);
  191.         return;
  192.         }
  193.     }
  194.  
  195.     Graphics g = getGraphics();
  196.     if (g == null) {
  197.         return;
  198.     }
  199.     g.drawImage(img, 20, 20, WIDTH - 100, HEIGHT - 200, this);
  200.     g.setColor(Color.white);
  201.     g.drawString("Hello World", 200, HEIGHT - 100);
  202.     }
  203.  
  204.     static {
  205.     System.loadLibrary("HelloWorld");
  206.     }
  207.  
  208.     /* native method, for purposes of demonstration */
  209.     private static native void nativeMethod(String s);
  210.     
  211.     public static void main(String[] a) throws IOException {
  212.     String title = (a.length == 0) ? "Howdy": a[0];
  213.     nativeMethod(title);
  214.         HelloWorld w = new HelloWorld(title);
  215.     w.setSize(WIDTH, HEIGHT);
  216.     w.setVisible(true);
  217.     }
  218. }
  219.       
  220.