【发布时间】: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