【问题标题】:Resizing Image Not working调整图像大小不起作用
【发布时间】:2015-01-14 00:58:23
【问题描述】:

我制作了一个 Button 类,它允许我拥有按钮(有点明显)。但在我的按钮类中,我使用图像在屏幕上显示按钮。我得到了它的工作,但我想将图像调整为按钮的大小。

我的“Image Resizer”工作完美,但是当我尝试调整按钮大小时,按钮不显示。我没有收到任何错误。

这是我的 Button 类:

private String text;
private int size = 0;
private BufferedImage buttonHD;

public Button(int x, int y, int width, int height, int size) {
    super(x, y, width, height);
    this.size = size;
    buttonHD = Renderer.resizeImage(Images.button, x, y, width, height);
}

public Button setText(String text) {
    this.text = text;
    return this;
}

public void drawButton(Graphics g, int xoffset, int yoffset) {
    int xx = x + xoffset;
    int yy = y + yoffset;

    if(!MouseInput.MOUSE.intersects(this)) {
        g.drawImage(buttonHD, x, y, width, height, null);
    } else if(MouseInput.MOUSE.intersects(this)){
        g.setColor(Color.DARK_GRAY);
        g.fillRect(x, y, width, height);
    }

    Renderer.drawText(text, g, xoffset, yoffset, size);//Draws button text
}

我正在调整大小的原始图像存储在我的 Images 类中:

public static BufferedImage button;

这是我的“按钮调整大小”方法:

public static BufferedImage resizeImage(BufferedImage origImg, int x, int y, int initWidth, int initHeight) {
    BufferedImage resizedImg = new BufferedImage(initWidth, initHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = resizedImg.createGraphics();
    g2d.drawImage(origImg, x, y, initWidth, initHeight, null);
    g2d.dispose();
    return resizedImg;
}

我使用这些按钮的方式是在ScreenState 类中。每个类表示为每个状态。按钮设置在那里,并由类的构造函数加载。

按钮确实可以正常工作,但图像却不显示。如果需要更多代码,请告诉我,我会为您提供。

我一直在尝试解决这个问题,但没有成功。如果有人可以提示我的问题在哪里或者有解决方案,那就太好了。谢谢!

【问题讨论】:

    标签: java image-resizing


    【解决方案1】:

    此函数将BufferedImage 的大小调整为给定的widthheight

    public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
        // calculate the scale factor
        double xScale = width / (double) image.getWidth();
        double yScale = height / (double) image.getHeight();
        // create the object that will contain the resized image
        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        // resize the image
        Graphics2D g = (Graphics2D) resizedImage.getGraphics();
        g.scale(xScale, yScale);
        g.drawImage(image, 0, 0, null);
        g.dispose();
        // return the resized image
        return resizedImage;
    }
    

    然后简单地使用它:

    public class MyButton extends JButton
    {
        private BufferedImage image;
    
    
        public MyButton() {
            image = resizeImage(ImageIO.read(IMAGE_PATH), BUTTON_WIDTH, BUTTON_HEIGHT);
        }
    
    
        @Override protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }
    }
    

    IMAGE_PATH 是您的图片所在的FileBUTTON_WIDTHBUTTON_HEIGHT 是您的按钮尺寸。

    【讨论】:

    • 对不起,我忘了提到我使用的是矩形(按钮类扩展矩形)作为按钮而不是 JButton。还在一个单独的类中使用 MouseAdapter(我检查按钮是否被点击)。
    • 如果鼠标悬停在按钮上,您想要一个充满图像(调整大小的图像)的按钮,而当鼠标退出按钮本身时,您想要一个深灰色按钮?
    猜你喜欢
    • 2014-05-13
    • 2014-08-29
    • 2014-05-12
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多