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

📄 picture.java

📁 a Big Java source Code Exemples
💻 JAVA
字号:
// Updates: 2004.07.16


import java.awt.*;
import javax.swing.*;


/**
 * Displays a centered image in the available area. If the picture is too big
 * (width or height), a zoom will be applied in order to show the full image.
 * This class works also if the picture is in a jar file.
 * @author Michel Deriaz 
 */
public class Picture extends JPanel {
  private Image img;      


  /**
   * Loads the specified image, which must be a JPG, a GIF, or a PNG.
   * @param file the file to load
   */  
  public Picture(String file) {
    img = new ImageIcon(getClass().getResource(file)).getImage();
    repaint(); 
  }
  

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (img == null) return;
    int w = img.getWidth(this);  
    int h = img.getHeight(this);
    boolean zoom = (w > getWidth() || h > getHeight());
    if (zoom) g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    else g.drawImage(img, (getWidth()-w)/2, (getHeight()-h)/2, this);
  }
}

⌨️ 快捷键说明

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