图像绘制适用于JFrame,但不适用于JPanel [英] Image Drawing Works for JFrame, But Not JPanel
问题描述
我正在处理一些简单的应用程序,以熟悉Swing并遇到问题。
I'm working through some simple applications to get familiar with Swing and running into problems.
我正在尝试使用包含图像的框架(在面板)以及用于放大/缩小图像的按钮。
I'm attempting to have a frame containing an image (in a panel) along with buttons to zoom in/out from the image.
我已经能够使添加的图像工作正常(尽管有一些框架大小调整问题)但是,这是另一个故事),然而,当我调用相同的组件类将其添加到面板时,什么都没有出现。我希望你们中的一个能帮助解决这个问题。
I have been able to make a frame with the added image work fine (albeit with some frame sizing issues, but that is another story), however, when I call the same component class to add it to a panel, nothing appears. I'm hoping one of you can help shed light on the situation.
代码:
图像框架 - 正如图所示
Image Frame - Is working as shown
class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("Java Image Machine");
init();
pack();
}
public final void init(){
//ZoomPanel zoomPanel = new ZoomPanel();
//ImagePanel imagePanel = new ImagePanel();
ImageComponent component = new ImageComponent();
//this.add(zoomPanel, BorderLayout.CENTER);
this.add(component);
//this.add(imagePanel, BorderLayout.SOUTH);
}
}
然而,使用ImagePanel或同时添加ZoomPanel直接的ImageComponent调用,不:
However, using the ImagePanel or adding the ZoomPanel simultaneously with the direct ImageComponent call, does not:
class ImagePanel extends JPanel{
public ImagePanel(){
//setBorder(BorderFactory.createLineBorder(Color.black));
ImageComponent component = new ImageComponent();
add(component);
}
}
组件类:
class ImageComponent extends JComponent{
public ImageComponent(){
try{
image = ImageIO.read(new File("test1.bmp"));
}
catch ( IOException e ){
e.printStackTrace();
}
System.out.println("W: " + image.getWidth(this) + " H: " + image.getHeight(this));
}
public void paintComponent( Graphics g ){
super.paintComponent(g);
if (image == null)
return;
width = image.getWidth(this);
height = image.getHeight(this);
//System.out.println("Image should be painted");
g.drawImage(image, 0, 0, null);
}
private Image image;
public int width;
public int height;
}
推荐答案
它适用于我(我刚刚测试了 ImageComponent
类):
It works fine for me ( I just tested the ImageComponent
class):
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Test {
/**
* Default constructor Test.class
*/
public Test() {
initComponents();
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test test = new Test();
}
});
}
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add ImageComponent to JFrame instance
jFrame.add(new ImageComponent());
//pack frame (size JFrame to match preferred sizes of added components and set visible
jFrame.pack();
jFrame.setVisible(true);
}
}
class ImageComponent extends JComponent {
private Image image;
public int width;
public int height;
public ImageComponent() {
try {
image = ImageUtils.scaleImage(300, 300, ImageIO.read(new URL("http://harmful.cat-v.org/software/_java/java-evil-edition.png")));
//image = ImageIO.read(new URL("http://harmful.cat-v.org/software/_java/java-evil-edition.png"));//uses images scale
} catch (Exception e) {
e.printStackTrace();
}
//so we can set the JPanel preferred size to the image width and height
ImageIcon ii = new ImageIcon(image);
width = ii.getIconWidth();
height = ii.getIconHeight();
}
//so our panel is the same size as image
@Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null) {
return;
}
width = image.getWidth(this);
height = image.getHeight(this);
g.drawImage(image, 0, 0, null);
}
}
//class used for scaling images
class ImageUtils {
static Image scaleImage(int width, int height, BufferedImage filename) {
BufferedImage bi;
try {
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(filename, 0, 0, width, height, null);
} catch (Exception e) {
return null;
}
return bi;
}
}
问题可能是您文件的路径,或者事实上 JPanel
宽度和高度将与图片不同,因此我们覆盖 getPrefferedSize(...)
JPanel
并根据图片
problem might be the path to your file, or the fact that JPanel
width and height will not be the same as the pictures thus we override getPrefferedSize(...)
of JPanel
and return correct size according to Image
这篇关于图像绘制适用于JFrame,但不适用于JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!