2013-05-07 87 views
0

我在這個java代碼中有問題。 我想製作一個畫家程序,但每當我選擇一個形狀並繪製它時,之前繪製的所有形狀都與此形狀相同。這是代碼。 我知道問題出自paintComponent中的for語句,但是我可以用什麼替換它?Java畫家程序繪製形狀

class inner extends JPanel implements MouseListener,MouseMotionListener{ 
     private int oldx,oldy,newx,newy; 
     private Point point1,point2; 
     private Shape newRec,line1; 
     Rectangle currRec; 
     private Vector shape; 
     private boolean status,status1; 
     private int count=0; 
     private Object line; 
     inner(){ 
      point1=point2=new Point(0,0); 
      shape = new Vector(); 
      addMouseListener(this); 
      addMouseMotionListener(this); 
     } 

     public void mouseDragged(MouseEvent event) { 

      point2=event.getPoint(); 
      newx = point2.x; 
      newy = point2.y; 
      if(Universal==7){ 
      line = new Object(oldx,oldy,newx,newy); 
      status = true; 
      repaint(); 
      } 


      currRec = new Rectangle(Math.min(point1.x,point2.x),Math.min(point1.y,point2.y),Math.abs(point1.x-point2.x),Math.abs(point1.y-point2.y)); 
      status = true; 
      repaint(); 

     } 

     @Override 
     public void mouseMoved(MouseEvent arg0) {} 
     public void mouseClicked(MouseEvent arg0) {}     
     public void mouseEntered(MouseEvent arg0) {} 
     @Override 
     public void mouseExited(MouseEvent arg0) {} 


     public void mousePressed(MouseEvent event) { 

      point1=event.getPoint(); 
      oldx=event.getX(); 
      oldy=event.getY(); 
     } 

     @Override 
     public void mouseReleased(MouseEvent event) { 

      point2=event.getPoint(); 
      newx=event.getX(); 
      newy=event.getY(); 
      //count++; 


     if(Universal==7){ 
       line1 = new Shape(point1.x,point1.y,point2.x,point2.y); 
       shape.add(line1); 
       //Graphics g = getGraphics(); 
       //g.setColor(colour); 
       //g.drawLine(point1.x,point1.y,point2.x,point2.y); 

       count++; 
       repaint(); 
      } 
     else if(Universal==1||Universal==2||Universal==3||Universal==4||Universal==5||Universal==6){ 
       newRec = new Shape(Math.min(point1.x,point2.x),Math.min(point1.y,point2.y),Math.abs(point1.x-point2.x),Math.abs(point1.y-point2.y)); 
       shape.add(newRec); 
       count++; 
       repaint(); 
      } 
     } 

    ///// the problem is in here  
     public void paintComponent(Graphics g){ 
      super.paintComponent(g); 
      Shape c; 
      g.setColor(colour); 
      for(int i=0;i<shape.size();i++){ 
       c = (Shape) shape.get(i); 

      if(Universal==1){ 

       g.drawRect(c.x, c.y, c.w, c.h); 
       if(status){ 
        g.drawRect(currRec.x, currRec.y, currRec.width, currRec.height); 
       } 
      } 

      if(Universal==2){ 
       g.fillRect(c.x, c.y, c.w, c.h); 

       if(status){ 
        g.fillRect(currRec.x, currRec.y, currRec.width, currRec.height); 

       } 
      } 

      if(Universal==3){ 
       g.drawRoundRect(c.x, c.y, c.w, c.h, c.w/4, c.h/4); 

       if(status){ 
        g.drawRoundRect(currRec.x, currRec.y, currRec.width, currRec.height,currRec.width/4,currRec.height/4); 

       } 
      } 

      if(Universal==4){ 
       g.fillRoundRect(c.x, c.y, c.w, c.h, c.w/4, c.h/4); 

       if(status){ 
        g.fillRoundRect(currRec.x, currRec.y, currRec.width, currRec.height,currRec.width/4,currRec.height/4); 

       } 
      } 

       if(Universal==5){ 

        g.drawOval(c.x, c.y, c.w, c.h); 

        if(status){ 
         g.drawOval(currRec.x, currRec.y, currRec.width, currRec.height); 
        } 
       } 

       if(Universal==6){ 

        g.fillOval(c.x, c.y, c.w, c.h); 

        if(status){ 
         g.fillOval(currRec.x, currRec.y, currRec.width, currRec.height); 
        } 
       } 

       if(Universal==7){ 

        g.drawLine(c.x, c.y, c.w, c.h); 

        if(status){ 
         g.drawLine(line1.x, line1.y, line1.w,line1.h); 
        } 
       } 

       if(Universal==8){ 
        shape.clear(); 
       } 
      } 
     } 
+0

什麼是通用? – arynaq 2013-05-07 19:25:36

+1

你可能想看看這個只畫多邊形的[答案](http://stackoverflow.com/a/16246610/928711),但它不會很複雜,以適應繪製其他形狀 – 2013-05-07 19:37:24

回答

1

Universal只會在任何給定的時間成爲給定的值。

塗料不累積,它們是破壞性的。

也就是說,每次調用paintComponent時,以前的所有內容都會被刪除/清除乾淨,並且您需要「重新繪製」內容。

而不是依靠一個單一的標誌,你應該添加Shape s到某種List並重新繪製所有時,當調用paintComponent。同樣,你可以簡單地添加「類型」(int)的List和過程,對每個列表重繪

看看Painting in AWT in Swing的烤漆工藝

1

的說明,請參見Custom Painting Approaches爲兩種不同的方式做到這一點:

  1. 添加形狀的列表,然後重新繪製所有列表中的形狀,每次的paintComponent()被調用。

  2. 將圖形繪製到BufferedImage上,然後只是在調用paintComponent()時重新繪製圖像。

這兩個示例都沒有完全符合您的要求,它只顯示您的方法。