I'm creating a program that is supposed to create a memorization game that assigns colors to a user, and then tests how well the user can relay the colors back. Because of this, I created an array class that has two arrays (one with colors already assigned to numbers, and another that uses the first array to randomly assign colors for the game's purpose).
Because of this, I then need to access the array class from the main in order to extract which colors were actually chosen -- and this is where I'm running into problems, specifically when I try to create an instance of the colorArrays class. Here is the code for my main class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Mission13Main
{
public static void main (String[] args) // This is the main driver of the program
{
//Start of game:
//--------------------------------------------------------
colorArrays color = new colorArrays();//"error: non-static variable this cannot be referenced from a static context"
System.out.println(color.getColor(0));//A test to see if I can use getColor
JOptionPane.showMessageDialog(null, "How good is your memory?\nTry to memorize this color sequence:\n \n" + color.getColor[0] + " " + color.getColor[1] + " " + color.getColor[2] + " " + color.getColor[3] + " " + color.getColor[4] + " " + color.getColor[5] + " " + color.getColor[6] + " ");
}
I had some of these command lines commented out so that I could get the program to compile.
Here's the arrayColors class:
//Class to construct arrays and assign colors to user
//-----------------------------------------------------------
public class colorArrays
{
String[] getColor
String[] userColor;
colorArrays color;
public void colorArrays() //Array Constructor
{
Random r = new Random();
String test = "test";
//Array # 1 for the colors user may get
//------------------------
String[] color = new String[10];
color[0] = "red";
color[1] = "yellow";
color[2] = "blue";
color[3] = "green";
color[4] = "purple";
color[5] = "brown";
color[6] = "black";
color[7] = "white";
color[8] = "pink";
color[9] = "orange";
//Array # 2 which selects the colors user will memorize
//-------------------------
String[] userColor = new String[7];
for (int i = 0; i <= 6; i++)
{
int randomC = r.nextInt(10);
userColor[i] = color[randomC];
System.out.print(userColor[i] + " "); //--> a test to see if it is assigning colors properly
}
//--------------------------------------------------------------
}
//Method to extract specific color
public String getColor (int n)
{
String correctColor = "";
correctColor = color.userColor[n];
return correctColor;
}
}
I've spent a long time on this program, and feel that I'm fairly close, but that gathering information from the other classes is really slowing me down. I feel like all the classes work individually, but I just can't get them to communicate properly. Any help is greatly appreciated.
UPDATE:
Here is the full code as everyone else suggested.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Mission13Main
{
public static void main (String[] args) // This is the main driver of the program
{
//Start of game:
//--------------------------------------------------------
colorArrays color = new colorArrays();//"error: non-static variable this cannot be referenced from a static context"
System.out.println(color.getColor(0));//A test to see if I can use getColor
JOptionPane.showMessageDialog(null, "How good is your memory?\nTry to memorize this color sequence:\n \n" + color.getColor[0] + " " + color.getColor[1] + " " + color.getColor[2] + " " + color.getColor[3] + " " + color.getColor[4] + " " + color.getColor[5] + " " + color.getColor[6] + " ");
}
//-----------------------------------------------------------
// Class that is the code for the game
//-----------------------------------------------------------
public class MemoryGame
{
private JFrame mainWindow;
private JLabel color1;
private JLabel color2;
private JLabel color3;
private JLabel color4;
private JLabel color5;
private JLabel color6;
private JLabel color7;
private JLabel gameResults;
private JTextField txtColor;
//-------------------------------------------------------------
public MemoryGame () // This is the constructor for the class -- creates an instance of the class
{
//creating frame
mainWindow = new JFrame();
mainWindow.setSize(600,400);
mainWindow.setTitle("Memory Game");
mainWindow.setDefaultCloseOperation(mainWindow.EXIT_ON_CLOSE);
mainWindow.setLayout(new FlowLayout());
//building contents
color1 = new JLabel("Enter color # 1: ");
color2 = new JLabel("Enter color # 2: ");
color3 = new JLabel("Enter color # 3: ");
color4 = new JLabel("Enter color # 4: ");
color5 = new JLabel("Enter color # 5: ");
color6 = new JLabel("Enter color # 6: ");
color7 = new JLabel("Enter color # 7: ");
txtColor = new JTextField(15);
//Add contents
mainWindow.add(color1);
mainWindow.add(txtColor);
mainWindow.add(gameResults);
//add action listener
txtColor.addActionListener(new CheckListener());
} //end of create contents
// Action Listener:
//--------------------------------------------------------------------
private class CheckListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Mission13Main m = new Mission13Main();
int i = 0;
if (i == 0)
{
gameResults.setText("Correct Guess");
}
else
{
gameResults.setText("Incorrect Guess.");
}
}
}
}
//Class to construct arrays and assign colors to user
//-----------------------------------------------------------
public class colorArrays
{
String[] getColor;
String[] userColor;
colorArrays color;
public colorArrays() //Array Constructor
{
Random r = new Random();
String test = "test";
//Array # 1 for the colors user may get
//------------------------
String[] color = new String[10];
color[0] = "red";
color[1] = "yellow";
color[2] = "blue";
color[3] = "green";
color[4] = "purple";
color[5] = "brown";
color[6] = "black";
color[7] = "white";
color[8] = "pink";
color[9] = "orange";
//Array # 2 which selects the colors user will memorize
//-------------------------
String[] userColor = new String[7];
for (int i = 0; i <= 6; i++)
{
int randomC = r.nextInt(10);
userColor[i] = color[randomC];
System.out.print(userColor[i] + " "); //--> a test to see if it is assigning colors properly
}
//--------------------------------------------------------------
}
//Method to extract specific color
public String getColor (int n)
{
String correctColor = "";
correctColor = color.userColor[n];
return correctColor;
}
}
}
I'm sorry if it's indented weird, I'm new to this and not sure how to easily format it.
Thank you for everyone who is being NICE about my question -- I feel like while many users are very helpful and supply constructive criticism, others may be condescending about beginners not knowing as much as them. I only started learning about object oriented programming last week -- today finally figuring out what the constructor is actually used for. -- so thank you for the kind responses.