I am new to this website so please forgive me if I have broken any rules. Iv'e been looking for a couple days and have not found an answer to this, please let me know if this has been answered before.
I am creating a java program that can draw an image, (a rectangle in this instance), based on data inside of a file. I have come across an error that I am unable to fix or find a solution for.
this is the main class file where I run everything.
package pkg9_weeks_project;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
public class Map {
public static void main(String[] args) throws FileNotFoundException {
JFrame window = new JFrame();
window.setSize(640, 640);
window.setTitle("this is a frame of J's, otherwise known as a JFrame");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
drawningcomponent DC = new drawningcomponent();
window.add(DC);
drawningcomponent.readinmapdata(); //this is to make sure it is reading the file in
}
}
this is the file that reads in and adds the datafile's data into the ArrayList and... well.. tries to draw the rectangle with the data inside of the ArrayList
package pkg9_weeks_project;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
public class drawningcomponent extends JComponent
{
public static ArrayList<String> maptiles2 ;
public static void readinmapdata() throws FileNotFoundException
{
maptiles2 = new ArrayList<String>();
Scanner inputFile = new Scanner(new File("./src/MapData"));
while(inputFile.hasNextLine())
{
String line = inputFile.nextLine();
String data[] = line.split("");
String tile1 = data[0].trim();
String tile2 = data[1].trim();
String tile3 = data[2].trim();
String tile4 = data[3].trim();
maptiles2.add(tile1);
maptiles2.add(tile2);
maptiles2.add(tile3);
maptiles2.add(tile4);
(This was a test to make sure the items were added
and could be printed from the ArrayList)
System.out.println(maptiles2.get(0));
System.out.println(maptiles2.get(1));
System.out.println(maptiles2.get(2));
System.out.println(maptiles2.get(3));
}
System.out.println("SUCCESS READING IN FILE");
}
(This is where the program is coming up with an error)
(I tested the graphics to make sure you could draw a )
(rectangle beforehand and it worked perfectly)
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Rectangle rect1 = new Rectangle(5,5,100,200);
if(maptiles2.get(1).contains("X"))
{
g2.draw(rect1);
}
if(maptiles2.get(1).contains("O"))
{
g2.draw(rect1);
}
}
}
And last but not least, this is the file I am trying to read in(The file is called MapData)
XOXX
When I run the class Map, I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
X
O
X
X
Success Reading in File
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at pkg9_weeks_project.drawningcomponent.paintComponent(drawningcomponent.java:60)
at javax.swing.JComponent.paint(JComponent.java:1053)
at javax.swing.JComponent.paintChildren(JComponent.java:886)
at javax.swing.JComponent.paint(JComponent.java:1062)
at javax.swing.JComponent.paintChildren(JComponent.java:886)
at javax.swing.JComponent.paint(JComponent.java:1062)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:886)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5230)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java: 1572)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1495)
at javax.swing.RepaintManager.paint(RepaintManager.java:1265)
at javax.swing.JComponent.paint(JComponent.java:1039)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116)
at java.awt.Container.paint(Container.java:1973)
at java.awt.Window.paint(Window.java:3901)
at javax.swing.RepaintManager$4.run(RepaintManager.java:835)
at javax.swing.RepaintManager$4.run(RepaintManager.java:807)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:7 5)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731)
at javax.swing.RepaintManager.access$1300(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:7 5)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201 )
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105 )
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I may be wrong but, I have a feeling when it says: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 it means there are no longer any items in the arraylist maptiles2, but I have no idea at this point and am very confused. For all who respond, thank you for the replies, they are greatly appreciated!
EDIT: Thank you all for answering, the answer was a simple mistake involving running the method readinmapdata after everything else ran. thank you @amit farag and @svarog for your helpful answers!
it may be not correct but have you called to the readinmapdata()
before you called repaint()
?
it might cause this problem