2016-04-13 1 views
1

私は現在、デバイスのIPアドレスが下に表示されている描画キャンバスJFrameを作成しようとしています(別のデバイスが同じJFrame後の段階で)、私は現在、いくつかの問題だけでなく、タイトルの付いたエラーを受けています。スレッド "AWT-EventQueue-0"と奇妙なTextAreaの例外

問題: - AWTエラー(私は仮定し、これは、これを原因とIP電話である) - 私はJFrameのを最小限に抑え、再び開いた後JTextAreaには、黒いバーとして、表示された - 私は再び黒これを行う場合バーが消えて、最後に私が望むIPが表示されます

"ペインティング"の実行に関する第2の問題があるとします。私はコードを使いこなしていましたが、現在の段階では巨大な結び目のように感じられます。リストされた問題を解決し、いくつかのコントロールを取り戻すための助けは、非常に高く評価されます!

public class CanvasFrame extends JPanel { 

    ArrayList<Point> location = new ArrayList<Point>(); 

    JTextArea consoleOutput = new JTextArea(1,20); 

    public void addComponentToPane(Container pane) { 
     consoleOutput.setEditable(false); 
    } 

    public CanvasFrame() { 
     addMouseListener(new MouseAdapter() { 
      @Override 
      public void mousePressed(MouseEvent e) { 
       location.clear(); 
       location.add(e.getPoint()); 
      } 
     }); 

     addMouseMotionListener(new MouseMotionAdapter() { 
      @Override 
      public void mouseDragged(MouseEvent e) { 
       location.add(e.getPoint()); 
       repaint(); 
      } 
     }); 
     setPreferredSize(new Dimension(800, 500)); 
     setBackground(Color.WHITE); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     Point p = location.get(0); 
     for (int i = 1; i < location.size(); i++) { 
      Point q = location.get(i); 
      g.drawLine(p.x, p.y, q.x, q.y); 
      p = q; 
     } 
    } 

    public static void main(String[] args) throws Exception { 

     InetAddress SERVERIP = InetAddress.getLocalHost(); 

     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 

       JFrame frame = new JFrame("Drawing with friends"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new CanvasFrame(), BorderLayout.CENTER); 

       JTextArea IPadress = new JTextArea(1,20); 
       IPadress.setEditable(false); 
       IPadress.append("DEVICE IP: " + SERVERIP.getHostAddress()); 
       frame.add(IPadress, BorderLayout.SOUTH); 

       frame.setSize(new Dimension(800,600)); 
       frame.setLocationRelativeTo(null); 
       frame.setResizable(false); 
       frame.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(runnable); 
    } 
} 

コンプリートエラー:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.rangeCheck(Unknown Source) 
    at java.util.ArrayList.get(Unknown Source) 
    at drawingwf.CanvasFrame.paintComponent(CanvasFrame.java:56) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JLayeredPane.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
    at javax.swing.RepaintManager.paint(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 
    at java.awt.Container.paint(Unknown Source) 
    at java.awt.Window.paint(Unknown Source) 
    at javax.swing.RepaintManager$4.run(Unknown Source) 
    at javax.swing.RepaintManager$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.access$1200(Unknown Source) 
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.rangeCheck(Unknown Source) 
    at java.util.ArrayList.get(Unknown Source) 
    at drawingwf.CanvasFrame.paintComponent(CanvasFrame.java:56) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JLayeredPane.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
    at javax.swing.RepaintManager.paint(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 
    at java.awt.Container.paint(Unknown Source) 
    at java.awt.Window.paint(Unknown Source) 
    at javax.swing.RepaintManager$4.run(Unknown Source) 
    at javax.swing.RepaintManager$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.access$1200(Unknown Source) 
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
+1

あなたは完全なエラーに –

+0

'点P = location.get(0を投稿することができます

チェックlocation0インデックスにアクセスする前に、空であるかどうか); '' paintComponent'中で 'java.lang.IndexOutOfBoundsException:Index:0、Size:0'が発生しています – MadProgrammer

+0

@FastSnail added complete error – WONDERGG

答えて

1

location変数がmousePressedまたはmouseDraggedの呼び出しの前paintComponentに空である

はここに私のクラスです。これに

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    // I add this 
    if(location.isEmpty()) 
     return; 

    Point p = location.get(0); 
    for (int i = 1; i < location.size(); i++) { 
     Point q = location.get(i); 
     g.drawLine(p.x, p.y, q.x, q.y); 
     p = q; 
    } 
} 
1

てみ変更:

protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Point p; 
    try { 
    p = location.get(0); 
    } catch (IndexOutOfBoundsException e) { 
     p = new Point(0, 0); 
    } 
    for (int i = 1; i < location.size(); i++) { 
     Point q = location.get(i); 
     g.drawLine(p.x, p.y, q.x, q.y); 
     p = q; 
    } 
関連する問題