处理mouseReleased事件时,Java applet会抛出多个异常

时间:2012-01-02 21:48:56

标签: java exception applet

我正在尝试制作一个简单的小程序,您可以通过在屏幕上拖动来绘制简单的形状(通过右键单击菜单选择形状和颜色)。该形状实际上是在鼠标释放时绘制的。现在我的代码:

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)

感谢您的耐心和帮助!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我不知道你保证两个Vector的长度是一样的;我猜测实际上culorifiguri短(实际上它是空的)。 更重要的是,您将null 的结果用于culori.get(i)之后,将投射到Integer并在其上调用方法。这不符合您的预期!

您可能要考虑不使用Vector,而是使用ArrayList,而肯定想要编写代码以便符合Java 5(即如果可能的话,使用泛型)。 (毕竟,它已经超过7年了......)