问题描述
试图将图像打印到窗口中。一切运行都没有错误,并且如果将drawImage替换为另一个图形类,它也可以正常工作。但是,窗口缺少图像,我不确定为什么。同样,JFrame素材和图形可以与绘制其他图形一起正常工作,但不能在此处绘制图像。
trying to get an image to print into a window. Everything runs without errors, and it also works if I replace the drawImage with another graphics class. However, the window is missing the image, and i'm not sure why. Again, the JFrame stuff and Graphics work fine with drawing other graphics, but only doesn't draw the image here. Thanks.
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class GraphicsMovement2 extends JApplet{
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
JApplet example = new GraphicsMovement2();
JFrame frame = new JFrame("Movement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(example);
frame.setSize(new Dimension(1366,768)); //Sets the dimensions of panel to appear when run
frame.setVisible(true);
}
public void paint (Graphics page){
page.drawImage(image, 100, 100, 100, 100, Color.RED, this);
}
}
推荐答案
您已经两次定义了 image
...
You've defined image
twice...
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
这实际上意味着您到达绘画
方法,它是 null
,因为您尚未初始化实例变量。
This essentially means that by the time you get to the paint
method, it is null
as you haven't initialized the instance variable.
另一个问题是您将尝试从静态引用加载图像,但图像
没有声明为 static
。最好将此逻辑移到构造函数或实例方法中。
Another problem you will have is the fact that you are trying to load the image from a static reference but the image
isn't declared as static
. Better to move this logic into the constructor or instance method.
在您使用容器时不要使用 JApplet
作为容器要添加到 JFrame
中,最好使用 JPanel
之类的东西。
Don't use JApplet
as your container when you're adding to a JFrame
, you're better of using something like JPanel
. It will help when it comes to adding things to the container.
您必须打电话 super.paint(g)
...实际上,不要覆盖诸如 paint 方法> JFrame 或 JApplet
。使用类似 JPanel
的方法,并覆盖 paintComponent
方法。
YOU MUST CALL super.paint(g)
...in fact, DON'T override the paint
method of top level containers like JFrame
or JApplet
. Use something like JPanel
and override the paintComponent
method instead. Top level containers aren't double buffered.
paint
方法完成了许多重要的工作,并且更加容易使用 JComponent#paintComponent
...但不要忘记调用 super.paintComponent
The paint
methods does a lot of important work and it's just easier to use JComponent#paintComponent
... but don't forget to call super.paintComponent
已更新
您需要定义图片
因为您将 image
声明为<$的实例字段,所以c $ c> GraphicsMovement2 ,则需要引用 GraphicsMovement2
的实例才能引用它。
Because you declared the image
as an instance field of GraphicsMovement2
, you will require an instance of GraphicsMovement2
in order to reference it.
但是,在您的 main
方法(即静态
)中,您还声明了一个名为<$ c的变量。 $ c>图片。
However, in you main
method, which is static
, you also declared a variable named image
.
的
看不到您在绘画
方法GraphicsMovement2 main
中声明的变量,只能看到实例字段( null
)。
The paint
method of GraphicsMovement2
can't see the variable you declared in main
, only the instance field (which is null
).
要解决此问题,您需要将图像的加载移动到 GraphicsMovement2
的实例的上下文中,这可以最好地实现(在您的上下文中),但是将图像的加载移动到<$的构造函数中c $ c> GraphicsMovement2
In order to fix the problem, you need to move the loading of the image into the context of a instance of GraphicsMovement2
, this can be best achived (in your context), but moving the image loading into the constructor of GraphicsMovement2
public GraphicsMovement2() {
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
}
下面的两个示例将产生相同的结果。
The two examples below will produce the same result...
简单方法
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
public ImagePane() {
setLayout(new BorderLayout());
ImageIcon icon = null;
try {
icon = new ImageIcon(ImageIO.read(new File("/path/to/your/image")));
} catch (Exception e) {
e.printStackTrace();
}
add(new JLabel(icon));
}
}
}
硬方法
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage background;
public ImagePane() {
try {
background = ImageIO.read(new File("/path/to/your/image"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
阅读教程的时间
- Creating a GUI With JFC/Swing
- Performing Custom Painting
这篇关于JFrame中的BufferedImage不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!