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;

}

}

[18390 byte] By [Nadal_Federera] at [2007-11-27 6:13:52]
# 1

Hi,

Can you be a bit more specific and point out exactly where you are having a problem and what you have tried?

People are usually put off by a lot of code like this although sometimes it is needed. Can you post simply where you are calling from and how and what you are calling and also where the array is that you are calling in another post?

I think you will get an answer more quickly.

Cheers

_helloWorld_a at 2007-7-12 17:22:49 > top of Java-index,Java Essentials,Java Programming...
# 2

hey man,

the problem for me is that i cant transfer the array named 損layers?in "Score" class to the "MemoryMatch" class. (it would also be alrite if i could just transfer the array values from the"score" class to another array in the "MemoryMatch" class )

this is my first year doing java so i dont know everything. the thing is, in the score class i get the names of the people playing the game in the variables such as "g1" and "g3" and "g5" and so on. then i put these values into the 1D array 損layers?which is in the "score" class. the problem is that i cant transfer the array or the values of the array from the "score" class to the"MemoryMatch" class.

i've been trying everything i know to try and get the 損layers?array with the values such as the names of the people playing the game into the "MemoryMatch" class. i thought if i returned the array in the "score" class i would get the 損layers?array with the values in the "MemoryMatch" class. but it did'nt happen. Because everytime i printed out the values of the 損layers?array in the "MemoryMatch" class they were empty but when i printed out the values of the array

( after i input the names into the array) in the score class the names were there.

as you might have noticed for the last part (plz look at the code right at the end of the score class). after it says ?br>

/**

* Create and show the GUI.

*/

"

i tried to return the array names 損layers?but that did not happen.

what i had before i had the array trouble was ::::

this part is the last part of the code of the array class

before i tried changed it so that icould return the array.

/**

* Create and show the GUI.

*/

private static void runGUI ()

{

JFrame.setDefaultLookAndFeelDecorated (true);

score myGrades = new score ();

}

public static void main (String[] args)

{

/* Methods that create and show a GUI should be

run from an event-dispatching thread */

javax.swing.SwingUtilities.invokeLater (new Runnable ()

{

public void run ()

{

runGUI ();

}

}

);

}

}

i basically have an array in the "score" class. i get information into that array and then get that array into the "MemoryMatch" class or transfer the values of the array in the"score" class to another array in the "MemoryMatch" class.

And another thing is that the "MemoryMatch" is one file and the"score" class is another file

i hope this helps. sry if it didn't. i'm new at this so... yea

thanks for the trouble yur taking

Nadal_Federera at 2007-7-12 17:22:49 > top of Java-index,Java Essentials,Java Programming...
# 3
could you pass the the MemoryMatch object to the Score class through its constructor and have Score push the arrayList to the MemoryMatch object when it is ready?caveat: I'm just learning to, so take this w/ a grain of salt.
petes1234a at 2007-7-12 17:22:49 > top of Java-index,Java Essentials,Java Programming...
# 4

In your Scores class (you should change its first letter to uppercase)

If you put this JOptionPane in your "main" method, what do you discover?

public static 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 ()

{

public void run ()

{

runGUI ();

delay (1000);

//!!!!

JOptionPane.showMessageDialog(null, "will this pop up?");

//!!!!

}

}

);

return players;

}

I added the following to your Score class

public class Score implements ActionListener

{

JFrame frame;

........

........

//!!!!

MemoryMatch parentClass;

Then changed the constructor:

public Score (MemoryMatch mm)

{ /* Create and set up the frame */

//!!!!

this.parentClass = mm;

//!!!!

In the actionperformed method

public void actionPerformed (ActionEvent event)

{

String eventName = event.getActionCommand ();

.......

.......

.......

parentClass.pushPlayers(players); // last statement, pushes the players array to the parent

}

In MemoryMatch I added a method

public void pushPlayers(String[] p)

{

players = p;

}

and changed the main method to:

public static void main (String[] args)

{

//Score

MemoryMatch myMemoryMatch = new MemoryMatch();

JFrame.setDefaultLookAndFeelDecorated (true);

Score myGrades = new Score (myMemoryMatch);

//Score

delay (15000);

//running of main game

myMemoryMatch.setVisible (true);

for (int i = 0; i < myMemoryMatch.players.length; i++)

{

// show that the variable has been updated here

System.out.println(myMemoryMatch.players[i]);

}

}

I would consider creating an interface that contains the pushPlayers() method definition as its only content and have the MemoryMatch object implement this interface. Then score could hold a variable of the interface rather than MemoryMatch. This would give more flexibility if you ever changed MemoryMatches name or somesuch.

Not the best way probably (and hopefully someone else will post better), but it works.

petes1234a at 2007-7-12 17:22:49 > top of Java-index,Java Essentials,Java Programming...
# 5

hey man,

thanks for the help . it works perfectly now. ..and about the first thing abt the "JOptionPane" it did not work cuz it said that it did not exist in the main method.

i just have another question. U know for the last part of the program where the GUI are run (This is part of the main method in the "MemoryMatch" program ::::

public static void main (String[] args)

{

//MemoryGame

JFrame.setDefaultLookAndFeelDecorated (true);

MemoryGame luckyPlayer = new MemoryGame ();

//MemoryGame

//Score

MemoryMatch myMemoryMatch = new MemoryMatch ();

JFrame.setDefaultLookAndFeelDecorated (true);

Score myGrades = new Score (myMemoryMatch);

//Score

delay (15000);

//running of main game

myMemoryMatch.setVisible (true);

}

is it possible to run the "MemoryMatch" program mouse-clicked on the "Enter" button on the screen after they input the information?

or after the "Score" program has run.

the thing is that in order to get information from the "Score" program the user needs to input the information and click the "Enter" button on the screen before the "MemoryMatch" program runs. Thats y i had the 15 second delay.

i would rely appreciate if u could find a solution for that. where the "MemoryMatch" program runs after the user has input the names and clicked the "Enter" button on the screen.

thanks....and thanks again for the earlier help

Nadal_Federera at 2007-7-12 17:22:49 > top of Java-index,Java Essentials,Java Programming...
# 6

> hey man,

> thanks for the help .

Your welcome. If it helped you, then some Duke's stars would be appreciated.

> i just have another question. U know for the last

> part of the program where the GUI are run (This is

> part of the main method in the "MemoryMatch" program

> ::::

>

> is it possible to run the "MemoryMatch" program

> mouse-clicked on the "Enter" button on the screen

> after they input the information?

> or after the "Score" program has run.

One way to do this (and again, there are probably better ways, since I'm a novice) is to change the Score class to a subclass of dialog (have it extend JDialog I think). Since dialogs can be modal, they can be set up to prevent further program flow until they have completed.

Message was edited by:

petes1234

petes1234a at 2007-7-12 17:22:49 > top of Java-index,Java Essentials,Java Programming...
# 7

A couple of things you might need to make a custom modal JDialog out of Score:

1) Again, your class Score would have to extend JDialog.

2) You wouldn't need "JFrame frame". Score will be your top-level container

3) You'd probably need to make your JPanel called contentPane to become Score's contentPane.

4) In your constructor, you'd have to call the JDialog super constructor in such a way to make Score modal. Look at the JDialog docs for this.

5) Since frame is out of the picture, you'll ahve to pack and setvisible Score.

6) You may need to call setvisible(false) at the end of the actionPerformed method

There would be more needed if you had to validate the input or the button pushed.

More on JDialog can be found [url=http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html]here[/url].

Good luck.

petes1234a at 2007-7-12 17:22:49 > top of Java-index,Java Essentials,Java Programming...