After much research and trial and error, I am unable to find out how to exactly take a randomly selected String
from a String Array
in an Array
of String Array
s and print it out, character by character onto a JTextField Array
after a JButton
is pressed. I know this sounds... awkward, wasn't exactly sure how to phrase it to make it sound less awkward. So, here's the class I made that makes the JTextField Array
on a JPanel
.
package wheelOfFortune;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
public class letterBoard extends JPanel
implements ActionListener
{
private static JTextField[] fields;
private static final String wheelFoods[] = {"PASTRAMI SANDWICH","PEANUT BUTTER","JAR OF PICKLES","NUTELLA SPREAD","SPAGHETTI AND MEATBALLS","BUTTERED LOBSTER","KING CRAB","PIRATES BOOTY","ORANGE JUICE","GRILLED STEAK","MOLTEN LAVA CAKE"};
private static final String wheelHouse[] = {"BURNT OUT LIGHTBULB", "VACUUM CLEANER","DESKTOP COMPUTER", "ANTIQUE FURNITURE","FLATSCREEN TELEVISION","COFFEE TABLE","MAHOGANY DESK","GARDENING SUPPLIES","REMOTE CONTROLLER"};
private static final String wheelMovies[] = {"SAVING PRIVATE RYAN","THE GREAT GATSBY", "ZOOLANDER","ANCHORMAN","STEP BROTHERS","THE DARK NIGHT RISES",
"PULP FICTION","FIGHT CLUB","THE SIXTH SENSE","DJANGO UNCHAINED","SHUTTER ISLAND","THE EVIL DEAD","THE NOTEBOOK","FORREST GUMP","HAPPY GILMORE","THE CHRONICLES OF NARNIA",
"BILL AND TEDS MOST EXCELLENT ADVENTURE","HAROLD AND KUMAR","HARRY POTTER","THE SHAWSHANK REDEMPTION", "THE TERMINATOR", "THE BLUES BROTHERS", "V FOR VENDETTA"};
private static final String wheelPeople[] = {"OPRAH WINFREY","SPONGEBOB SQUAREPANTS","PATRICK STAR", "LEONARDO DICAPRIO","WILL FERRELL", "NEIL ARMSTRONG", "BARACK OBAMA", "JON TRAVOLTA", "ARNOLD SCHWARZENEGGER", "JAMES FRANCIS RYAN", "MS VOLDSTAD",
"BILLY MAYS", "EDWARD SCISSORHANDS","BILL GATES", "STEVE JOBS", "GANDOLF THE GRAY","TWOCHAINZ","RICK ASTLEY"};
private static final String wheelPlaces[] = {"THE EIFFEL TOWER","MOUNT RUSHMORE","THE MOON", "THE GREAT WALL OF CHINA", "FISHERMANS WARF", "DISNEYLAND", "UNDERNEATH A DESK IN A CLASSROOM", "THE GREAT PLAINS", "THE GREAT BARRIER REEF", "AMERISTRALIA", "THE EMPIRE STATE BUILDING",
"THE NORTH POLE", "THE SPACE NEEDLE", "THE KRUSTY KRAB","WEENIE HUT JUNIOR","THE FORBIDDEN PALACE","MORDOR", "NARNIA","THE LAST STALL ON THE LEFT", "INSIDE OF A BOUNCY HOUSE"};
private static final String wheelRandom[] = {"A BOWL OF OATMEAL","LEMON PLEDGE","GREASY HAMBURGERS", "STAINED PANTS", "A CUP OF CHOCOLATE MILK", "ROLLY CHAIRS", "BENDY STRAWS", "WIENER DOGS", "AN ANGRY OSTRICH", "BART SIMPSON","A CONFUSED MOUNTAIN COW",
"BIGFOOT", "THE ILLUMINATI", "THE C HALL BATHROOMS", "A SHAVED BABOON", "THE SPICE GIRLS", "I AM STUPID"};
private Box[] boxes = new Box[SUIT_COUNT];
private static int TEXT_FIELD_COUNT = 14;
private static int SUIT_COUNT = 1;
Color yungMoney = new Color(0, 180, 100);
private static String puzzle;
private static List<JTextField> field;
private static String[][] puzzles = {wheelFoods, wheelHouse, wheelMovies, wheelPeople, wheelPlaces, wheelRandom};
private static String[] puzzless;
private static JPanel panel;
public letterBoard()
{
fields = new JTextField[TEXT_FIELD_COUNT];
setPreferredSize(new Dimension(1,299));
setBackground(yungMoney);
JPanel panel = new JPanel(new GridLayout(0,14));
panel.setBackground(yungMoney);
for(int t=0; t<4; t++)
{
for (int i =0; i < boxes.length; i++)
{
boxes[i] = Box.createHorizontalBox();
for (int j=0; j< TEXT_FIELD_COUNT/SUIT_COUNT; j++)
{
int index = i * (TEXT_FIELD_COUNT/SUIT_COUNT) + j;
fields[index] = new JTextField(" ");
fields[index].setEditable(false);
fields[index].setPreferredSize(new Dimension(50, 50));
fields[index].setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
panel.add(fields[index]);
}
}
}
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,2),"WHEEL OF FORTUNE"));
add(panel);
}
public static String findPuzzle()
{
for(int i=0; i<puzzles.length;i++)
{
puzzless = puzzles[(int) Math.round(Math.random() * (puzzles.length-1))];
for(int j=0; j<puzzless.length; j++)
{
puzzle = puzzless[(int) Math.round(Math.random() * (puzzless.length-1))];
}
}
return puzzle;
}
public static void reset()
{
findPuzzle();
field = new ArrayList<>(puzzle.length());
for(int k=0;k<puzzle.length();k++)
{
field.add(fields[k]);
}
}
public void actionPerformed(ActionEvent e)
{
//game logic
}
}
So, the JTextField Array
is formed within the constructor of the class letterBoard
. The constructor is functioning and works as it's supposed to. However, String puzzles[][]
, which holds the various arrays that the puzzles are filed into, (This is a wheel of fortune game. Each individual array
is a category with puzzles inside, etc.) is not working with me how I would like it to. What I essentially want to happen is this: the method reset()
takes the letterBoard formed in the constructor and fills it with the String
chosen in the method findPuzzle()
. From there, an ActionListener
in the main class, wheelGUI
will be hooked up to JButton reset
, which, when pressed, should call the reset()
method from the letterBoard
class and wipe the current puzzle from the board, and fill it with a new, randomly chosen String from puzzles[][]
, allowing for a new round of gameplay. Sorry if this is a bit...confusing. I'm confused by it myself. I looked at some tutorials for writing Strings
onto JTextFields
, but mine is set up slightly differently, and not allowing that method of things to work. Thank you for taking the time to look at this. I will post the wheelGUI
class code if needed, but I don't think it houses any relevant information pertaining to this issue. Thanks again.
I think you're trying to do something like...
public static void reset() {
findPuzzle();
field = new ArrayList<>(puzzle.length());
// Clear the existing values
for (int index = 0; index < fields.length; index++) {
fields[index].setText(null);
}
// Update the new values
for (int k = 0; k < puzzle.length(); k++) {
// Get an individual character from the String and make it a String
// cause it's easier to deal with...
String character = Character.toString(puzzle.charAt(k));
fields[k].setText(character);
field.add(fields[k]);
}
}
Now this will display the answer, but I think you can work your way around it...