Java drawImage() 方法无论图像大小如何,都会将图像绘制得很小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19331269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drawImage() method draws the image small regardless of image size
提问by FartVader
I have an imagepanel
class that draws an image on a JPanel. My problem is that the image appears to be very small inside the jpanel
, and I dont know why.
我有一个imagepanel
在 JPanel 上绘制图像的类。我的问题是图像在里面看起来很小jpanel
,我不知道为什么。
I have done all I could and searched the net and even some java books for this little problem but to no success. I really need some help on this one.
我已经尽我所能,并在网上甚至一些Java书籍中搜索了这个小问题,但没有成功。我真的需要一些帮助。
I am extremely new to java.
我对java非常陌生。
class Weapons extends JPanel {
private Image weaponimage = weapon1.getImage();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null)
g.drawImage(weaponimage, 0, 0, getWidth(), getHeight(), this);
}
}
that's the image class.
那是图像类。
采纳答案by Andrew Thompson
The 'instant fix' is to paint that element at its natural size. E.G. Change this:
“即时修复”是以其自然大小绘制该元素。EG 改变这个:
g.drawImage(weaponimage, 0, 0, getWidth(), getHeight(), this);
To this:
对此:
g.drawImage(weaponimage, 0, 0, this);
Which seems logical. A 'weapon' should probably be drawn at natural size.
这似乎合乎逻辑。“武器”可能应该以自然尺寸绘制。
..problem is that the image appears to be very small inside the jpanel,
..问题是图像在 jpanel 内看起来很小,
No. The problem seems to be that the panel itself is very small. The image is painted the width and height of the panel as assigned by the layout.
不。问题似乎是面板本身非常小。图像按照布局指定的面板宽度和高度绘制。
To increase the size of the (BG) image, add components to the panel (properly laid out) and display the panel at its preferred size.
要增加 (BG) 图像的大小,请将组件添加到面板(正确布局)并以其首选大小显示面板。
回答by nullptr
May be your class look like this:
可能你的班级是这样的:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
class Weapons extends JPanel {
// get image according to your application logic
BufferedImage image;
@Override
public void paint(Graphics g){
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth() - 1, getHeight() - 1, 0, 0, image.getWidth() - 1, image.getHeight() - 1, null);
}
public void setImage(BufferedImage image){
this.image = image;
}
}
class Main {
Weapons weapons;
public void updateWeapons(){
// create image
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR);
// draw on image
// TO UPDATE WEAPONS GUI, below steps are impotant
weapons.setImage(image);
weapons.repaint();
}
}
You have to override paint() method. Then you can set image and call repaint() to update graphics on panel.
您必须覆盖paint() 方法。然后您可以设置图像并调用 repaint() 来更新面板上的图形。
回答by A. I. Abana
use a dimension object to change the size of the image
使用维度对象来改变图像的大小
Weapons picture = new Weapons();
picture.setPreferredSize(new Dimension(110, 160));
panel.add(picture);
do this and you can stretch the length and breadth of your photo as needed.
这样做,您可以根据需要拉伸照片的长度和宽度。