transfer arrays between classes
hey,
i'm having problems transferring a 1D array from one class to another. i get the information using a class called score with GUI. but when i try to access the values in my main part of the program which is "MemoryMatch" the values are not there. but in the score class file the values are there.
the program is an assignment called memory game. the 18 images are 40*40 pics which are supposed to be matched
//my main program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class MemoryMatchextends JFrame
{
MyButton[] button =new MyButton [36];
ImageIcon[] icons =new ImageIcon [button.length];
int buttonShowingIndex = -1;
int time = 0;
String[] players ={"","","","",""};
//delay method
publicstaticvoid delay (int num)
{
try
{
Thread.sleep (num);
}
catch (InterruptedException e)
{
}
}
public MemoryMatch ()
{
icons [0] =new ImageIcon (getClass ().getResource ("image1.gif"));
icons [1] =new ImageIcon (getClass ().getResource ("image2.gif"));
icons [2] =new ImageIcon (getClass ().getResource ("image3.gif"));
icons [3] =new ImageIcon (getClass ().getResource ("image4.gif"));
icons [4] =new ImageIcon (getClass ().getResource ("image5.gif"));
icons [5] =new ImageIcon (getClass ().getResource ("image6.gif"));
icons [6] =new ImageIcon (getClass ().getResource ("image7.gif"));
icons [7] =new ImageIcon (getClass ().getResource ("image8.gif"));
icons [8] =new ImageIcon (getClass ().getResource ("image9.gif"));
icons [9] =new ImageIcon (getClass ().getResource ("image10.gif"));
icons [10] =new ImageIcon (getClass ().getResource ("image11.gif"));
icons [11] =new ImageIcon (getClass ().getResource ("image12.gif"));
icons [12] =new ImageIcon (getClass ().getResource ("image13.gif"));
icons [13] =new ImageIcon (getClass ().getResource ("image14.gif"));
icons [14] =new ImageIcon (getClass ().getResource ("image15.gif"));
icons [15] =new ImageIcon (getClass ().getResource ("image16.gif"));
icons [16] =new ImageIcon (getClass ().getResource ("image17.gif"));
icons [17] =new ImageIcon (getClass ().getResource ("image18.gif"));
icons [18] = icons [0];
icons [19] = icons [1];
icons [20] = icons [2];
icons [21] = icons [3];
icons [22] = icons [4];
icons [23] = icons [5];
icons [24] = icons [6];
icons [25] = icons [7];
icons [26] = icons [8];
icons [27] = icons [9];
icons [28] = icons [10];
icons [29] = icons [11];
icons [30] = icons [12];
icons [31] = icons [13];
icons [32] = icons [14];
icons [33] = icons [15];
icons [34] = icons [16];
icons [35] = icons [17];
/* Create and add a second prompt and then a text field */
Collections.shuffle (Arrays.asList (icons));
setDefaultCloseOperation (EXIT_ON_CLOSE);
JPanel mainPanel;
mainPanel =new JPanel ();
mainPanel.setBorder (BorderFactory.createEmptyBorder (10, 10, 200, 10));
mainPanel.setBackground (Color.white);
mainPanel.setLayout (new GridLayout (6, 6));
for (int x = 0 ; x < button.length ; x++)
{
button [x] =new MyButton (icons [x], x);
mainPanel.add (button [x]);
}
getContentPane ().add (mainPanel);
pack ();
for (int i = 0 ; i < 5 ; i++)
{
System.out.println (players [i]);
}
}
class MyButtonextends JButton
{
int index;
ImageIcon icon;
public MyButton (ImageIcon img,int i)
{
icon = img;
index = i;
setPreferredSize (new Dimension (100, 100));
addActionListener (new ActionListener ()
{
publicvoid actionPerformed (ActionEvent actionEvent)
{
if (getIcon () ==null)
{
setIcon (icon);
if (buttonShowingIndex > -1)
{
if (icon != button [buttonShowingIndex].getIcon ())
{
finalint bsi = buttonShowingIndex;
ActionListener al =new ActionListener ()
{
publicvoid actionPerformed (ActionEvent actionEvent)
{
MyButton.this.setIcon (null);
button [bsi].setIcon (null);
}
}
;
javax.swing.Timer timer =new javax.swing.Timer (450, al);
timer.setRepeats (false);
timer.start ();
}
buttonShowingIndex = -1;
}
else
buttonShowingIndex = index;
time++;
}
}
}
);
}
}
publicstaticvoid main (String[] args)
{
//score
JFrame.setDefaultLookAndFeelDecorated (true);
score myGrades =new score ();
//score
delay (15000);
//running of main game
new MemoryMatch ().setVisible (true);
}
}
//the score class
// The "score" class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass scoreimplements ActionListener
{
JFrame frame;
JPanel contentPane;
JLabel prompt1, prompt2, prompt4, prompt6, prompt8, average, prompt9;
JTextField grade1, grade2, grade3, grade4, grade5, gender, gender2, gender3, gender4, gender5;
JButton avgButton;
static String[] players ={"","","","",""};
//delay method
publicstaticvoid delay (int num)
{
try
{
Thread.sleep (num);
}
catch (InterruptedException e)
{
}
}
public score ()
{/* Create and set up the frame */
frame =new JFrame ("Semester Average");
/* Create a content pane with a GridLayout and empty borders */
contentPane =new JPanel ();
contentPane.setLayout (new GridLayout (0, 2, 10, 5));
contentPane.setBorder (BorderFactory.createEmptyBorder (10, 10, 10, 10));
// 1
/* Create and add a prompt and then a text field */
prompt1 =new JLabel ("Player 1 please enter Your name ");
contentPane.add (prompt1);
grade1 =new JTextField (10);
contentPane.add (grade1);
// 1
// 2
/* Create and add a prompt and then a text field */
prompt2 =new JLabel ("Player 2 please enter Your name ");
contentPane.add (prompt2);
grade2 =new JTextField (10);
contentPane.add (grade2);
// 2
// 3
/* Create and add a prompt and then a text field */
prompt4 =new JLabel ("Player 3 please enter Your name ");
contentPane.add (prompt4);
grade3 =new JTextField (10);
contentPane.add (grade3);
// 3
// 4
/* Create and add a prompt and then a text field */
prompt6 =new JLabel ("Player 4 please enter Your name ");
contentPane.add (prompt6);
grade4 =new JTextField (10);
contentPane.add (grade4);
// 4
// 5
/* Create and add a prompt and then a text field */
prompt8 =new JLabel ("Player 5 please enter Your name ");
contentPane.add (prompt8);
grade5 =new JTextField (10);
contentPane.add (grade5);
/* Create and add a prompt and then a text field */
prompt9 =new JLabel ("The game window will load in 10 seconds ");
contentPane.add (prompt9);
/* Create and add button that will display the average of the grades */
avgButton =new JButton ("Enter");
avgButton.setActionCommand ("Enter");
avgButton.addActionListener (this);
contentPane.add (avgButton);
/* Create and add a label that will display the average */
average =new JLabel (" ");
average.setBorder (BorderFactory.createEmptyBorder (10, 0, 10, 0));
contentPane.add (average);
/* Add content pane to frame */
frame.setContentPane (contentPane);
/* Size and then display the frame. */
frame.pack ();
frame.setVisible (true);
}
/**
* Handle button click action event
* pre: none
* post: The average of the grades entered has been calculated and displayed.
*/
publicvoid actionPerformed (ActionEvent event)
{
String eventName = event.getActionCommand ();
if (eventName.equals ("Enter"))
{
String g1 = grade1.getText ();
String g3 = grade2.getText ();
String g5 = grade3.getText ();
String g7 = grade4.getText ();
String g9 = grade5.getText ();
for (int i = 0 ; i < 5 ; i++)
{
if (i == 0)
{
players [i] = g1;
if (g3 =="")
JFrame.setDefaultLookAndFeelDecorated (false);
}
elseif (i == 1)
{
players [i] = g3;
if (g5 =="")
JFrame.setDefaultLookAndFeelDecorated (false);
}
elseif (i == 2)
{
players [i] = g5;
if (g5 =="")
JFrame.setDefaultLookAndFeelDecorated (false);
}
elseif (i == 3)
{
players [i] = g7;
if (g7 =="")
JFrame.setDefaultLookAndFeelDecorated (false);
}
elseif (i == 4)
{
players [i] = g9;
JFrame.setDefaultLookAndFeelDecorated (false);
}
}
}
}
/**
* Create and show the GUI.
*/
privatestatic String[] runGUI ()
{
JFrame.setDefaultLookAndFeelDecorated (true);
score myGrades =new score ();
return players;
}
publicstatic String[] main (String[] args)
{
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
publicvoid run ()
{
runGUI ();
delay (1000);
}
}
);
return players;
}
}

