2014-02-27 105 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