displayimage.java

来自「Java Classic Examples是我买的两本书:《JAVA经典实例》和」· Java 代码 · 共 45 行

JAVA
45
字号
import java.awt.*;
import javax.swing.*;
import java.net.*;

public class DisplayImage extends JApplet
{
  public void init()
  {
    ImageIcon icon = null;
    try
    {
      icon = new ImageIcon(new URL(getCodeBase(),"Images/wrox_logo.gif"));
    }
    catch(MalformedURLException e)
    {
      System.out.println("Failed to create URL:\n" + e);
    }

    int imageWidth = icon.getIconWidth();           // Get icon width
    int imageHeight = icon.getIconHeight();         // and its height
    resize(imageWidth,imageHeight);                 // Set applet size to fit the image

    // Create panel a showing the image
    ImagePanel imagePanel = new ImagePanel(icon.getImage());

    getContentPane().add(imagePanel);               // Add the panel to the content pane
  }

   // Class representing a panel displaying an image
   class ImagePanel extends JPanel
   {
     public ImagePanel(Image image)
     {
      this.image = image;
     }

     public void paint(Graphics g)
     {
       g.drawImage(image, 0, 0, this);                        // Display the image
     }

     Image image;                                             // The image
   }
}

⌨️ 快捷键说明

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