使用Java AWT将图像添加到面板

时间:2009-03-29 21:52:47

标签: java image awt frames panels

我之前发过关于有一个非常混乱的面板但是,我通过简单地改变布局来修复它(感谢Charlie Martin帮助我)。现在,我正在尝试将图像添加到面板,因此我可以将该面板添加到框架中。这是我正在搞乱的课程的一部分。

<击> http://friendpaste.com/13zibFC4oVxCbm83500KVj (死链接)

当我运行程序并点击开始游戏(在启动弹出窗口中)时,会出现这种情况。 screenshot

基本上,主窗口上应该有一个图像和按钮,我不确定如何实现它。

2 个答案:

答案 0 :(得分:2)

我可能只是在JLabel上设置了一个ImageIcon,并在面板中添加了你想要的JLabel。

答案 1 :(得分:0)

拜托,请 - 请不要 - 使用JLabel。虽然这是最简单的方法,但这也是不好的做法,如果您的图像不是您想要显示的实际尺寸,则会导致问题。

第二个答案是在正确的道路上,但仍然有同样的问题。

这是我过去写过的课程,这是你需要的课程:

它还允许您根据需要设置特定尺寸;它会缩放图像以最适合面板,并对齐图像。

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.SwingConstants;

public class ImagePanel extends JComponent implements SwingConstants, Printable {

    private Image image;
    private int verticalAlignment = CENTER;
    private int horizontalAlignment = CENTER;

    public ImagePanel() {}

    public ImagePanel(Image image) {
        setImage(image);
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
        repaint();
    }

    public void setImage(String file) {
        setImage(new ImageIcon(file).getImage());
    }

    public void setImage(File file) {
        setImage(new ImageIcon(file.getAbsolutePath()).getImage());
    }

    public void setImage(byte[] imageData) {
        setImage(imageData==null ? null : new ImageIcon(imageData).getImage());
    }

    public int getVerticalAlignment() {
        return verticalAlignment;
    }

    /**
     * @beaninfo
     *        bound: true
     *         enum: TOP    SwingConstants.TOP
     *               CENTER SwingConstants.CENTER
     *               BOTTOM SwingConstants.BOTTOM
     *    attribute: visualUpdate true
     *  description: The alignment of the image along the Y axis.  
     */
    public void setVerticalAlignment(int verticalAlignment) {
        if( (verticalAlignment==TOP) || (verticalAlignment==CENTER) || (verticalAlignment==BOTTOM) )
            this.verticalAlignment = verticalAlignment;
        else
            throw new IllegalArgumentException("Invalid Vertical Alignment: " + verticalAlignment);
    }

    public int getHorizontalAlignment() {
        return horizontalAlignment;
    }

    /**
     * @beaninfo
     *        bound: true
     *         enum: LEFT    SwingConstants.LEFT
     *               CENTER SwingConstants.CENTER
     *               RIGHT SwingConstants.RIGHT
     *    attribute: visualUpdate true
     *  description: The alignment of the image along the X axis.  
     */
    public void setHorizontalAlignment(int horizontalAlignment) {
        if( (horizontalAlignment==LEFT) || (horizontalAlignment==CENTER) || (horizontalAlignment==RIGHT) )
            this.horizontalAlignment = horizontalAlignment;
        else
            throw new IllegalArgumentException("Invalid Horizontal Alignment: " + horizontalAlignment);
    }

    @Override
    public Dimension getPreferredSize() {
        if(image == null)
            return super.getPreferredSize();
        else
            return new Dimension(image.getWidth(this), image.getHeight(this));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(image==null)
            return;

        Insets insets = getInsets();
        int x = insets.left;
        int y = insets.top;

        int w = getWidth() - insets.left - insets.right;
        int h = getHeight() - insets.top - insets.bottom;

        int src_w = image.getWidth(null);
        int src_h = image.getHeight(null);

        double scale_x = ((double)w)/src_w;
        double scale_y = ((double)h)/src_h;

        double scale = Math.min(scale_x, scale_y);

        int dst_w = (int)(scale * src_w);
        int dst_h = (int)(scale * src_h);

        int dx = x + (w-dst_w)/2;
        if(horizontalAlignment==LEFT)
            dx = x;
        else if(horizontalAlignment==RIGHT)
            dx = x + w - dst_w; 

        int dy = y + (h-dst_h)/2;
        if(verticalAlignment==TOP)
            dy = y;
        else if(verticalAlignment==BOTTOM)
            dy = y + h - dst_h; 

        g.drawImage(image, dx, dy, dx+dst_w, dy+dst_h, 0, 0, src_w, src_h, null);
    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

        if(pageIndex>0 || image==null)
            return NO_SUCH_PAGE;

        double w = pageFormat.getImageableWidth();
        double h = pageFormat.getImageableHeight();

        int src_w = image.getWidth(null);
        int src_h = image.getHeight(null);

        double scale_x = w/src_w;
        double scale_y = h/src_h;

        double scale = Math.min(scale_x, scale_y);

        int dst_w = (int)(scale * src_w);
        int dst_h = (int)(scale * src_h);

        int dx = (int)((w-dst_w)/2);

        int dy = (int)((h-dst_h)/2);

        graphics.drawImage(image, dx, dy, dx+dst_w, dy+dst_h, 0, 0, src_w, src_h, null);

        return PAGE_EXISTS;
    }
}