0

I created a enumeration with a few words. I want a word at random to be selected on displayed on the gui, however, I am unsure of how to have the random word actually display. If anymore info is needed, feel free to let me know, thank you!

Here is the enum:

public enum Words {
    RIFLE,RAILROAD,FARM,SLOPE,LEPERACHAUN,SONG,CREATOR,TENT,FORM,FOOD,DINNER,TICKET,NOVEL,SPARK,
    KITTEN,GUST,SMOKE,HORSE,LOSS,BRAKE,JAZZ,BASEBALL,SIZZLE,LEOPARD,SPARROW,EGG,QUARTER,MULTIPLE,DUPLEX,VOICE,
    GNU,UNDERWEAR,SAND,BED,CANNON,NOTEBOOK,CAUSE,DIRT,PYTHON,SWING,WORD,RAY,SNOW,TRUCK,SILVER,NERVE,DEATH,
    SEASHORE,WATER,COBWEB;

    public String toString() {
        String name = name();
        return name.substring(0, 1) + name.substring(1);
    }

}

Here is the view, where I want a word to display:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLabel;

import acm.graphics.GLabel;
import acm.program.Program;

public class HangmanView implements MouseListener, ActionListener {

    private static final int DEFAULT_SIZE = 600;
    private static final Font FONT = new Font("Helvetica", Font.BOLD, 20);
    private JLabel statusMsg;
    private Words words;
    HangmanGraphics Content = new    HangmanGraphics(DEFAULT_SIZE/2,DEFAULT_SIZE/2);
    Hangman game;         // the controller
    HangmanModel model;   // the model

public HangmanView(Hangman game) {

    final GLabel label = new GLabel(words.toString());
    label.setFont(FONT);
    game.add(label, DEFAULT_SIZE/4, DEFAULT_SIZE/4);

}
}
DjDamage
  • 1
  • 5
  • Just a note, I have a main class that runs the view. – DjDamage Feb 09 '17 at 23:55
  • 1
    "`return name.substring(0, 1) + name.substring(1);`" Or, `return name;`. Or just don't override `toString()` in the first place. – Andy Turner Feb 09 '17 at 23:56
  • 2
    Are you asking [how to pick a random value from an enum](http://stackoverflow.com/questions/1972392/java-pick-a-random-value-from-an-enum)? (BTW, it's not really appropriate to use an enum here. Just use a `List`). – Andy Turner Feb 09 '17 at 23:57
  • I'm just having trouble figuring out how to have a word actually display. I'm not too sure how to input that in the code for the view. Edit: I will try the list as well. – DjDamage Feb 10 '17 at 00:01

1 Answers1

0

You need the code bellow:

import java.util.Random;

public static void main(String[] args){
    Random random = new Random();
    int rand = random.nextInt(Words.values().length);
    System.out.println(Words.values()[rand]);
}

What it does is create a random number (rand) from 1 to number of words, using the class Random and then print out the element with index rand (Words.values()[rand]). Words.values() returns an array of all the words in the enumeration.

Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
  • 1
    OP wants to print to the GUI, not the console. – shmosel Feb 10 '17 at 00:08
  • @shmosel I don't think this is the problem here. However, if it's on the GUI on what element? On a Label? Then instead of System.out this is needed: `label = new Label(Words.values()[rand]); frame.add(label);` with frame being the main frame or instead the panel in which the label will be placed. – Thanasis1101 Feb 10 '17 at 00:11
  • I have no idea. That's why I didn't answer. – shmosel Feb 10 '17 at 00:12