単純な図形を描くことができるシンプルなアプレットを作成しようとしています(シェイプと色は右クリックメニューで選択します)。シェイプは実際にマウスを放すと描画されます。今、私のコード:mouseReleasedイベントを処理するときにJavaアプレットが複数の例外をスローする
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)
はあなたの忍耐とあなたの助けをありがとう!
ヒント(答えではありません):クラスのパッケージを使用してください(例:ファイルの先頭に 'package com.flolancu;')。あなたのjavaファイルの 'com/flolancu /'ディレクトリ)、大文字のクラス名( 'events' - >' Events')などの命名規則に従います。 –
@TheNail:+1。さらに、以下の回答に記載されている問題でも、コードには依然として重大な問題があると思います。 (私は 'culoare'という名前の2つの変数があることを知っています;それは混乱する_bound_です)_excellent_ Java IDEのどれかは、おそらくこれらの単純な問題の多くを真っ直ぐに強調しているでしょう。 –
存在しているアプレット、それはまったく私にも混乱しています。さらに、アプレットが実行されている間にコンソールを表示するには、奇妙なIDE(JCreator)を使用する必要があります(Netbeans/Eclipseでこれを行う方法はわかりません)。とにかく、ありがとうございます。 :) – FloIancu