2013-07-10 282 views
-1

我想使用paint方法將圖像添加到屏幕上?我不想使用JLabel,因爲我想自己設置位置。如何在屏幕上繪製圖像?

Board.class:

public class Board extends JPanel{ 
    BufferedImage image; 

public Board() { 
    try { 
     image = ImageIO.read(new File("C:\\Users\\alexa_000\\Pictures\\RocketShip.png")); 
    }catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 
protected void paintComponent(Graphics g) { 
    super.paintComponents(g); 
    g.drawImage(image, 0, 0, this); 
} 
public Dimension getPreferredSize() { 
    return new Dimension(image.getWidth(),image.getHeight()); 
} 

} 

RType.class:

public class RType extends JFrame{ 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
       Panel panel = new Panel(); 
       frame.setContentPane(panel); 
       frame.setSize(800,600); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
     } 
    }); 
} 
} 
+0

你在哪裏重寫這個'paint'方法?在擴展'JPanel'的類中? –

+2

請發佈一個更完整的程序,一個我們可以運行並向我們展示什麼是不工作的,[sscce](http://sscce.org)。 –

+0

這裏是一個很好的教程爲你:http://zetcode.com/tutorials/javaswingtutorial/painting/ –

回答

2

你有這麼多的錯誤,我不知道,甚至從哪裏開始......而不是在JLabel顯示圖像的你可以在一些容器上顯示你的圖像。像JPanel。 這是一個小型演示,演示如何在JPanel上顯示圖像。代碼中的解釋。

import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

//No need to extend your class with JFrame 
public class Board { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       //Instantiate Panel class. 
       Panel panel = new Panel(); 
       //Set instance of Panel class as a content pane for your JFrame 
       frame.setContentPane(panel); 
       //Avoid calling setSize. Call pack instead 
       frame.pack(); 
       //If you want to position your frame on center of screen, no need 
       //for fancy calculations. This is how you can do it. 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 

    } 

    /*Image will be displayed on JPanel*/ 
    private static class Panel extends JPanel { 
     BufferedImage image; 

     public Panel() { 
      try { 
       //String you are passing in "new File()" is a path to your image 
       image = ImageIO.read(new File("path-to-your-image")); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     /*Override this method to display graphics on JPanel. 
     * Do not override paint method!*/ 
     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(image,0,0,this); 

     } 

     /*Override getPreferredSize method so it returns dimensions of your image. 
     * Size of your container (Panel) will be equal to size of that image*/ 
     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(image.getWidth(),image.getHeight()); 
     } 
    } 
} 
+0

我喜歡你開始這個問題的方式:p我對java比較陌生,所以這就是爲什麼有很多錯誤的原因。無論如何,我試過你的答案,我無法讓它工作。我更新了我試過的代碼,如果你想再看一次:D – alexdr3437

+1

@ alexdr3437:這意味着你的圖片文件的路徑可能是錯誤的。儘管使用InputStream更好,但對此答案只有1+。另外,你真的應該瀏覽你的評論中鏈接到的教程。你不能猜測這個東西,並希望它可以工作。 –

+0

複製該代碼的樣子。將路徑設置爲:'C:/ Users/alexa_000/Pictures/RocketShip.png'。它應該工作。我只是測試了我的代碼。 –