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

发布于 2024-07-16 05:16:58 字数 429 浏览 4 评论 0原文

我之前发布过关于面板非常混乱的文章,但是,我通过简单地更改布局来解决这个问题(感谢查理·马丁帮助我)。 现在,我尝试将图像添加到面板中,以便我可以将该面板添加到框架中。 这是我正在搞乱的课程的一部分。

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

这是我运行程序并点击开始游戏(在启动弹出窗口上)时出现的情况。 screenshot

本质上,主窗口上应该有一个图像以及按钮,但我不确定如何我会着手实施这一点。

I posted earlier about having a really messed up panel but, I fixed that by simply changing the layout (thank you to Charlie Martin for helping me). Now, I'm trying to add an image to a panel, so I can add that panel to the frame. This is part of the class that I am messing around with.

http://friendpaste.com/13zibFC4oVxCbm83500KVj
(dead link)

This is what comes up when I run the program and hit start game (on the startup popup)..
screenshot

Essentially, there is supposed to be an image on the main window along with the buttons and I'm not exactly sure how I would go about implementing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷离° 2024-07-23 05:16:58

我可能只是在 JLabel 上设置 ImageIcon 并将 JLabel 添加到面板中所需的位置。

I'd probably just set an ImageIcon on a JLabel and add the JLabel where you want in the panel.

心碎的声音 2024-07-23 05:16:58

拜托,拜托,拜托,不要使用 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;
    }
}

Please, please, please -don't- use the JLabel. While that -is- the easiest way, it's also bad practice and causes problems when you have images that aren't the actual size you want displayed.

The second answer is on the right path, but still has the same problem.

Here's a class I've written in the past which is what you need:

It will also allow you to set the specific size if you want; it will scale the image to best fit the panel, and align the image as well.

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文