Java:Image Not Displaying [英] Java: Image Not Displaying
问题描述
我正在制作一个需要显示图像的程序。我正在使用ImageIcon和Image类来执行此操作。我在构造函数中声明了ImageIcon,然后通过i.getImage()分配图像值。除图像外的所有内容似乎都加载正常。这是我的代码:
更新:我在与代码相同的目录中有图像。我正在使用mac,并尝试过image.png,./image.png,Users / myStuff / Documents / workspace / Game / src / image.png。其他的这些都起作用了。
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
@SuppressWarnings(serial)
public class Game扩展JFrame {
int x = 100,y = 100;
私人图片dbimage;
私人图形dbg;
图片图片;
class AL extends KeyAdapter {
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
if(x <= 20){
x = 20;
} else {
x - = 5;
}
} else if(keyCode == e.VK_RIGHT){
if(x> = 230){
x = 230;
} else {
x + = 5;
}
} else if(keyCode == e.VK_UP){
if(y <= 20){
y = 20;
} else {
y - = 5;
}
} else if(keyCode == e.VK_DOWN){
if(y> = 230){
y = 230;
} else {
y + = 5;
$ b public Game(){
//载入图像
ImageIcon i =新ImageIcon(image.png);
image = i.getImage();
//设置属性
addKeyListener(new AL());
setTitle(游戏);
setSize(250,250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.CYAN);
setVisible(true);
$ b public void paint(Graphics g){
dbimage = createImage(getWidth(),getHeight());
dbg = dbimage.getGraphics();
paintComponent(dbg);
g.drawImage(dbimage,0,0,this);
$ b $ public void paintComponent(Graphics g){
g.setFont(new Font(Arial,Font.BOLD | Font.ITALIC,30));
g.setColor(Color.MAGENTA);
g.drawString(Hello World!,50,50);
g.setColor(Color.RED);
g.drawImage(image,100,100,this);
repaint();
public static void main(String [] args){
new Game();
}
}
上面的代码存在重大问题......首先,如果您的图像与您的类文件一起使用,那么请不要将它作为文件获取,而是将其作为MadProg显示的资源(并且本网站上的大多数类似问题会告诉您):
//获取你的图像作为资源
URL resource = Game.class.getResource(RESOURCE_PATH);
BufferedImage img = null;
尝试{
//使用ImageIO读入
img = ImageIO.read(resource);
} catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
接下来,永远不要在JFrame中直接绘制,方法。相反,请查看Swing绘图教程并遵循它们的前导:在JPanel的paintComponent方法内绘制,但只能在调用super的paintComponent方法后才能使绘画继续沿着绘画链的线条:
//在paintComponent方法中绘制,而不是绘制方法
@Override
保护void paintComponent(Graphics g){
//将super的方法调用到所有绘画以链接该行
super.paintComponent(g);
if(dbimage!= null){
g.drawImage(dbimage,imgX,imgY,this);
}
}
我更喜欢在修改尺寸时覆盖JPanel的getPreferredSize我的图形用户界面,如下所示:
//安全地设置主游戏JPanel的首选大小
@Override
public Dimension getPreferredSize(){
if(isPreferredSizeSet()){
return super.getPreferredSize();
}
返回新维(PREF_W,PREF_H);
放在一起,就像可能一样:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing。*;
public class Game extends JPanel {
public static final String RESOURCE_PATH =image.png;
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private BufferedImage dbimage = null;
private int imgX = 0;
private int imgY = 0;
public Game(BufferedImage dbimage){
this.dbimage = dbimage;
}
//在paintComponent方法中绘制,而不是绘制方法
@Override
protected void paintComponent(Graphics g){
// call超级的方法,以所有的绘画链下来
super.paintComponent(g);
if(dbimage!= null){
g.drawImage(dbimage,imgX,imgY,this);
//设置主游戏JPanel的首选大小安全
@Override
public Dimension getPreferredSize(){
if(isPreferredSizeSet()){
return super.getPreferredSize();
}
返回新维(PREF_W,PREF_H);
}
private static void createAndShowGui(){
//将图片作为资源
URL resource = Game.class.getResource(RESOURCE_PATH);
BufferedImage img = null;
尝试{
//使用ImageIO读入
img = ImageIO.read(resource);
} catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
//将图像传入您的游戏JPanel
游戏mainPanel =新游戏(img);
//将JPanel传递给JFrame
JFrame frame = new JFrame(Game);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane()。add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true); //并显示它
}
public static void main(String [] args){
//以线程安全的方式启动您的Swing GUI
SwingUtilities .invokeLater(() - > {
createAndShowGui();
});
接下来查看键绑定以帮助您执行动画用这个GUI键击
I am making a program where I need to display an image. I am using the ImageIcon and Image classes in order to do this. I declared the ImageIcon in the constuctor and then assigned the images value through i.getImage(). Everything but the image seems to be loading fine. Here is my code:
UPDATE: I have the image in the same directory as the code. I am using a mac and I have tried "image.png", "./image.png", "Users/myStuff/Documents/workspace/Game/src/image.png". Niether of these have worked.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Game extends JFrame {
int x = 100, y = 100;
private Image dbimage;
private Graphics dbg;
Image image;
class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
if (x <= 20) {
x = 20;
} else {
x -= 5;
}
} else if (keyCode == e.VK_RIGHT) {
if (x >= 230) {
x = 230;
} else {
x += 5;
}
} else if (keyCode == e.VK_UP) {
if (y <= 20) {
y = 20;
} else {
y -= 5;
}
} else if (keyCode == e.VK_DOWN) {
if (y >= 230) {
y = 230;
} else {
y += 5;
}
}
}
}
public Game() {
//load up image
ImageIcon i = new ImageIcon("image.png");
image = i.getImage();
//set up properties
addKeyListener(new AL());
setTitle("Game");
setSize(250, 250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.CYAN);
setVisible(true);
}
public void paint(Graphics g) {
dbimage = createImage(getWidth(), getHeight());
dbg = dbimage.getGraphics();
paintComponent(dbg);
g.drawImage(dbimage, 0, 0, this);
}
public void paintComponent(Graphics g) {
g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30));
g.setColor(Color.MAGENTA);
g.drawString("Hello World!", 50, 50);
g.setColor(Color.RED);
g.drawImage(image, 100, 100, this);
repaint();
}
public static void main(String[] args) {
new Game();
}
}
Significant issues with your code above...
first and foremost, if your image is with your class files, then don't get it as a File, but rather get it as a resource as MadProg shows (and as most similar questions on this site will tell you):
// get your image as a resource
URL resource = Game.class.getResource(RESOURCE_PATH);
BufferedImage img = null;
try {
// read in using ImageIO
img = ImageIO.read(resource);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
Next of all, never draw directly within the JFrame and certainly not within its paint method. Instead check out the Swing drawing tutorials and follow their lead: draw within the paintComponent method of a JPanel, but only after calling the super's paintComponent method so that the painting can continue down the line of the painting chain.:
// draw within the paintComponent method, not the paint method
@Override
protected void paintComponent(Graphics g) {
// call the super's method to all painting to chain down the line
super.paintComponent(g);
if (dbimage != null) {
g.drawImage(dbimage, imgX, imgY, this);
}
}
I prefer to override the JPanel's getPreferredSize when fixing the sizes of my GUI, something like:
// set the preferred size of the main Game JPanel safely
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
Put all together, something like so might work:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Game extends JPanel {
public static final String RESOURCE_PATH = "image.png";
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private BufferedImage dbimage = null;
private int imgX = 0;
private int imgY = 0;
public Game(BufferedImage dbimage) {
this.dbimage = dbimage;
}
// draw within the paintComponent method, not the paint method
@Override
protected void paintComponent(Graphics g) {
// call the super's method to all painting to chain down the line
super.paintComponent(g);
if (dbimage != null) {
g.drawImage(dbimage, imgX, imgY, this);
}
}
// set the preferred size of the main Game JPanel safely
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
// get your image as a resource
URL resource = Game.class.getResource(RESOURCE_PATH);
BufferedImage img = null;
try {
// read in using ImageIO
img = ImageIO.read(resource);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
// pass image into your Game JPanel
Game mainPanel = new Game(img);
// pass the JPanel into a JFrame
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true); // and display it
}
public static void main(String[] args) {
// start your Swing GUI in a thread-safe manner
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
Next look up Key Bindings to help you to do animation with key strokes with this GUI
这篇关于Java:Image Not Displaying的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!