Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Options window for game

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Options window for game

    I am having trouble implementing a window on a game I have designed.

    The game has a class, GUI, which includes the frame for the game itself, a menu, and multiple panels. When I choose "options" from the menu, I either want the options panel to replace the game panel in my frame, or I want another window to open up with my options. Once I have completed selecting my options, I want the panel to go back to my game, or the options window to close. I don't care which.

    I have never programmed in swing before, so I am doing this completely based on google searches, and it's making me insane. I just don't understand well enough what's going on to make it look how I want. I am also having immense trouble with my action listeners. I want the listeners associated with my comboboxes in the options class to update values in the variables class, which then updates the look of my GUI.

    I'm sorry this is so long, I really need some guidance here. I don't know what I am doing wrong!

    Here's what I have for the options class
     
    // deleted long list of imports to save space in post
     
    public class Options extends JPanel implements ActionListener, ItemListener {
     
    	private static final long serialVersionUID = 1L;
    	private JComboBox windowSize;
    	private JComboBox boardSize;
    	private JPanel windowPanel;
    	private JPanel top;
    	private JPanel middle;
    	private JPanel bottom;
    	private JButton okay;
    	private JButton cancel;
    	private JComboBox colors;
    	private JComboBox font;
    	private JButton twoPlayerGame = new JButton("2 Players"),
    			onePlayerGame = new JButton("1 Player");
    	private JTextArea buttonLabelBoard = new JTextArea(
    			"Use the following options to select the size of the board, \n size of the game, color, and font desired.");
     
    	GUI GUI;
    	Variables Variables;
     
    	public void Options() {
    		buttonLabelBoard.setBackground(getBackground());
    		windowPanel = new JPanel();
    		top = new JPanel();
    		windowPanel.add(top, BorderLayout.NORTH);
    		middle = new JPanel();
    		middle.setBorder(new EtchedBorder(EtchedBorder.RAISED));
    		middle.setLayout(new FlowLayout(FlowLayout.CENTER));
    		windowPanel.add(middle, BorderLayout.CENTER);
    		bottom = new JPanel();
    		windowPanel.add(bottom, BorderLayout.SOUTH);
     
    		// initiate combo box for board size modifications
     
    		boardSize = new JComboBox();
    		for (int i = 0; i < Constants.BOARD_SIZES.length; i++) {
    			boardSize.addItem(Constants.BOARD_SIZES[i]);
    		}
    		boardSize.addItemListener(this);
    		bottom.add(buttonLabelBoard);
    		top.add(boardSize);
     
    		// initiate combo box for window size modifications
    		windowSize = new JComboBox();
    		for (int i = 0; i < Constants.WINDOW_SIZES.length; i++) {
    			windowSize.addItem(Constants.WINDOW_SIZES[i]);
    		}
    		windowSize.addItemListener(this);
    		top.add(windowSize);
     
    		// initiate combo buttons to choose color
    		colors = new JComboBox();
    		for (int i = 0; i < Constants.COLORS.length; i++) {
     
    			colors.addItem(Constants.COLOR_OPTIONS[i]);
    		}
    		colors.addItemListener(this);
    		top.add(colors);
     
    		// initiate dropdown box to choose font
    		font = new JComboBox();
    		for (int i = 0; i < Constants.FONT.length; i++) {
    			font.addItem(Constants.FONT_OPTIONS[i]);
    		}
    		font.addItemListener(this);
    		top.add(font);
     
    		middle.add(onePlayerGame);
    		onePlayerGame.addActionListener(this);
    		middle.add(twoPlayerGame);
    		twoPlayerGame.addActionListener(this);
     
    		// create button to close options pane
    		okay = new JButton("OK");
    		okay.addActionListener(this);
    		bottom.add(okay);
    		cancel = new JButton("Cancel");
    		cancel.addActionListener(this);
    		bottom.add(cancel);
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		Object source = e.getSource();
     
    		if (source == okay) {
    			GUI.frame.remove(windowPanel);
    			GUI.frame.add(GUI.buttonHolder);
    		} else if (source == cancel) {
     
    		}
     
    	}
     
    	@Override
    	public void itemStateChanged(ItemEvent a) {
    		JComboBox source = (JComboBox) a.getSource();
    		if (source.equals(windowSize)) {
    			System.out.println("window size modified");
    			String windowSizeTemp = source.getSelectedItem().toString();
    			int windowSizeSelected = Integer.parseInt(windowSizeTemp);
    			Variables.setX(windowSizeSelected);
    			Variables.setY(windowSizeSelected);
    		}
     
    	}
     
    	public JPanel getPanel() {
    		Options();
    		return windowPanel;
    	}
    }

    Here's what I have for the GUI class

    package game.cc.gui;
    // deleted import list to save space
     
    public class GUI implements ActionListener {
    	Player Player = new Player();
    	Board Board = new Board();
    	Options Options = new Options();
    	Save_Game Save_Game = new Save_Game();
    	Variables Variables = new Variables();
     
    	// JButton backButton = new JButton("Back");
    	ImageIcon pretzel = new ImageIcon("pretzel.gif");
    	ImageIcon cheese = new ImageIcon("game/cheese.gif");
    	boolean gameInProgress = false;
    	String player1Name;
    	String player2Name;
    	int windowSize;
    	JFrame frame;
    	JPanel gameMain = new JPanel(), buttonHolder = new JPanel(),
    			pnlNorth = new JPanel(), pnlSouth = new JPanel(),
    			pnlTop = new JPanel(), pnlBottom = new JPanel(),
    			gameSetup = new JPanel();
     
    	JMenuBar menuBar;
    	JMenu file, help;
    	JMenuItem helpMenu, aboutGame, newGame, saveGame, loadGame, options, exit;
    	String message;
    	JTextArea score, playerTurn, helpMenuText, aboutTxt;
     
    	public void GUI() {
    		System.out.println("checking image");
    		System.out.println(cheese);
    		System.out.println(pretzel);
    		// design game window frame
    		frame = new JFrame("Cheese & Pretzels" + Constants.VERSION);
    		frame.setLayout(new BorderLayout());
    		frame.setSize(Variables.getX(), Variables.getY());
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		// Setting Panel layouts and properties
    		buttonHolder.setLayout(new GridLayout(2, 1, 2, 10));
    		pnlNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
    		pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
     
    		pnlNorth.setBackground(new Color(Variables.getColor() - 20, Variables
    				.getColor() - 20, Variables.getColor() - 20));
    		pnlSouth.setBackground(new Color(Variables.getColor(), Variables
    				.getColor(), Variables.getColor()));
     
    		pnlTop.setBackground(new Color(Variables.getColor(), Variables
    				.getColor(), Variables.getColor()));
    		pnlBottom.setBackground(new Color(Variables.getColor(), Variables
    				.getColor(), Variables.getColor()));
     
    		pnlTop.setLayout(new FlowLayout(FlowLayout.CENTER));
    		pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER));
    		buttonHolder.setBackground(Color.blue);
     
    		// gameSetup.add(backButton);
    		// backButton.addActionListener(this);
     
    		buttonHolder.setLayout(new GridLayout(Variables.getBoardSize(),
    				Variables.getBoardSize(), 2, 2));
    		buttonHolder.setBackground(Color.black);
     
    		frame.add(gameMain);
    		frame.add(buttonHolder);
     
    		menuBar = new JMenuBar();
    		frame.setJMenuBar(menuBar);
     
    		file = new JMenu("File");
    		menuBar.add(file);
    		file.addActionListener(this);
     
    		help = new JMenu("Help");
    		menuBar.add(help);
    		help.addActionListener(this);
     
    		helpMenu = new JMenuItem("Cheese and Pretzels Help");
    		help.add(helpMenu);
    		helpMenu.addActionListener(this);
     
    		aboutGame = new JMenuItem("About Cheese and Pretzels");
    		help.add(aboutGame);
    		aboutGame.addActionListener(this);
     
    		newGame = new JMenuItem("New Game");
    		file.add(newGame);
    		newGame.addActionListener(this);
     
    		saveGame = new JMenuItem("Save Game");
    		file.add(saveGame);
    		saveGame.addActionListener(this);
     
    		loadGame = new JMenuItem("Load Game");
    		file.add(loadGame);
    		loadGame.addActionListener(this);
     
    		options = new JMenuItem("Options");
    		file.add(options);
    		options.addActionListener(this);
     
    		exit = new JMenuItem("Exit");
    		file.add(exit);
    		exit.addActionListener(this);
     
    		setName(); // call set name method to assist in creating interface
     
    		score = new JTextArea(player1Name + " Score : "
    				+ Player.getPlayerScore(1) + "      " + player2Name
    				+ " Score: " + Player.getPlayerScore(2));
    		score.setBackground(Color.GRAY);
    		playerTurn = new JTextArea("Your turn, Player "
    				+ Player.getPlayerTurn());
    		playerTurn.setEditable(false);
    		;
    		score.setEditable(false);
    		pnlSouth.add(score);
    		pnlNorth.add(pnlTop);
    		pnlNorth.add(pnlBottom);
    		// pnlSouth.add(backButton);
     
    		for (int i = 0; i < Variables.getBoardSize() * Variables.getBoardSize(); i++) {
    			Variables.getButtons()[i] = new JButton();
    			Variables.getButtons()[i].setText("");
    			Variables.getButtons()[i].setBackground(new Color(Variables
    					.getButtonColor(), Variables.getButtonColor(), Variables
    					.getButtonColor()));
    			Variables.getButtons()[i].addActionListener(this);
    			buttonHolder.add(Variables.getButtons()[i]);
    		}
    		// Adding to window and Showing window
    		frame.add(pnlNorth, BorderLayout.NORTH);
    		frame.add(pnlSouth, BorderLayout.SOUTH);
    		frame.setVisible(true);
     
    		for (int i = 0; i < 49; i++) { // set all buttons unused
    			Variables.getButtonUsed()[i] = false;
    		}
    	}
     
    	public void actionPerformed(ActionEvent click) {
    		Object source = click.getSource();
    		for (int i = 1; i <= Variables.getBoardSize()
    				* Variables.getBoardSize(); i++) { // place
    													// x or
    													// o on
    			// button based on
    			// player turn
    			if (source == Variables.getButtons()[i]
    					&& !Variables.getButtonUsed()[i]) {
    				Variables.getButtonUsed()[i] = true;
    				if (Player.getPlayerTurn() == 1) {
    					Variables.getButtons()[i].setText("X");
    					// buttons[i].setIcon(pretzel);
    				}
     
    				else
    					Variables.getButtons()[i].setText("O");
    				// buttons[i].setIcon(cheese);
    				Variables.getButtons()[i].setEnabled(false);
    				buttonHolder.requestFocusInWindow();
    				Player.changePlayer(Player.getPlayerTurn());
    			} // end if
    		} // end for
    			// Board.checkBoard(gameBoard, playerNumber); // update new check
    			// board
    			// method
    		if (source == newGame) {
    			pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
    			pnlTop.add(gameMain);
    			pnlSouth.add(pnlTop);
    			pnlSouth.add(pnlBottom);
    			// pnlBottom.add(backButton);
    			for (int i = 0; i < Variables.getBoardSize()
    					* Variables.getBoardSize(); i++) {
    				Variables.getButtons()[i].setText("");
    				Variables.getButtonUsed()[i] = false;
    				Variables.getButtons()[i].setEnabled(true);
    			}
    			// need to decide whether to set this here or in options pane
    		}/**
    		 * else if (source == twoPlayerGame) { if (gameInProgress) { int option
    		 * = JOptionPane.showConfirmDialog(null, "If you start a new game," +
    		 * "your current game will be lost..." + "\n" +
    		 * "Are you sure you want to continue?", "Quit Game?",
    		 * JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION) {
    		 * gameInProgress = false; } } if (!gameInProgress) { clearBoard();
    		 * Player.getPlayerTurn(); for (int i = 1; i < 10; i++) {
    		 * buttons[i].setText(""); buttons[i].setEnabled(true); } // win =
    		 * false; // showGame();
    		 * 
    		 * } } else if (source == onePlayerGame) {
    		 * JOptionPane.showMessageDialog(null, "Coming Soon!!");
    		 * 
    		 * }
    		 */
    		else if (source == exit) { // replace exit action option with this.
    			int option = JOptionPane.showConfirmDialog(null,
    					"Do you want to save your game?", "Exit Game",
    					JOptionPane.YES_NO_OPTION);
    			if (option == JOptionPane.NO_OPTION)
    				System.exit(0);
    			else
    				try {
    					Save_Game.save();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} // correct to call the save game class.
    		} else if (source == helpMenu) {
    			clearPanelSouth();
    			message = "Instructions:\n\n"
    					+ "Your goal is to be the first player to fill a straight line with your pieces,\n"
    					+ " horizontally, diagonally, or vertically.";
    			helpMenuText = new JTextArea(message);
    			helpMenuText.setBackground(new Color(Variables.getColor(),
    					Variables.getColor(), Variables.getColor()));
    			helpMenuText.setEditable(false);
    			helpMenuText.setText(message);
    			pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
    			pnlTop.add(helpMenuText);
    			// pnlBottom.add(backButton);
    			pnlSouth.add(pnlTop);
    			pnlSouth.add(pnlBottom);
    		} else if (source == aboutGame) {
    			clearPanelSouth();
    			message = "About:\n\n" + "Title: Cheese & Pretzels\n"
    					+ "Author: Joanna Alcausin\n" + "Version: "
    					+ Constants.VERSION + "\n";
    			aboutTxt = new JTextArea(message);
    			aboutTxt.setBackground(new Color(Variables.getColor(), Variables
    					.getColor(), Variables.getColor()));
    			aboutTxt.setEditable(false);
    			aboutTxt.setText(message);
    			pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
    			pnlTop.add(aboutTxt);
    			// pnlBottom.add(backButton);
    			pnlSouth.add(pnlTop);
    			pnlSouth.add(pnlBottom);
     
    		}
     
    		// else if (source == backButton) {
    		// clearPanelSouth();
    		// pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
    		// pnlNorth.setVisible(true);
    		// }
     
    		else if (source == options) {
    			frame.remove(buttonHolder);
    			frame.add(Options.getPanel());
     
    		}
     
    		// else if (source == Options.close) {
     
    		// }
    		pnlSouth.setVisible(false);
    		pnlSouth.setVisible(true);
    	}
     
    	/**
    	 * allows player to set the names of self and opponent upon opening new game
    	 * 
    	 */
    	public void setName() {
     
    		this.player1Name = JOptionPane.showInputDialog(null,
    				"Enter Name of player 1: ", "", 1);
     
    		this.player2Name = JOptionPane.showInputDialog(null,
    				"Enter Name of player 2: ", "", 1);
     
    	}
     
    	public void closeWindow() {
    		frame.remove(Options);
    		frame.add(gameMain);
    		frame.add(buttonHolder);
    	}
     
    	public void clearBoard() {
    		for (int i = 0; i < Variables.getBoardSize() * Variables.getBoardSize(); i++) {
    			Variables.getButtons()[i].setText("");
    			Variables.getButtonUsed()[i] = false;
    		}
    	}
     
    	public void clearPanelSouth() {
    		pnlSouth.removeAll();
    	}
    }


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Options window for game

    you see that when you're adding new components to the panel/frame etc..

    I have no time to test your code but first option would be to try to reset the frame as visible as the last line of code on your action performed.

    frame.setVisible(true);

    It should refresh the frame. Otherwise I would make the options class a JFrame / frame and simply put new Options(); in the settings action performed..

    make sure you've got a constructor in your options class..

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Options window for game

    // deleted long list of imports to save space in post
    That makes it very hard to copy your code, compile it and test it.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Options window for game

    public void GUI() {
    If you think it's acting as a constructor, then you are probably wrong. Constructor has no return type(neither void)

    And as i am not able to compile your code due to dependencies it's asking for. Well, i can assume that you are trying to instantiate an object of GUI from somewhere your main class and as it's not finding any constructor, so resulting in error.
    Replace
    public GUI(){

    Well, if i assumed wrong, you must provide details and before posting read SSCCE

Similar Threads

  1. Replies: 1
    Last Post: October 26th, 2011, 08:02 AM
  2. Craating non active window ,and inputing to the non active window.
    By java-beginner in forum Java Theory & Questions
    Replies: 0
    Last Post: February 25th, 2011, 10:13 PM
  3. New Window from a Servlet
    By RookieServletPrgmer in forum Java Servlet
    Replies: 6
    Last Post: December 2nd, 2010, 08:52 AM
  4. cmd.exe DOS Window
    By Curious in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 1st, 2010, 10:30 AM
  5. Navigating from one window to another
    By visharaddhavle83 in forum AWT / Java Swing
    Replies: 2
    Last Post: August 6th, 2009, 07:55 AM