2014-02-27 103 views
0

我在做一个SwingBot,无法弄清楚如何让图像出现。它编译得很好,但不可见。我究竟做错了什么? This is my image.它位于与我的代码java文件相同的目录中名为“images”的文件夹中。无法在Java中显示图像

import javax.swing.JFrame; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import javax.swing.JComponent; 
import java.awt.geom.Ellipse2D; 
import java.awt.geom.Line2D; 
import java.awt.Color; 
import java.awt.Polygon; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import javax.imageio.ImageIO; 
import java.io.IOException; 
import java.util.Scanner; 



public class SwingBot 
{ 
public static void main(String[] args) 
{ 

    // contruction of new JFrame object 
    JFrame frame = new JFrame(); 

    // mutators 
    frame.setSize(400,400); 
    frame.setTitle("SwingBot"); 

    // program ends when window closes 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Robot r = new Robot(); 

    frame.add(r); 

    // voila! 
    frame.setVisible(true); 


    // your Scanner-based command loop goes here 

    int noend = 0; 
    System.out.println("Enter Commands"); 
    while(noend == 0) 
    { 

    Scanner input = new Scanner(System.in); 

    String command = input.next(); 

    if(command.equals("left")) 
     r.moveBot(10,0); 

    if(command.equals("right")) 
     r.moveBot(-10,0); 

    if(command.equals("down")) 
     r.moveBot(0,10); 

    if(command.equals("up")) 
     r.moveBot(0,-10); 

    // call methods on the Robot instance like w.moveBot(10,10) in response to 
    // user input 



} 

}

public static class Robot extends JComponent 
{ 
    private Rectangle rect = new Rectangle(10,10); 
    private BufferedImage image; 

    public void ImagePanel() 
    { 
     try 
     {     
      image = ImageIO.read(new File("images/flower.png")); 
     } 
     catch (IOException ex) 
     { 
      // handle exception... 
     } 
    } 

    public void paintComponent(Graphics g) 
    { 
     Graphics2D g2 = (Graphics2D) g; 


     // set the color 
     //g2.setColor(Color.BLUE); 




     g.drawImage(image, 0, 0, null); 


    } 


    public void moveBot(int x, int y) 
    { 
     // move the rectangle 
     rect.translate(x,y); 

     // redraw the window 
     repaint(); 
    } 

} 

}

+0

尝试使用'的getClass()。的getResource( “图像/ flower.png”)'或'的getClass()。的getResource( “/图片/ flower.png”)'和'调用超。paintComponent'在做任何自定义绘画之前使用'this'作为'drawImage'的图像观察者参数 – MadProgrammer

+0

您在异常处理中究竟做了什么?在你不知道的情况下抛出异常,因为你在'catch'块中没有做任何事情? – Josh

+0

@John我从另一个StackOverflow答案中复制了这个。我认为它必须包括在内。抱歉。我对这一切都很陌生。 – user3200964

回答

3

这是容易被忽视,只是因为它是如此的不可思议。你有一个方法ImagePanel()。我认为可以肯定的是,您从ImagePanel得到的代码是类,ImagePanel()是构造函数,并且您刚刚添加void是因为您收到错误说该方法需要返回类型。

你应该做的是改为public ImagePanel()public Robot()所以你的Robot类有一个构造函数。目前您实例化Robot,但图像从不初始化,因为从未调用方法ImagePanel()

  1. 可能需要从File对象去了一些基本的what is a Constructor

  2. 更改public void ImagePanel()public Robot()

  3. 不要加载的图像。使用Robot.class.getResource("path/to/image.png")加载它们通过班级路径。见信息在Embedded Resources

  4. 你应该在你paintComponent方法调用super.paintComponent

    @Override 
    protected void paintComponent(Graphics g) { 
        super.paintComponent(g); 
    } 
    
  5. 当绘画,你应该重写你的JComponent所以成分具有较好大小,那么你可以pack()你的框架的getPreferredSize() 。从事件指派线程

    @Override 
    public Dimension getPreferredSize() { 
        return new Dimension(width, height); 
    } 
    
  6. 运行Swing应用程序,但使用SwingUtilities.invokLater。见Initial Thread

    public static void main(String[] args) { 
        SwingUtilities.invokeLater(new Runnable(){ 
         public void run() { 
    
         } 
        }); 
    } 
    

参见下面

UPDATE例如

enter image description here

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 


public class SwingBot { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.add(new Robot()); 
       frame.pack(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public static class Robot extends JComponent { 
     BufferedImage img; 

     public Robot() { 
      try { 
       img = ImageIO.read(Robot.class.getResource("/images/android.jpg")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(img, 0, 0, 300, 300, this); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(300, 300); 
     } 
    } 
} 

文件结构

ProjectRoot 
     src 
      somepackage 
       SwingBot.java 
      images 
       android.jpg 
+0

圣洁的废话。感谢您的所有努力。让我看看这是否有效...... – user3200964

+0

注意:使用''/images/flower.png''作为'getResource()'的路径,并确保你的'images'文件夹是'src'的直接子节点。 –

+0

你能解释一下吗? – user3200964