I’m trying to make a simple applet where you can draw simple shapes (the shape and the color are selected via right-click menu) by dragging on the screen. The shape is actually drawn on mouse release. Now my code:
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();
}
The exceptions thrown are:
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)
Thank you for your patience and your help!
It’s hard to say for sure without line numbers in your code, but there are only a few cases where you call
Vector.getso this looks like the culprit:You are iterating over
figuribut also looking up values inculori. It looks like you have added an item tofigurithat does not have a corresponding entry inculori.Check and make sure that whatever code you have that adds items to
figurialso adds the right items toculori.