从计算机显示图像

时间:2015-04-22 23:03:35

标签: java image file netbeans

我正在尝试显示文件中的图像(保存在我的计算机上)我试图加载的图像称为" 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();

    }
}

1 个答案:

答案 0 :(得分:0)

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;
}

如果抛出任何异常,这将显示加载的图像或带有errormessage的其他图像加载图像。