2012-01-02 51 views
0

我正試圖製作一個簡單的小程序,您可以通過在屏幕上拖動來繪製簡單形狀(通過右鍵菜單選擇形狀和顏色)。形狀實際上是在鼠標釋放時繪製的。現在,我的代碼:Java applet在處理mouseReleased事件時會拋出多個異常

public void init() 
    { 
     addMouseListener(this); 
     addMouseMotionListener(this); 

     figuri=new Vector(); 
     culori=new Vector(); 

     popup=new PopupMenu(); 
     Menu figura=new Menu("Figura"); 
     popup.add(figura); 


     for (int i=0;i<numeFigura.length;i++) 
     { 
      MenuItem mi=new MenuItem(numeFigura[i]); 
      mi.setActionCommand(numeFigura[i]); 
      mi.addActionListener(this); 
      figura.add(mi); 
     } 

     Menu culoare=new Menu("Culoare"); 
     popup.add(culoare); 

     for (int i=0;i<numeCuloare.length;i++) 
     { 
      MenuItem mi=new MenuItem(numeCuloare[i]); 
      mi.setActionCommand(numeCuloare[i]); 
      mi.addActionListener(this); 
      culoare.add(mi); 
     } 

     indexFiguri = 0; 
     indexCulori = 0; 

     mouseClickedStr = ""; 
     mousePressedStr = ""; 
     mouseDraggedStr = ""; 
     mouseReleasedStr= ""; 
     drawModeStr  = "Modul de desenare "+numeFigura[indexFiguri]; 
     drawColorStr = "culoarea "+numeCuloare[indexCulori]; 

     this.add(popup); 
} 
public void paint(Graphics g) 
    { 

     Graphics2D gg; 
     gg=(Graphics2D) g; 

     String drawMouseStr; 
     String drawMode; 

     drawMode = drawModeStr+" "+"folosind "+drawColorStr; 

     drawMouseStr = ""; 

     if(mouseClickedStr.length()>0) 
      drawMouseStr = mouseClickedStr+x_click+" "+y_click; 
     if(mousePressedStr.length()>0) 
      drawMouseStr = mousePressedStr+mouseX1+" "+mouseY1; 
     if(mouseDraggedStr.length()>0) 
      drawMouseStr = mouseDraggedStr+mouseX2+" "+mouseY2; 

// This is the call that throws the exception 
      if(mouseReleasedStr.length()>0){ 
      Color culoare; 
      switch (indexCulori){ 
       case 0: {culoare = Color.RED; break;} 
       case 1: {culoare = Color.GREEN; break;} 
       case 2: {culoare = Color.BLUE; break;} 
       case 3: {culoare = Color.YELLOW; break;} 
       case 4: {culoare = Color.BLACK; break;} 
      } 
      switch (indexFiguri){ 
       case 0: {figuri.addElement((new Line2D.Double(x_click,y_click, mouseX3-x_click,mouseY3-y_click))); break;} 
       case 1: {figuri.addElement((new Rectangle2D.Double(x_click,y_click,mouseX3-x_click,mouseY3-y_click))); break;} 
       case 2: {figuri.addElement((new Ellipse2D.Double(x_click,y_click,mouseX3-x_click, mouseY3-y_click))); break;} 
      } 
     } 


     if(drawMode.length()>0) 
     { 
      gg.setColor(Color.black); 
      gg.drawString(drawMode,10,20); 
     } 

     if(drawMouseStr.length()>0) 
     { 
      gg.setColor(Color.black); 
      gg.drawString(drawMouseStr,10,40); 
     } 

     for(int i=0;i<figuri.size();i++) 
     { 
      gg.setColor(culoare[((Integer)culori.get(i)).intValue()]); 
      gg.draw((Shape)figuri.elementAt(i)); 

      if (culori.get(i)!=null){ 
       gg.setColor(culoare[((Integer)culori.get(i)).intValue()]); 
       gg.fill((Shape)figuri.elementAt(i)); 
      } 
     } 
    } 

