I created a class that takes a 3 dimensional array and puts it in a txt file, the method is called output(). I can't figure out where to call the method though. I'm trying to have it so it is called when numb=4 in the actionListener on the bottom.
Please Help! Leaving for Afghanistan in a few hours!
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)
drawDay(page);
else if(numb==3)
drawHour(page);
else if(numb==4)
drawOutput(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=0; i<3; i++)
{
weekArray = museumArray.getWeeks();
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();
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();
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();
}
}
}
}