2012-12-08 26 views
-1

如何在面板中添加位圖圖像,然後獲取圖像正在使用的圖形,並告訴面板使用圖像內部的相同圖形繪製線條。將圖像添加到不使用擺動的面板

+1

你想編輯圖像?也許保存結果? – MadProgrammer

+0

沒有任何的那種..jst想做上述事情 – maddy

+0

請參閱[這個答案](http://stackoverflow.com/questions/12683533/drawing-a-rectangle-that-wont-disappear-in-next - 油漆/ 12683632#12683632)爲起點(雖然它有點長)。 –

回答

2

基本繪畫是由Swing組件paintComponent方法完成的。

你的最佳選擇是使用ImageIO API加載圖像...

BufferedImage image; 

public void loadImage() throws IOException { 
    image = ImageIO.read(...); 
    // ImageIO can read a image from a file or a URL or a ImageInputStream 
} 

然後簡單地繪製圖像...

protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.drawImage(image, 0, 0, this); 
    // Now you can continue drawing ontop of it... 
    g.setColor(Color.RED); 
    g.drawLine(0, 0, image.getWidth(), image.getHeight()); 
} 

你可能想有一個讀的