pnviewer3.java
来自「java 完全探索的随书源码」· Java 代码 · 共 175 行
JAVA
175 行
// PNViewer3.java
// Planetary Nebulae Viewer
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PNViewer3 extends Applet 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 void init ()
{
// Create a MediaTracker object to track image loading.
MediaTracker mt = new MediaTracker (this);
for (int i = 0; i < im.length; i++)
{
// Get the next image.
im [i] = getImage (getDocumentBase (), 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 applet background has a lightgray appearance.
setBackground (Color.lightGray);
// By default, AWT applets use a flow layout manager.
setLayout (new BorderLayout ());
// Create picture component.
pic = new Picture ();
// Draw first nebula image.
pic.draw (im [0]);
// Add picture component to North region of applet 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 applet 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 applet container.
// At this point, Label/Choice will be visible on screen.
add ("South", p);
}
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 ()]);
}
}
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.
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 + =
减小字号Ctrl + -
显示快捷键?