Alright, I'm trying to import data from a text file into an array, the data represents different properties of buildings that are to be drawn on a panel. I have 3 classes, Building3 which represents an individual building, CityPanel3 which represents the panel that the buildings are drawn upon (also where i try importing the data into the array), and NightCity3 which has the main method and adds the panel to a frame.
When i try and compile my CityPanel3 class, i keep getting an excepion stating "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" at the line of code "Scanner scan = new Scanner(new File("buildingData.txt"));
I try adding simple Try and Catch block statements and only get 30 something more errors that make no sense (They make no sense because this program works with an arrayList without importing data from a file) However, i need to do it in a way of importing the data from a text file.
Any help would be greatly appreciated
Here iss the code that i have so far of the 3 classes (CityPanel3 is the one where the exception is occuring)
import java.awt.*;
import java.util.*;
public class Building3
{
private int width, height, x;
private Color color;
public Building3(int wWidth, int hHeight, int upperX, Color shade)
{
width = wWidth;
height = hHeight;
x = upperX;
color = shade;
}
public void draw (Graphics page)
{
page.setColor (color);
page.fillRect (x, 420 - height, width, height);
drawWindows (page);
}
public void setWidth (int wWidth)
{
width = wWidth;
}
public void setHeight (int hHeight)
{
height = hHeight;
}
public void setColor (Color shade)
{
color = shade;
}
public void setX (int upperX)
{
x = upperX;
}
public int getWidth ()
{
return width;
}
public int getHeight ()
{
return height;
}
public Color getColor ()
{
return color;
}
public int getX ()
{
return x;
}
public void drawWindows(Graphics page)
{
page.setColor(Color.yellow);
Random generator = new Random();
int rand = generator.nextInt(6) + 2;
int numWindows = 0;
int windowSize = 20;
int y1 = (420 - height);
int startingY = (y1 + 10);
while (numWindows <= rand && startingY < 390)
{
int x1 = x;
int xRand = generator.nextInt(width - 40) + (x1 + 10);
page.fillRect(xRand, startingY, windowSize, windowSize);
numWindows++;
startingY = startingY + (windowSize + 10);
}
}
}
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
public class CityPanel3 extends JPanel
{
private Building3 temporaryBuildings;
private Color gray = new Color (128,128,128);
String line;
Scanner scanToken;
Scanner scan = new Scanner(new File("buildingData.txt"));
Building3[] buildingList = new Building3[7];
int width;
int height;
int x;
Color color;
int cValue;
int size = 0;
Scanner choose = new Scanner (System.in);
public CityPanel3 ()
{
//*************************************************
// Parallel arrays assignvalues to the array list
//*************************************************
while (scan.hasNext())
{
line = scan.nextLine();
scanToken = new Scanner(line);
scanToken.useDelimiter(" ");
width = scanToken.nextInt();
height = scanToken.nextInt();
x = scanToken.nextInt();
cValue = scanToken.nextInt();
color = new Color (cValue,cValue,cValue);
buildingList[size] = new Building3 (width, height, x, color);
size++;
}
setPreferredSize (new Dimension(800,500));
setBackground (Color.black);
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);
for(int count=0;count<7;count++)
{
buildingList[count].draw(page);
}
}
}
import javax.swing.*;
import java.awt.*;
public class NightCity3
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("NightCity3");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CityPanel3());
frame.pack();
frame.setVisible(true);
}
}
Thanks again for any help