2012-03-05 158 views
2

我無法在我寫的paint方法上插入圖像。我想要在某些座標處將圖像與paint方法重疊。如何在JPanel(在JPanel)paint方法上添加圖像?

我的代碼:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Polygon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class testguipaint { 

    public static void main(String[] args) { 
     testguipaint img = new testguipaint(); 
    } 
    public testguipaint() { 
     JFrame frame = new JFrame(); 
     frame.add(crafting, BorderLayout.CENTER); 
     frame.setSize(442, 284); 
     frame.setLocationRelativeTo(null); 
     frame.setResizable(false); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(3); 
    } 

    static JPanel crafting = new JPanel() { 
     public void paint(Graphics g) { 
      Color darkGrey = new Color(153, 153, 153); 
      g.setColor(darkGrey); 
      g.fillRect(0, 0, 436, 252); 
      Color lightGrey = new Color(198, 198, 198); 
      g.setColor(lightGrey); 
      g.fill3DRect(3, 3, 430, 246, true); 
      g.setColor(darkGrey); 
      g.fill3DRect(16, 16, 222, 222, true); 
      g.fill3DRect(320, 78, 100, 100, true); 
      g.fillRect(248, 121, 39, 12); 
      Polygon triangle = new Polygon(); 
      triangle.addPoint(287, 103); 
      triangle.addPoint(287, 151); 
      triangle.addPoint(311, 127); 
      g.fillPolygon(triangle); 
      g.setColor(Color.white); 
      g.fill3DRect(88, 16, 3, 222, true); 
      g.fill3DRect(163, 16, 3, 222, true); 
      g.fill3DRect(16, 88, 222, 3, true); 
      g.fill3DRect(16, 163, 222, 3, true); 
      //BufferedImage image = new ImageIO.read(new File("/minecraft jpeg's/Products/Bread.png")); 
      //g.drawImage(image, 44, 191, null); 
      //44, 191 
     } 
    };  
} 
+0

你是否意味着將圖像作爲背景,然後使用其他繪畫到JPanel? – mKorbel 2012-03-05 18:37:40

+0

不,我需要將圖像放在上面看到的繪製方法(重疊)上。 – Lee 2012-03-05 18:40:27

+0

好,如果你需要,但是這個圖像可以覆蓋使用的油漆 – mKorbel 2012-03-05 18:42:39

回答

2

不是答案如何重疊或重疊

1)testguipaintTestGuiPaint更多關於Java命名約定herehere

2)搖擺GUI rellated代碼應被包裹成invokeLater(),更多有關Initial Threads

3),用於油畫Swing JComponents有方法paintComponent()代替paint()方法,詳細瞭解在2D Graphics

2

只是一個建議,描繪影像最後在塗料成分。這將允許通過在其他對象上繪製圖像。另外,我知道你可能只是在測試一些東西,但是你的類名中每個單詞的首字母都應該大寫。

0

此處僅對短代碼進行冷凝,僅實現MKorbel所做的(1和3)中的2條建議,並且爲了簡潔起見對JFrame進行了擴展,從主中刪除了從未使用的實例。沒有你要求的。

但是,您肯定不希望從HDD重複讀取圖像,每次面板都需要重新繪製。所以我們創建一個擁有屬性的實例,它就是圖像。

import java.awt.*; 
import javax.swing.*; 
import javax.swing.JPanel; 
import java.awt.image.*; 
import javax.imageio.*; 
import java.io.*; 

public class TestGuiPaint extends JFrame { 

    public static void main (String [] args) { 
     new TestGuiPaint(); 
    } 
    public TestGuiPaint() { 
    super ("TestGuiPaint"); 
     add (new CraftingPanel(), BorderLayout.CENTER); 
     setSize (442, 284); 
     setLocationRelativeTo (null); 
     setResizable (false); 
     setVisible (true); 
     setDefaultCloseOperation (3); 
    } 

    class CraftingPanel extends JPanel { 
    BufferedImage image = null; 
     public CraftingPanel() { 
     try { 
      image = ImageIO.read (new File ("./maeander3.png")); 
     } catch (java.io.IOException ioe) 
     { 
      System.err.println (ioe.getMessage()); 
     }  
     } 

     public void paintComponent (Graphics g) { 
      Color darkGrey = new Color (153, 153, 153); 
      g.setColor (darkGrey); 
      g.fillRect (0, 0, 436, 252); 
      Color lightGrey = new Color (198, 198, 198); 
      g.setColor (lightGrey); 
      g.fill3DRect (3, 3, 430, 246, true); 
      g.setColor (darkGrey); 
      g.fill3DRect (16, 16, 222, 222, true); 
      g.fill3DRect (320, 78, 100, 100, true); 
      g.fillRect (248, 121, 39, 12); 
      Polygon triangle = new Polygon(); 
      triangle.addPoint (287, 103); 
      triangle.addPoint (287, 151); 
      triangle.addPoint (311, 127); 
      g.fillPolygon (triangle); 
      g.setColor (Color.white); 
      g.fill3DRect (88, 16, 3, 222, true); 
      g.fill3DRect (163, 16, 3, 222, true); 
      g.fill3DRect (16, 88, 222, 3, true); 
      g.fill3DRect (16, 163, 222, 3, true); 
     g.drawImage (image, 44, 191, null); 
     } 
    };  
} 

我希望你明白我爲圖像選擇了不同的文件名。 :)