import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class MuseumPanel extends JPanel
{
private JLabel label;
private JPanel buttonPanel;
private JButton week, day, hour, output;
private Museum museumArray = new Museum();
private int numb;
int[] weekArray = new int[3];
int[] dayArray = new int[7];
int[] hourArray = new int[4];
public MuseumPanel() throws IOException
{
week = new JButton("Week");
day = new JButton("Day");
hour = new JButton("Hour");
output = new JButton("Output");
ButtonListener listener = new ButtonListener();
week.addActionListener (listener);
day.addActionListener (listener);
hour.addActionListener (listener);
output.addActionListener (listener);
label = new JLabel ("Click to see the amount of visitors for each button.");
buttonPanel = new JPanel();
buttonPanel.setPreferredSize (new Dimension(650,40));
buttonPanel.setBackground (Color.blue);
buttonPanel.add (week);
buttonPanel.add (day);
buttonPanel.add (hour);
buttonPanel.add (output);
setPreferredSize (new Dimension(650,500));
setBackground (Color.yellow);
add (label);
add (buttonPanel);
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);
if(numb==1)
drawWeek(page);
else if(numb==2)
drawWeek(page);
else if(numb==3)
drawWeek(page);
else if(numb==4)
drawWeek(page);
}
/*public void drawOutput(Graphics page) throws IOException
{
page.setFont(new Font("Arial", Font.BOLD,40));
page.drawString("**Output File Created**",100,250);
}*/
public void drawWeek(Graphics page)
{
page.setColor(Color.blue);
int top=93;
int width = 93;
for(int i=1; i<4; i++)
{
weekArray = museumArray.getWeeks(i);
page.fillRect(top,450-weekArray[i], width, weekArray[i]);
page.setColor(Color.red);
page.drawString("" + weekArray[i], top+35, 450-weekArray[i]-5);
page.setColor(Color.blue);
page.drawString("Week " + (i+1), top+30, 470);
top = top + (width + 93);
}
}
/*public void drawDay(Graphics page)
{
String[] days = new String[]{"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};
page.setColor(Color.blue);
int top=43;
int width=43;
for(int i=0; i<7; i++)
{
dayArray = museumArray.getDays(i);
page.fillRect(top,450-dayArray[i], width, dayArray[i]);
page.setColor(Color.red);
page.drawString("" + dayArray[i], top+10, 450-dayArray[i]-5);
page.setColor(Color.blue);
page.drawString(days[i], top, 470);
top = top + (width + 43);
}
}*/
/*public void drawHour(Graphics page)
{
page.setColor(Color.blue);
int top=72;
int width=72;
for(int i=0; i<4; i++)
{
hourArray = museumArray.getHours(i);
page.fillRect(top,450-hourArray[i], width, hourArray[i]);
page.setColor(Color.red);
page.drawString("" + hourArray[i], top+25, 450-hourArray[i]-5);
page.setColor(Color.blue);
if(i==0)
page.drawString("12PM-1PM", top+5, 470);
else if (i==1)
page.drawString("1PM-2PM", top+5, 470);
else if (i==2)
page.drawString("2PM-3PM", top+5, 470);
else
page.drawString("3PM-4PM", top+5, 470);
top = top + (width + 72);
}
}*/
/*public void output() throws IOException
{
final FileWriter outputFile = new FileWriter("OUTPUT_Duguay");
final BufferedWriter OutputBuffer = new BufferedWriter(outputFile);
final PrintWriter out = new PrintWriter(OutputBuffer);
weekArray = museumArray.getHours();
dayArray = museumArray.getDays();
hourArray = museumArray.getHours();
for(int i=0; i<3; i++)
out.println("Week " + (i+1) + ":" + weekArray[i]);
for(int i=0; i<7; i++)
out.println("Day " + (i+1) + ":" + dayArray[i]);
for(int i=0; i<4; i++)
out.println("Hour " + (i+1) + ":" + hourArray[i]);
out.close();
}*/
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if (source == week)
{
numb = 1;
repaint();
}
else if (source == day)
{
numb = 2;
repaint();
}
else if (source == hour)
{
numb = 3;
repaint();
}
else
{
numb = 4;
//output();
repaint();
}
}
}
}
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
public class Museum
{
private final int WEEK = 3;
private final int DAY = 7;
private final int HOUR = 4;
private int[][][] visitors = new int[WEEK][DAY][HOUR];
private int[] totalWeeks;
private int[] totalDays;
private int[] totalHours;
public Museum() throws FileNotFoundException
{
Scanner scan = new Scanner(new File("Museum_name.txt"));
for(int w = 0; w < WEEK-1; w++)
{
for(int d = 0; d < DAY-1; d++)
{
for(int h = 0; h < HOUR-1; h++)
{
visitors [w][d][h] = scan.nextInt();
}
}
}
}
public int[] getWeeks( int n)
{
totalWeeks = new int[1];
for(int t = n-1; t < n; t++)
for(int j = 0; j < DAY; j++)
for(int k=0; k < HOUR; k++)
totalWeeks[t] += visitors[t][j][k];
return totalWeeks;
}
/* public int[] getDays( int n)
{
totalDays = new int[1];
for(int t = n-1; t < n; t++)
for(int j = 0; j < DAY; j++)
for(int k=0; k < HOUR; k++)
totalDays[t] += visitors[t][j][k];
return totalDays;
}
public int[] getHours( int n)
{
totalHours = new int[1];
for(int t = n-1; t < n; t++)
for(int j = 0; j < DAY; j++)
for(int k=0; k < HOUR; k++)
totalDays[t] += visitors[t][j][k];
return totalHours;
}*/
}
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.JFrame;
public class MainClass
{
public static void main (String[] args) throws IOException
{
JFrame frame = new JFrame ("Flow Of Visitors Into Museum");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MuseumPanel());
frame.pack();
frame.setVisible(true);
}
}
I have the above code, but I keep getting a java.lang.ArrayIndexOutOfBoundsException: 1. My text file just contains 84 integers.
This is what the code is supposed to do: Design and implement an application that helps a museum analyze the flow of visitors for the exhibition that
lasts three weeks. Museum is opened all 7 days per week for 4 hours from 12PM - 4PM. A text input file
"Museum_YourLastName.txt" contains 84 integers (one per line, each is single digit) that represents the
number of visitors that entered the exhibition during each hour that museum was opened in chronological
order. Read the information and store it in a three dimensional array visitors. Design methods to analyze
the array. Have three buttons to display analysis per WEEK, per DAY and per HOUR. Analysis per WEEK should
display three rectangles in blue color. All have the same width and their bottom line is on the same level. The
height of n-th rectangle will correspond to total number of visitors in n-th week. (For analysis per week design
a method to calculate a total number of visitors for given week). To help understand the bar chart write word
week and appropriate number n below each bar. Also write the total number of visitors above the bar. Use
one listener for all three buttons.
Analysis per DAY should display seven rectangles that visualize total number of visitors for each of the seven
days. Analysis per HOUR should display four rectangles that visualize the total number of visitors for each of
the four hours when museum was opened. Use same style as specified for week analysis.
Add fourth button that will create an output file "OUTPUTyourLastName.txt" with 14 lines displaying total
visitors for week1, week2, week3, day1, day2, ...., day7, hour1, hour2, hour3, and hour4.
Any idea?