java JAI图像一屏显示

来源:百度知道 编辑:UC知道 时间:2024/05/11 20:03:28
我新建了一个JFrame,大小为(500,500),但现在有一个定义好的PlanarImage(JAI类的图片)(600,600),如何可以使图片刚好填充容器,并且随着容器大小改变图片仍然刚好一屏显示?求高手赐教,如果附代码最好,谢谢!!!

class DrawImageTest {

static BufferedImage iamge = getImage();
static int width, height;

public static void main(String[] args) {

JFrame frame = new JFrame();
final JPanel panel = new JPanel() {

@Override
public void paint(Graphics g) {
g.drawImage(iamge, 0, 0, width, height, 0, 0, iamge.getWidth(), iamge.getHeight(), this);
}
};
frame.add(panel);
frame.addComponentListener(new ComponentAdapter() {

@Override
public void componentResized(ComponentEvent e) {
width = panel.getWidth();
height = panel.getHeight();
panel.repaint();
}
});
frame.setSize(200, 300);
frame.setVisible(true);
}

public static BufferedImage getImage() {
BufferedImage image = // Create an off-screen image
new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB);
Graphics2D ig = image.createGraphics(); // Get its Graphics for draw