将图像加载到Java中

时间:2014-01-26 13:57:01

标签: java image swing loading

我刚开始用java编程。然而,最近我无法将图像加载到我的游戏中。

我已经尝试将它们加载到一个单独的资源包中并阅读/观看了无数的教程,但似乎没有任何工作!

有人能告诉我代码有什么问题或提供任何建议吗?任何帮助将非常感激。

import java.awt.Color;
import java.awt.Dimension;
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;

public class Main extends JFrame{

Image dbImage;
Graphics dbg;

//Variables for screen size
int
GWIDTH = 800,
GHEIGHT = 500;

//Dimension of gwidth and gheight
Dimension screenSize = new Dimension (GWIDTH, GHEIGHT);

public Main (){
//constructor to spawn window
this.setTitle ("Pond");
this.setSize(screenSize);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.cyan);
this.addKeyListener(new AL ());
ImageIcon img = new ImageIcon (getClass().getResource("/resources/ninja-png.gif"));

}

 public static void main (String[] args){

 Main m = new Main ();
 }

 //Double Buffering
  @Override

   public void paint (Graphics g){

dbImage = createImage (getWidth(), getHeight());
dbg = dbImage.getGraphics ();
draw (dbg);
g.drawImage(dbImage, 0, 0, this);  

 }

 public void draw (Graphics g){
 }
 public void paintComponent (Graphics g){
 }
 //Event Listener Class
 public class AL extends KeyAdapter {
@Override
public void keyPressed (KeyEvent e){
}
@Override
public void keyReleased (KeyEvent e){
 }
 }

  } 

2 个答案:

答案 0 :(得分:3)

  

“有人可以告诉我代码有什么问题或提供任何建议吗?”

  1. 不要在JFrame等顶级容器上绘画。

  2. 改为使用JPanel并覆盖其paintComponent方法并致电super.paintComponent

  3. @Override getPreferredSize()在绘画时JPanel,因此面板将具有优先大小。

  4. 请致电frame.pack()而不是设置尺寸,并且会尊重JPanel的首选尺寸。

  5. 将自定义绘画面板添加到JFrame

  6. 您从未初始化您尝试绘制的图像。这样做。

    dbImage = new ImageIcon(getClass().getResource("/resources/stackoverflow5.png")).getImage();
    

    声明您永远不会使用的新ImageIcon

  7. Event Dispatch Thread像这样运行Swing应用

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                Main m = new Main();
            }
        });
    }
    
  8. 确保您在src中直接拥有资源包

    Project
          src
             resources
    

  9. enter image description here

    完整重构的源代码所有上述内容。

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Main extends JFrame {
    
        Image dbImage;
    
        int GWIDTH = 800;
        int GHEIGHT = 500;
    
        public Main() {
            dbImage = new ImageIcon(getClass().getResource("/resources/stackoverflow5.png")).getImage();
            this.setTitle("Pond");
            add(new ImagePanel());
            this.pack();
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            this.setBackground(Color.cyan);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    Main m = new Main();
                }
            });
        }
    
        public class ImagePanel extends JPanel {
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                g.drawImage(dbImage, 0, 0, GWIDTH, GHEIGHT, this);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(GWIDTH, GHEIGHT);
            }
        }
    }
    

答案 1 :(得分:1)

我相信有一种更简单的方法: 创建一个JLabel并将其添加到框架中:

BufferedImage bi = ImageIO.read(/*This uses a URL*/new URL(YOURURLHERE));
ImageIcon image = new ImageIcon(bi);
JLabel label = new JLabel(image);

然后只需将该标签添加到框架中。