Results 1 to 3 of 3
Thread: IndexOutOfBoundsException
- 05-17-2011, 02:19 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 24
- Rep Power
- 0
IndexOutOfBoundsException
Ok so I'm trying to create a drawing program, and it was working fine until I got a random error, I can't find what I messed up and I keep getting an error at nt point1 = (pointList.get(k)); Here is my whole code:
The ArrayList is 4 by 4, yet the error states:Java Code:class MyPanel extends JPanel { private int last_x; private int last_y; private int x; private int y; private Graphics g; private int radiusX; private int radiusY; private int click_x; private int click_y; private String mouseString; public MyPanel(ArrayList pointList, ArrayList colorList, ArrayList eraserList, ArrayList overlapList, ArrayList rectangleList) { setBorder(BorderFactory.createLineBorder(Color.black)); this.pointList = pointList; this.colorList = colorList; this.eraserList = eraserList; this.overlapList = overlapList; this.rectangleList = rectangleList; defaultColor = new Color(238, 238, 238); mouseString = "default"; addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { last_x = e.getX(); last_y = e.getY(); } } ); addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); mouseString = "Pressed"; repaint(); } } ); addMouseMotionListener(new MouseAdapter() { public void mouseRelease(MouseEvent e) { mouseString = "Released"; } } ); } public Dimension getPreferredSize() { return new Dimension(250,200); } protected void paintComponent(Graphics g) { super.paintComponent(g); int k = 0; int j = 0; int n = 0; int m = 0; int p = 0; String eraserString12 = (String) setEraser1(); if (eraserString12.equals("Lines") && mouseString.equals("Pressed")) { pointList.add(last_x); pointList.add(last_y); pointList.add(x); pointList.add(y); colorList.add(setColor1()); overlapList.add(1); } else if (eraserString12.equals("Eraser") && mouseString.equals("Pressed")) { eraserList.add(last_x); eraserList.add(last_y); overlapList.add(2); } setBackgroundColor(); g.drawString("This is my custom Panel!",10,20); int overlapSize = (int) overlapList.size(); last_x = x; last_y = y; while (m<overlapSize) { int overlap = overlapList.get(m); if (overlap==1) { System.out.println(pointList); int point1 = (pointList.get(k)); int point2 = ( pointList.get(k+1)); int point3 = (pointList.get(k+2)); int point4 = (pointList.get(k+3)); g.setColor(colorList.get(j)); g.drawLine(point1, point2, point3, point4); g.setColor(defaultColor); k = k+4; j = j+1; } if (overlap==2) { int circleX = eraserList.get(n); int circleY = eraserList.get(n+1); Color a = ColorBox.getBackground1(); if (a==Color.darkGray) { g.setColor(defaultColor); } else if (a!=Color.darkGray) { g.setColor(a); } g.fillOval(circleX-15, circleY-15, 30, 30); n = n+2; } } m++; } public Color setColor1() { Color color = ColorBox.getColor(); return color; } public void setBackgroundColor() { Color a = ColorBox.getBackground1(); if (a==Color.darkGray) { setBackground(defaultColor); } else if (a!=Color.darkGray) { setBackground(a); } } public String setEraser1() { String eraserString = ColorBox.getEraser1(); return eraserString; } public void Clear() { pointList.clear(); colorList.clear(); eraserList.clear(); overlapList.clear(); rectangleList.clear(); ColorBox.setBackgroundDefault(); setBackground(defaultColor); repaint(); } private ArrayList<Integer> pointList; private ArrayList<Color> colorList; private ArrayList<Integer> eraserList; private ArrayList<Integer> overlapList; private ArrayList<Integer> rectangleList; private Color defaultColor; }
So I'm stuck, any suggestions/tips?Java Code:Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at MyPanel.paintComponent(Scribble.java:137) at javax.swing.JComponent.paint(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._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.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)
-
Your error is not random at all -- you're trying to get an item out of an array list that is beyond the size of the array list. In that loop you check the value of m but don't check whether or not k or k+3 is < the size of the pointList.
There are other poblems with your code including trying to do program logic from within a paint/paintComonent method.
- 05-17-2011, 02:29 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
IndexOutOfBoundsException when jDialog.setVisible(true)
By madcloud in forum AWT / SwingReplies: 4Last Post: 12-27-2010, 03:43 PM -
java.lang.IndexOutOfBoundsException: Invalid location 1, size is 2
By biswajithit in forum AndroidReplies: 3Last Post: 09-27-2010, 07:07 AM -
IndexOutOfBoundsException
By Luciform in forum New To JavaReplies: 8Last Post: 05-24-2010, 03:44 AM -
IndexOutOfBoundsException with AWT-EventQueue-0
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 03-10-2009, 01:52 PM -
IndexOutOfBoundsException
By aldo1987 in forum New To JavaReplies: 6Last Post: 04-30-2008, 03:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks