Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,269 Java Programmers. There are 1,880 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Adding a GUI

 
Reply to this topicStart new topic

Adding a GUI

mogstronaut_
post 8 May, 2008 - 02:51 AM
Post #1


New D.I.C Head

*
Joined: 18 Apr, 2008
Posts: 33



Hi guys,
I'm currently trying to turn my text based game-thing into an interactive GUI. I'm just doing it one step at a time, but I've already run into a problem..
My code looks like this:
java
import javax.swing.JFrame;
import java.awt.*;
import java.util.Scanner;
public class AWeekIsALongTime extends JFrame
{
private static final int WIDTH = 700;
private static final int HEIGHT = 500;
private JLabel intro;

private DeputyPremier politician; // Declares a new DeputyPremier object called politician
private boolean tracing; // Declares a boolean called tracing to aid debugging
private Scanner sc; // Declares a new scanner object called 'sc'
private int userChoice; // Declares an int that will hold the value of what the user enters in the getChoices method

private int numGames; // Declares an int to hold the number of games the user has played
private int numWon; // Declares an int to hold the number of games the user has won
private int numLost; // Declares an int to hold the number of games the user has lost
private int numDrawn; // Declares an int to hold the number of games the user has drawn

/** constructor
instantiates the politician and scanner objects, sets tracing to false.
instantiates all of the integers that hold the number of times the user has won/lost/drawn/played
the game to zero
*/
public AWeekIsALongTime()
{
layoutTest obJ = new layoutTest();
politician = new DeputyPremier();
sc = new Scanner(System.in);
tracing = false;
numGames = 0;
numWon = 0;
numLost = 0;
numDrawn = 0;
intro = new JLabel(explain());
add(intro);
}

public void layoutTest()
{
setTitle("Deputy Premier!");
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

/** play
calls the setUp method from DeputyPremier, details the
game about to be played using the explain method and then
engages the startLoop method to begin the game
*/
public void play()
{
politician.setUp(0);
explain();
startLoop();
}

/** explain
a block of text that details the storyline of the game and the instructions/options
the player has
*/
public void explain()
{
System.out.println("\t\t\tA Week is a Long Time in Politics\n\t\t\t=================================\n\n Taswegia is a fictitious state of a country called Straya. "
+"In this state, the government is led by a Premier and a Deputy Premier. The Deputy Premier (i.e. you) has been asked to appoint a new magistrate and a "
+ "person has been recommended and you have drafted a memorandum to appoint him. Strangely, you have also written and sent a second memorandum with someone else's name on"
+ " it. What will the Deputy Premier do?\n\nYour task is to pretend to be the Deputy Premier and to make choices based around the pressure that is put on you and the"
+ " options available. In order to succeed, you must stay in your job for seven days. \n\nYou will be under pressure from many areas: the press, the leader of the state "
+ "service, journalists, and other political parties. There may even be a whistle-blower within your own ranks!\nAt the beginning of each day (if you're still in the job) you have"
+ " a number of choices. You can:\n\n\t1. make no comment\n\t2. deny in Parliament that this memorandum ever existed\n\t3. shred the document or\n\t4. resign\nYou win "
+ " the game if you survive seven days as Deputy Premier. You lose if you are fired. The game is a draw if you choose to resign.\n\nGood luck. Next!\n");
add(intro);
}

/** startLoop
@return an int
accepts the users input as to whether they want to start the game
*/
public int startLoop()
{
String loop; // Holds the user's input to test if beginGame needs to be called

System.out.print("Would you like to play (y)es or (n)o? ");
loop = sc.next();

if(loop.equalsIgnoreCase("y")) // If the user inputs the character 'y', the beginGame method is called
{
numGames++; // Increments the number of games the player has played by one
beginGame();
}
else // If any other character than 'y' is entered, program reports on the user's details, then terminates.
{
System.out.println("\nYou played " + numGames + " times;");
System.out.println("\nYou won and survived " + numWon + " times;");
System.out.println("You lost and were fired " + numLost + " times;");
System.out.println("You drew by resignation " + numDrawn + " times;");
System.out.println("\nThank you for playing. Now get a real job.");
System.exit(0);
}
return numGames;
}

/**beginGame
@return an int
uses a loop to execute different methods throughout this class in a sequential order seven times.
has a winning screen if user is able to complete all seven days
*/
public int beginGame()
{
for(int i=1; i<=7;i++) // Completes the loop seven times to simulate the seven days
{
startDay();
memorandumCheck();
getChoices();
getConseq();
}

System.out.println("Phew, made it through a week!");
System.out.println("===================================================================\n\n\t\t\tGAME OVER\n"
+ "\nCongratulations Deputy Premier. Would you like to try a second week...\n");
numWon++; // Increments the number of games the player has won by one
politician.newGame(); // Resets the politican object
startLoop(); // Restarts the game

return numWon;
}

/** startDay
welcomes the user to the start of every day. calls the currentDay, getShredderVolume
and getEvents methods from DeputyPremier to display the days information/events
*/
public void startDay()
{
System.out.println("===================================================================\n\n"
+ "Welcome Mr Connz.\nIt is Day #" + politician.getCurrentDay() + "\nSo far this week:\n" + politician.getEvents());
System.out.println("Your shredder is currently " + politician.getShredderVolume() + "% full");
instinctCheck();
}

/** memorandumCheck
calls the hasDocument method from DeputyPremier to check if the memorandum
is in the players possession
*/
public void memorandumCheck()
{
if (politician.hasDocument())
{
System.out.println("The original document is still in your possession.");
}
}

/** getChoices
@return an int
displays a list of actions the user may engage in, records the user input
*/
public int getChoices()
{
System.out.println("\nWhat would you like to do:\n\t1. make no comment\n\t2. deny in Parliament that this memorandum ever existed"
+ "\n\t3. shred the document or\n\t4. resign");
userChoice = sc.nextInt();

if(userChoice == 1)
{
System.out.println("You attempt to tough it out.");
}
if(userChoice == 2)
{
System.out.println("Well, you've bluffed them so far. Or have you?");
}
if(userChoice == 3)
{
if (politician.hasDocument() && politician.getShredderVolume() != 100)
{
System.out.println("Document successfully shredded.");
}
else
{
System.out.println("Sorry but you can't destroy what you do not have!");
}
}
if(userChoice == 4)
{
System.out.println("===================================================================\n\n\t\tGAME OVER\n"
+ "Oh well, I suppose you only let yourself down really...\n");
numDrawn++; // Increments the number of games the player has drawn by one
politician.newGame();
startLoop();
}

return numDrawn;
}

/** getConseq
@return an int
calls the getConsequences method from DeputyPremier, if the severity is < 4, the player continues the game
if severity is >= 4, player is sacked
*/
public int getConseq()
{
int severity = politician.getConsequences(userChoice);
if (severity < 4)
{
System.out.println("Well you've apparently survived another day, but as you leave you hear that ...");
System.out.println(politician.getRumour());
}
else
{
System.out.println("That's it for you. The Premier has sacked you.\n ===================================================================\n\n"
+ "\t\tGAME OVER\n\nEnjoy the back-bench. Would someone ask Mr Baatlit to step in please...\n");
numLost++; // Increments the number of games the player has lost by one
politician.newGame();
startLoop();

}
return numLost;
}
/** instinctCheck
calls the getInstinct method from DeputyPremier and if > 6, displays message
*/
public void instinctCheck()
{
if(politician.getInstinct() >= 6)
{
System.out.println("Your political instinct tells you that you might be in trouble...");
}

}

/** trace
@param String - the message
use to display tracing messages
shown if the variable tracing is true
*/
public void trace(String message)
{
if(tracing)
{
System.out.println("AWeekIsALongTime: " + message);
}
}
}


I'm getting the errors: "private JLabel cannot find symbol", "intro = new JLabel cannot find symbol" and "intro = new JLabel(explain()); - void not allowed here".
Any help? crazy.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Ellie
post 8 May, 2008 - 04:34 AM
Post #2


D.I.C Head

Group Icon
Joined: 17 Jan, 2007
Posts: 244

Hi,

Glad to see you still playing with this icon_up.gif

You just need to import the rest of the Swing package

import javax.swing.*;

That will enable your program to find the JLabel stuff.

However, the JLabel constructor takes a String as an argument, but your execute() method returns void, so essentially you are trying to create a label with a void text string in it. You could modify your execute() method to return the String and then that would work.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

mogstronaut_
post 8 May, 2008 - 05:21 AM
Post #3


New D.I.C Head

*
Joined: 18 Apr, 2008
Posts: 33

Thanks for the reply, Ellie.
But RAWRRR, I can't get it to work at all!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Ellie
post 8 May, 2008 - 08:21 AM
Post #4


D.I.C Head

Group Icon
Joined: 17 Jan, 2007
Posts: 244

Just an idea, but since you're going to be displaying quite a bit of text, why not a JTextArea (or plain old fashioned TextArea)
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Sonar
post Yesterday, 11:15 PM
Post #5


New D.I.C Head

*
Joined: Yesterday, 11:12 PM
Posts: 1

This wouldn't be for a UTas assignment now then would it?....
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 10:30AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month