【问题标题】:displaying an image from a computer显示来自计算机的图像
【发布时间】:2015-04-22 23:03:34
【问题描述】:

我正在尝试显示来自文件(保存在我的计算机上)中的图像 我正在尝试加载的图像称为“maze.jpg”。从我目前的代码来看,它能够运行和编译,但是每当我运行它时,我都会得到一个空白窗口。我究竟做错了什么??我正在使用 netbeans 对此进行编码。这是我的代码:

package mazedisplay;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.Scanner;
import javax.imageio.*;
import javax.swing.*;

public class MazeDisplay extends Component
{
   BufferedImage b;

   public void paint(Graphics g)
   {
      g.drawImage(b,0,0,null);//supposed to draw an image at these coordinates
   }

   public MazeDisplay()
   {
      try
      {
         b = ImageIO.read(new File("C:\\Users\\Owner\\Documents\\NetBeansProjects\\MazeDisplay\\maze.jpg""));//reads the file

      }

      catch(Exception e)
      {
          e.printStackTrace();

      }


   }

   public Dimension getPreferredSize() 
   {
        if (b == null) 
        {
         return new Dimension(800,800);
        } 
        else 
        {
           return new Dimension(b.getWidth(null), b.getHeight(null));
        }
   }

   private static String[][] readMaze(String filepath) 
    {
        try
        {
            Scanner scan = new Scanner(new File(filepath));

        }

        catch(Exception e)
        {

        }
       return null;
    }


    public static void main(String[] args) 
    {
        String[][] maze = readMaze("C:\Users\Owner\Documents\NetBeansProjects\MazeDisplay\maze.jpg");
        JFrame f = new JFrame("Maze");//sets the frame to say "maze"
        System.out.println(new File("maze.jpg").getAbsolutePath());

        f.addWindowListener(new WindowAdapter()
        {
                public void windowClosing(WindowEvent e)//this is for closing the program
                {
                    System.exit(0);
                }
            });

        Component add = f.add(new MazeDisplay());
        f.pack();
        f.setVisible(true);// makes sure we can see it

        JPanel displayPanel = new JPanel();
        JPanel content = new JPanel();

    }
}

【问题讨论】:

  • 好的,我想我必须给出这样的路径: ("C:\\Users\\Owner\\Desktop\\maze.jpg") ?
  • 取决于图像的位置。如果它在项目文件夹中,您也可以使用相对路径。在try-catch-block中添加e.printStackTrace(),用于加载图片查看是否有异常抛出
  • 好吧!但是 e.printStackTrace () 是做什么的?另外,我在其中添加了它,显然我得到的是:运行:javax.imageio.IIOException:无法读取输入文件!在 javax.imageio.ImageIO.read(ImageIO.java:1301) 在 mazedisplay.MazeDisplay.(MazeDisplay.java:23) 在 mazedisplay.MazeDisplay.main(MazeDisplay.java:58) 构建成功(总时间:1第二)
  • e.printStackTrace() 打印异常的堆栈跟踪(所有方法及其在堆栈中的行从抛出异常的位置到捕获异常的方法)。 new File("maze.jpg") 很可能没有指向带有您的图像的文件。您可以通过打印完整路径 (file.getAbsolutePath()) 进行检查。
  • 我明白了,但是我应该在哪里添加 file.getAbsolutePath() ?

标签: java image file netbeans


【解决方案1】:
JComponent display = new JComponent() {
    BufferedImage bi = loadImg();

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bi, 0, 0, this);
    }

    public Dimension getPreferredSize() {
        return new Dimension(bi.getWidth(), bi.getHeight());
    }
};

JFrame frame = new JFrame("Maze");
frame.setContentPane(display);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);

private static BufferedImage loadImg(){
    BufferedImage bi = null;
    try{
        bi = ImageIO.read(new File("yourfile.jpg"));
    }catch(IOException e){
        e.printStackTrace();

        bi = new BufferedImage(200 , 100 , BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0 , 0 , 200 , 100);
        g.setColor(Color.black);
        g.drawString(e.getMessage() , 20 , 20);
    }

    return bi;
}

如果抛出任何异常,这将显示加载的图像或另一个带有加载图像的错误消息的图像。

【讨论】:

  • 我能够让它工作! =) 非常感谢你帮助我!对不起所有这些 cmets XD 但这段代码做了什么:图形 g = bi.getGraphics(); g.setColor(Color.white); g.fillRect(0 , 0 , 200 , 100); g.setColor(Color.black); g.drawString(e.getMessage() , 20 , 20); }
  • 它创建一个白色背景和黑色错误消息的新图像。每个图像都有自己的图形对象,可用于在其上绘画。
猜你喜欢
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
相关资源
最近更新 更多