2012-03-30 37 views
3

我想在here描述畫了一系列的矩形的玻璃面板。問題是隻有我列表中的最後一個元素顯示在窗格上。怎麼可能畫上玻璃板不止一個矩形?

有誰怎麼能畫一個以上的矩形在同一窗格?

下面是所用的代碼:在窗格的類

塗料的方法,延伸的JComponent

protected void paintComponent(Graphics g) { 
     if (point != null) { 

      int value = this.getGradient(); 


      Color myColour = new Color(255, value, 0, 175); 
      g.setColor(myColour); 
      g.fillRect(point.x - 13, point.y - 15, this.width, this.height); 

     } 
    } 
+0

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 – 2012-03-30 04:22:56

+2

'圖形G = container.getGraphics();'也就是說立即懷疑,但我會進一步評論之前等待SSCCE。 – 2012-03-30 04:23:57

+0

+1,對於這個問題,讓我學到新的東西:-) – 2012-03-30 05:13:37

回答

3

有上畫沒有內在限制在玻璃板,比剪裁邊界其他。例如,請在MyGlassPane中嘗試以下內容。

glass pane demo

protected void paintComponent(Graphics g) { 
    if (point != null) { 
     g.setColor(Color.red); 
     g.drawRect(point.x, point.y, 60, 20); 
     g.setColor(Color.blue); 
     g.drawRect(point.x, point.y, 20, 60); 
    } 
} 
+0

非常感謝您的幫助,我不畫都在這個方法中,矩形的,但我叫了好幾遍..感謝了很多:) – ict1991 2012-03-30 05:03:45

+1

很高興你把它分類。如果直接調用'paintComponent()',請考慮調用'repaint()'。 – trashgod 2012-03-30 05:18:24

+0

不錯,因爲我相信,直到這一刻,玻璃/的rootPane約爲漆(API和其他垃圾。)不是的paintComponent :-),右,外觀極好 – mKorbel 2012-03-30 06:01:58