I wrote an image canvas class which can be automatically re-size image so that it fits the window. The code itself is pretty simple. package com.dukesoftware.utils.swing.others; import java.awt.Graphics; import java.awt.Image; import javax.swing.JPanel; public class AutoResizeImageCanvas extends JPanel{ private Image img; public void setImage(Image img){ this.img = img; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); final int panelWidth = getWidth(); final int panelHeight = getHeight(); g.fillRect(0, 0, panelWidth, panelHeight); if(img != null){ final int imgWidth = img.getWidth(null); final int imgHeight = img.getHeight(null); final double rW = (double)panelWidth / imgWidth; final double rH = (double)panelHeight / imgHeight; int newWidth; int newHeight; if(rW < rH){
IT関連の技術やプログラミングを中心に記事を書いています。ハードウェアも好きなので、日々のちょっとしたお役立ち情報も投稿しています。