I would like some help refactoring this code. This is to improve some of the nonfunctional attributes of the software to
- improve code readability
- reduce complexity to improve the maintainability of the source code.
package Game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
class GameStructure {
private String []wordList = {"computer","java","activity","alaska","appearance","article",
"automobile","basket","birthday","canada","central","character","chicken","chosen",
"cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
"establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
"hurried","identity","importance","impossible","invented","italian","journey","lincoln",
"london","massage","minerals","outer","paint","particles","personal","physical","progress",
"quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
"strike","successful","sudden","terrible","traffic","unusual","volume","yesterday"};
private JTextField tf;
private JLabel jlLetsUsed;
static String letter;
static int []wordLength = new int[64];
public void window() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Developer", KeyEvent.VK_T);
menu.add(menuItem);
JMenuItem menuItem2 = new JMenuItem("Instructions", KeyEvent.VK_T);
menu.add(menuItem2);
JMenuItem menuItem3 = new JMenuItem("Levels", KeyEvent.VK_T);
menu.add(menuItem3);
JMenuItem menuItem4 = new JMenuItem("Restart", KeyEvent.VK_T);
menu.add(menuItem4);
JMenuItem menuItem5 = new JMenuItem("Exit", KeyEvent.VK_T);
menu.add(menuItem5);
ImageIcon ic = new ImageIcon("hangman2.png");
JFrame gameFrame = new JFrame();
JPanel bottomRight = new JPanel();
JPanel bottomLeft = new JPanel();
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JPanel imgPane = new JPanel();
JPanel panel1 = new JPanel();
bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
imgPane.setLayout(new BorderLayout());
panel1.setLayout(new BorderLayout());
panel1.setOpaque(false);//!!
top.setBorder(BorderFactory.createTitledBorder(""));
bottom.setBorder(BorderFactory.createTitledBorder(""));
tf = new JTextField(1);
JLabel img = new JLabel(ic, JLabel.CENTER);
JLabel jl = new JLabel("Enter a letter", JLabel.CENTER);
jlLetsUsed = new JLabel("Letters used: ", JLabel.CENTER);
final JLabel jlLines = new JLabel("__ ", JLabel.CENTER);
jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
jlLetsUsed.setFont(new Font("Rockwell", Font.PLAIN, 20));
jlLines.setFont(new Font("Rockewell", Font.PLAIN, 20 ));
imgPane.add(img);//center
top.add(jl);//top center
top.add(tf);//top center
bottomLeft.add(jlLetsUsed);//bottom left position
bottomRight.add(jlLines);//bottom right position
bottom.add(bottomLeft);//bottom
bottom.add(bottomRight);//bottom
panel1.add(imgPane, BorderLayout.CENTER);//background image (center)
panel1.add(top, BorderLayout.NORTH);//text field and jlabel (top)
panel1.add(bottom, BorderLayout.SOUTH);// blank spaces and letters used (bottom)
gameFrame.setJMenuBar(menuBar);
gameFrame.setTitle("Hangman");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setIconImage(
new ImageIcon("hangmanIcon.png").getImage());
gameFrame.setResizable(false);
gameFrame.add(panel1);
gameFrame.setSize(800, 500);
gameFrame.setLocationRelativeTo(null);
gameFrame.setVisible(true);
int j = 0;
String line = "";
for(j = 0; j<64; j++) {
wordLength[j] = wordList[j].length();//gets length of words in wordList
}//end for
int f = 2;//change num to change level
int m = 0;
//creates line first then put into .setText
while(m<wordLength[f]) {
line += "__ ";
m++;
}//end for
jlLines.setText(line);
tf.addActionListener(new ActionListener() {
int wrong = 0;
@Override
public void actionPerformed(ActionEvent e) {//when enter key pressed
JTextField tf = (JTextField)e.getSource();
letter = tf.getText();
jlLetsUsed.setText(jlLetsUsed.getText() + letter + " ");//sets jlabel text to users entered letter
char[] jlabelText = jlLines.getText().toCharArray();//converts string to character array (array length is length of string)
char userEnteredChar = letter.charAt(0);
//int wrong = 0;
int level = 2;//change num to change level
if (!wordList[level].contains(letter)) {
wrong++;
if (wrong >= 6) {
System.out.println("He's dead, game over.");//prompt user
}
return;
}
int i = 0;
for(i = 0; i<wordList[level].length(); i++){
if(wordList[level].charAt(i) == userEnteredChar){
jlabelText[3 * i] = ' ';
jlabelText[3 * i + 1] = userEnteredChar;
jlLines.setText(String.valueOf(jlabelText));
}
}//end for
}//end actionPerformed method
});
}//end window method
}
public class GameMain {
public static void main(String[] args) {
GameStructure game = new GameStructure();
game.window();
}
}