//And the function declaration. 
public void mouseReleased(MouseEvent me) 
    { 
     mouseX3=me.getX(); 
     mouseY3=me.getY(); 
     mouseReleasedStr= "Mouse released at "; 
     mouseClickedStr = ""; 
     mousePressedStr = ""; 
     mouseDraggedStr = ""; 

     repaint(); 
    } 

的拋出異常是:

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0 
    at java.util.Vector.get(Vector.java:694) 
    at events.paint(events.java:218) 
    at java.awt.Container.update(Container.java:1801) 
    at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239) 
    at sun.awt.RepaintArea.paint(RepaintArea.java:216) 
    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310) 
    at java.awt.Component.dispatchEventImpl(Component.java:4727) 
    at java.awt.Container.dispatchEventImpl(Container.java:2099) 
    at java.awt.Component.dispatchEvent(Component.java:4481) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643) 
    at java.awt.EventQueue.access$000(EventQueue.java:84) 
    at java.awt.EventQueue$1.run(EventQueue.java:602) 
    at java.awt.EventQueue$1.run(EventQueue.java:600) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) 
    at java.awt.EventQueue$2.run(EventQueue.java:616) 
    at java.awt.EventQueue$2.run(EventQueue.java:614) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

感謝您的耐心和幫助!

+1

一些提示(不是答案):使用一個包裝爲你的類(例如'package com.flolancu;'在你的文件的頂部,並創建一個目錄'com/flolancu /'爲你的java文件),並堅持命名約定,例如帶大寫的類名('events' - >'Events') – 2012-01-02 21:56:20

+0

@TheNail:+1。更重要的是,即使在下面的答案中提到的問題,我猜代碼仍然會有重大問題。 (我發現有兩個名爲'culoare'的變量;這是令人迷惑的!)任何一個_excellent_ Java IDE都可能會突出顯示許多這些簡單問題...... – 2012-01-02 22:09:31

+0

正如我所說的,我只是修改了一個現有的小程序,對我來說也是徹頭徹尾的混淆。更重要的是,我必須使用蹩腳的IDE(JCreator)在applet運行時看到控制檯(我不知道如何在Netbeans/Eclipse中執行此操作)。無論如何,謝謝大家的支持! :) – FloIancu 2012-01-02 22:14:47

回答

0

很難說肯定沒有在你的代碼行數,但只有在您調用Vector.get,它看起來是罪魁禍首少數病例:

for(int i=0;i<figuri.size();i++) 
    { 
     gg.setColor(culoare[((Integer)culori.get(i)).intValue()]); 
     gg.draw((Shape)figuri.elementAt(i)); 

     if (culori.get(i)!=null){ 
      gg.setColor(culoare[((Integer)culori.get(i)).intValue()]); 
      gg.fill((Shape)figuri.elementAt(i)); 
     } 
    } 

你迭代figuri也期待在culori。看起來您已將一個項目添加到figuri,該項目在culori中沒有相應的條目。

檢查並確保您添加了figuri項目的任何代碼也將正確的項目添加到culori

+0

我忘了我有兩個向量處理。這個任務實際上是基於我的教授的骨幹修改一個applet,我仍然在努力去理解它。 非常感謝你的回答和觀察的意見:) – FloIancu 2012-01-02 22:04:50

+0

改變你的教授! – 2012-01-02 22:12:08

0

我不明白你在哪裏保證兩個Vector是相同的長度;我猜實際上culorifiguri短(確實是空的)。 更重要的是,您在culori.get(i)的結果上使用了null的測試之後,您已將其投射到Integer並對其調用方法。這不會以你期望的方式工作!

你可能要考慮不使用Vector而是使用ArrayList,你肯定寫你的代碼,以便符合Java 5中(即使用泛型)如果在所有可能的。 (畢竟,它已經超過7年了...)