⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pnviewer1.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
// PNViewer1.java

// Planetary Nebulae Viewer

import java.awt.*;
import java.awt.event.*;

public class PNViewer1 extends Frame implements ItemListener
{
   private Choice c;

   private String [] imageFileNames =
   {
      "mycn18.jpg",
      "ngc2022.jpg",
      "ngc2867.jpg",
      "ngc3918.jpg",
      "ngc5307.jpg",
      "ngc6369.jpg",
      "ngc7009.jpg",
      "ngc7027.jpg"
   };

   private String [] imageTitleNames =
   {
      "Hourglass Nebula",
      "NGC 2022",
      "NGC 2867",
      "NGC 3918",
      "NGC 5307",
      "NGC 6369",
      "NGC 7009",
      "NGC 7027"
   };

   private Image [] im = new Image [imageFileNames.length];

   private Picture pic;

   public PNViewer1 (String title)
   {
      // Pass the title to the superclass so it will appear in the
      // frame window.

      super (title);

      // Register a listener to respond to window closing events.

      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                         });

      // Create a MediaTracker object to track image loading.

      MediaTracker mt = new MediaTracker (this);

      // Applications require a Toolkit for image loading.

      Toolkit tk = Toolkit.getDefaultToolkit ();

      for (int i = 0; i < im.length; i++)
      {
           // Get the next image.

           im [i] = tk.getImage (imageFileNames [i]);

           // Add image to MediaTracker object.

           mt.addImage (im [i], 0);
      }

      // Force all images to load.

      try
      {
           mt.waitForID (0);
      }
      catch (InterruptedException e) {}

      // Ensure that application frame window background has a
      // lightgray appearance.

      setBackground (Color.lightGray);

      // Create picture component.

      pic = new Picture ();

      // Draw first nebula image.

      pic.draw (im [0]);

      // Add picture component to North region of frame container.
      // At this point, picture component will be visible on screen.

      add ("North", pic);

      // Create a Panel for holding Label and Choice components.

      Panel p = new Panel ();

      Label l = new Label ("Planetary Nebula:");

      // Ensure that Label background has a lightgray appearance.

      l.setBackground (Color.lightGray);

      // Add Label component to Panel.

      p.add (l);

      // Create a Choice component (a combobox) for holding textual
      // items.

      c = new Choice ();

      // When the user selects an item, an item state changed event
      // will be fired.  Register the current frame window object to
      // listen for these events, so the image can be automatically
      // updated.

      c.addItemListener (this);

      // Add nebulae names to Choice component.

      for (int i = 0; i < imageTitleNames.length; i++)
           c.add (imageTitleNames [i]);

      // Add Choice component to Panel.

      p.add (c);

      // Add Panel component to South region of frame window
      // container. At this point, Label/Choice will be visible on
      // screen.

      add ("South", p);

      // Size this frame window to fit the preferred size and layouts
      // of its components.

      pack ();

      // Do not allow users to resize the window.

      setResizable (false);

      // Ensure that window is visible.

      setVisible (true);
   }

   public void itemStateChanged (ItemEvent e)
   {
      // When item state changed event is fired, the code in the
      // itemStateChanged () method is called.  In turn, this code
      // will tell the picture component to draw the image that is
      // associated with the Choice component's current index.

      pic.draw (im [c.getSelectedIndex ()]);
   }

   public static void main (String [] args)
   {
      new PNViewer1 ("Planetary Nebulae Viewer");
   }
}

class Picture extends Canvas
{
   private Image image;     // The current image.

   private final Dimension size = new Dimension (420, 415);

   public void draw (Image image)
   {
      this.image = image;           // Save the current image.
      repaint ();                   // Draw the image.
   }

   public Dimension getPreferredSize ()
   {
      // When the layout manager calls picture's getPreferredSize ()
      // method, this method will return a Dimension object that
      // tells the layout manager how much room to reserve for the
      // picture component.  Because images range in size up to 400 x
      // 400 pixels, all picture components would request a size of
      // 420 x 415 (to accomodate a border).

      return size;
   }

   public void paint (Graphics g)
   {
      // Determine the upper-left image corner (x, y) coordinates so
      // that the image will be centered.

      int x = (getSize ().width - image.getWidth (this)) / 2;
      int y = (getSize ().height - image.getHeight (this)) / 2;

      // Draw the image.

      g.drawImage (image, x, y, this);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -