2017-03-05 147 views
-1

黑色JPanel的用途在於繪製。限制JPanel上的繪圖區域並保存圖形狀態

  • 我該如何限制圖形到由線形成的圓的半徑?

  • 有沒有辦法保存圖形對象狀態,以便可以添加更多的圖形以及添加撤消功能?

enter image description here

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 

    sectors = 12; 

    Graphics2D g2d = (Graphics2D) g;  
    g2d.setColor(Color.RED); 

    sector = new Line2D.Double(getWidth()/2, 0, getWidth()/2, getHeight()); 
    //g2d.setClip(new Ellipse2D.Double(getWidth()/2,getHeight()/2, radius, radius)); 


    //draws the sectors on the screen 
    for(int i=0; i<sectors; i++) 
    { 
     g2d.draw(sector); 
     g2d.rotate(Math.toRadians(30),getWidth()/2,getHeight()/2); 
    } 

    //draws the doily 
    if(dragging) 
    { 
     for(int i=0; i<sectors; i++) 
     { 
      g2d.fillOval((int) draw.getX(), (int) draw.getY(),20, 20); 
      g2d.rotate(Math.toRadians(30), getWidth()/2, getHeight()/2); 
     } 

     //saves the current drawing in a stack 
     graphics.push(g2d); 
    } 
} 
+0

您可以添加黑色JPanel的代碼 – Kennedy

+0

我只是將它的顏色設置爲黑色並給它一個大小。其他一切都在paintComponent() –

+0

1)爲了更好地幫助您,請發佈一個[MCVE]或[Short,Self Contained,Correct Example](http://www.sscce.org/)。 2)*「我如何限制繪圖到由線形成的圓的半徑?」* [''Graphics.setClip(Shape)'](http://docs.oracle.com/javase/8/docs/ api/java/awt/Graphics.html#setClip-java.awt.Shape-)3)*「有沒有辦法..」*哇!這是問答網站,而不是幫助臺。每個具體的問題應該是自我包含的,並且應該是自己的問題線索。我建議你[編輯]這個帖子以消除第二個問題,否則它有可能被關閉爲「太寬泛」。 –

回答