2011-04-26 56 views
1

嗨我嘗試繪畫應用程序,並試圖計算內部類如何訪問主要的graphic2D函數來實現徒手​​繪製選擇?還是我吠叫錯了樹?java:如何實現徒手繪製使用內部類

import javax.swing.*; // For JPanel, etc. 
    import java.awt.*; // For Graphics, etc. 
    import java.awt.geom.*; // For Ellipse2D, etc. 
    import java.awt.event.*; 
    import java.util.ArrayList; 
    import java.awt.Shape; 
    import java.awt.Graphics2D; 
    import java.lang.Math; 
    import javax.swing.event.ChangeListener; 
    import javax.swing.event.ChangeEvent; 

    public class DrawingPanel extends JPanel 
    { 
     private double x1=0; 
     private double x2=0; 
     private double y1=0; 
     private double y2=0; 
     private double x3=0; 
     private double x4=0; 
     private double y3=0; 
     private double y4=0; 
     private double tx=0; 
     private double ty=0; 
     private double tz=0; 
     double width = Math.abs(x1 -x2); 
     double height = Math.abs(y1-y2); 

     private Point start, end; 
     private ArrayList<Shape> myArr = new ArrayList<Shape>(); 
     private ArrayList<Shape> myArr2 = new ArrayList<Shape>(); 
     ButtonPanel buttonPress; 

     protected void paintComponent(Graphics g) { 
     super.paintComponent(g);// let panel draw itself 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.setPaint(Color.blue); 
     g2d.setStroke(new BasicStroke(4)); 
     for (Shape i : myArr) 
     { 
      g2d.draw(i); 
     } 
     for(int j = 0;j<myArr2.size();j++) 
     { 
      //g2d.setColor(shapeTransColor.get(i));// get the colour from the colour array 
      g2d.fill(myArr2.get(j));// fill the shape from the shape array         
     } 

     }  
     //inner class 

     class Listener1 extends MouseAdapter 
     { 
        public void mousePressed(MouseEvent e) 
     { 

      x1=e.getX(); 
      y1=e.getY(); 
      System.out.println("Mouse Pressed"); 
       if (buttonPress.buttonType.equals("Clear")) 
      {       
       System.out.println("ArrayList Size :"+myArr.size()); 
       System.out.println("ArrayList2 Size :"+myArr2.size());     
       myArr.clear(); 
       myArr2.clear(); // clears all elements from arraylists 
       System.out.println("ArrayList Size :"+myArr.size()); 
       System.out.println("ArrayList2 Size :"+myArr2.size()); 
       repaint();  
      } 
     } 

     public void mouseReleased(MouseEvent e) 
     { 
      x2=e.getX(); 
      y2=e.getY(); 
      Shape shape = null; 
      if (buttonPress.buttonType.equals("Rectangle")) 
      { 
      // Rectangles cannot have a zero width or height 
       if (x1 != x2 || y1 != y2) 
       { 
        double width = Math.abs(x1 - x2); 
        double height = Math.abs(y1 - y2); 
        Rectangle2D.Double rect = new Rectangle2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height); 
        myArr.add(rect); 
        repaint(); 
       } 
      } 
      if (buttonPress.buttonType.equals("Eclipse")) 
      { 
       double width = Math.abs(x1 - x2); 
       double height = Math.abs(y1 - y2); 
       Ellipse2D.Double elli = new Ellipse2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height); 
       myArr.add(elli); 
       repaint(); 
      } 
      if (buttonPress.buttonType.equals("Lines")) 
      { 
       Line2D.Double nuLine = new Line2D.Double(x1, y1, x2, y2);  
       myArr.add(nuLine); 
       repaint();   
      } 
      if (buttonPress.buttonType.equals("Triangle")) 
      {/* 

       * 
       * 
       * 
       repaint(); */  
      } 
      if (buttonPress.buttonType.equals("FillRectangle")) 
      {    
       if (x1 != x2 || y1 != y2) 
       { 
        double width = Math.abs(x1 - x2); 
        double height = Math.abs(y1 - y2); 
        Rectangle2D.Double fillRect = new  Rectangle2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height); 
        myArr2.add(fillRect); 
        repaint(); 
       } 
      } 
      if (buttonPress.buttonType.equals("FillEclipse")) 
      {    
       double width = Math.abs(x1 - x2); 
       double height = Math.abs(y1 - y2); 
       Ellipse2D.Double fillElli = new Ellipse2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height); 
       myArr2.add(fillElli); 
       repaint(); 
      }  
     if (buttonPress.buttonType.equals("Freehand")) 
      {  
        System.out.println("test free"); 

       //* 
        //* 
        //*    
        repaint(); 
       //myArr2.add(nuLine2);                
      }    

      if (shape != null) 
      { 
       myArr.add(shape);  
       myArr2.add(shape); 
      } 
      repaint(); 
     }       
     } 
    //end of inner class 
     public DrawingPanel(ButtonPanel reference) 
     { 
     buttonPress = reference; 
     setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,2)); 
     addMouseListener(new Listener1()); 
     repaint();  
     }    
    } 

回答

1

我使用兩個解決此問題:

  • 一種解決方案是具有外部類保持作爲一類字段BufferedImage對象並在其的paintComponent方法顯示它,然後將內部類可以從BufferedImage中提取一個Graphics或Graphcis2D對象,然後繪製它,處理Graphics對象,並在外部類上調用repaint。
  • 另一種選擇是使用從內部類中填充的Point(或其他Shape)對象的類ArrayLists繪製外部類的paintComponent方法。在後一種情況下,內部類與Graphics對象無關,而是填充外部的數組列表並調用repaint。
+0

謝謝。第二個選項聽起來對我來說最合理。你知道這個的任何例子嗎? – mix2000 2011-05-02 10:41:35

-1

你可以把:

private Graphics2D g2d; 

由於面板的類成員並在第一時間的paintComponent被調用後,將有主圖形對象到內部類訪問。

+1

通過paintComponent獲取的圖形對象可能不會持續,因此這個解決方案充其量是非常脆弱的。 – 2011-04-27 11:58:29

+0

真的嗎?你可以只需要一個名爲thisPanel的字段,並在構造函數中分配thisPanel = this。然後使用thisPanel.getGraphics()? – Danish94 2011-04-27 18:34:52

+0

您不希望在Swing組件上使用getGraphics,原因很相似。相反,如上所述,使用BufferedImage或被動地繪製paintComponent方法。 – 2011-04-27 19:19:22