12 Replies - 490 Views - Last Post: 04 June 2014 - 03:58 PM Rate Topic: -----

#1 Lame_Brain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-June 14

Text Based Game with a Window

Posted 04 June 2014 - 03:33 AM

Hello.
I am a beginner to Java and I was hoping to start somewhere simple. I started a text based game, but I am dissatisfied with the console that PrintF prints to. How can I set up a window and have text output to that window, and have my player type responses in the window?

thank you for your time.
Is This A Good Question/Topic? 0
  • +

Replies To: Text Based Game with a Window

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: Text Based Game with a Window

Posted 04 June 2014 - 03:43 AM

Look at using JOptionPane and JTextArea
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Text Based Game with a Window

Posted 04 June 2014 - 03:46 AM

So if you are still in the beginning phase, then you have alot of stuff to catch on before you would want to try java GUI.
You may find this topic useful as it tell someone what to lean after each step.
In there you will see a link to the game programming thread and alot other more.
Was This Post Helpful? 0
  • +
  • -

#4 drjoliv  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 60
  • Joined: 27-August 13

Re: Text Based Game with a Window

Posted 04 June 2014 - 05:50 AM

Post your current code.
Was This Post Helpful? 0
  • +
  • -

#5 Lame_Brain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-June 14

Re: Text Based Game with a Window

Posted 04 June 2014 - 09:05 AM

Hello,
Here is what I have so far:
package code.main;

public class Game {
	public static void main(String[] args){
		System.out.println("Hello World");
	}	
}



My goal is to get this to print in a window, instead of the console.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,767
  • Joined: 12-June 08

Re: Text Based Game with a Window

Posted 04 June 2014 - 09:08 AM

Okay.. what is a good textbased GUI handler? A textbox from the toolbox? That sounds like a solid lead to investigate. A textbox to enter.. a textbox (probably set to readonly) to display.. a button to trigger an event to move text from input textbox to the output display textbox.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7949
  • View blog
  • Posts: 13,538
  • Joined: 19-March 11

Re: Text Based Game with a Window

Posted 04 June 2014 - 09:15 AM

One thing to keep in mind is that you'd like to separate game logic from presentation - this should work equally well in the console or in the GUI, or in some other GUI you come up with later. You might have to think a little bit about how to make this work, but it'll be well worth the effort.
Was This Post Helpful? 0
  • +
  • -

#8 Lame_Brain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-June 14

Re: Text Based Game with a Window

Posted 04 June 2014 - 10:16 AM

I'm sorry, I may have misrepresented myself. I am a beginner looking to learn, and I haven't gotten very far in the actual code yet, but I am not a total level 0 beginner. I have taken some Java classes and I am taking this project on as a way to refresh myself with the language.

Since my last post I have gotten a bit further, (classes for player and room objects chock full of get and set functions!) and I have a pretty good idea of how I want the program to function... after setting up the player object (or objects, we will see how it goes,) and loading in the rooms into an array, I call a function to print out the description from my room objects, wait for player input, and pass the input to my text parser and logic functions.

The main sticking point, the main thing that my Java classes never really covered is GUI. To be honest, I would be happy with a DOS window. Something that I could pass a CLS command to every once in a while and has a recognizable input cursor. I suppose dedicated text boxes could work too...

I have also worked on a tile-based RPG tutorial that used JFrame, but I do not know how to pass text to a JFrame window or get input from it.

What else is out there? What is the simplest to begin with?
Was This Post Helpful? 0
  • +
  • -

#9 drjoliv  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 60
  • Joined: 27-August 13

Re: Text Based Game with a Window

Posted 04 June 2014 - 01:46 PM

This link will give you a very solid introduction into Java GUIs. Java swing
This link will give you information on how to use different types of text boxes.
Text Components

Below is code that takes your Hello world and adds it into a GUI.

public class HelloWorld extends javax.swing.JFrame {
    
    HelloWorld(String s){
        super(s);
        
        
        //Create text label 
        javax.swing.JLabel text = new javax.swing.JLabel("Hello World");
        
     
        //Adds text to Content Pane
        getContentPane().add(text);
        
        //Resize JFrame to fit its content
        pack();
        
        //Close Frame when and extis when X button is clicked
        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        
        
    }
    
    public static void main(String [] args){
        
        //Use Event Dispatch Thread because most Swing compoents are not thread safe
        java.awt.EventQueue.invokeLater(new Runnable(){
           public void run(){
               
               //Creates new Frame and makes it visible
               new HelloWorld("Hello World").setVisible(true);
           
           }
           
        });
        
    }
    
}



Was This Post Helpful? 1
  • +
  • -

#10 Lame_Brain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-June 14

Re: Text Based Game with a Window

Posted 04 June 2014 - 03:15 PM

Thanks for the code DRJOLIV. It definitely gets Hello World on the screen, but I don't think it is quite what I am looking for...
Thank you also for the links, I will start Delving into them immediately.

Here is the code I adapted from DRJOLIV's code. I don't think JLabels are what I am looking for, since it only prints the last item from my menu.
package newText;

import java.awt.Dimension;

import javax.swing.JFrame;
	public class HelloWorld extends javax.swing.JFrame {
	    
	    HelloWorld(String s){
	        super(s);	        
			
	        //Create text label 
	        javax.swing.JLabel text1 = new javax.swing.JLabel("MAIN MENU");
	        javax.swing.JLabel text2 = new javax.swing.JLabel("New Game");
	        javax.swing.JLabel text3 = new javax.swing.JLabel("Load Game");
	        javax.swing.JLabel text4 = new javax.swing.JLabel("Character Menu");
	        javax.swing.JLabel text5 = new javax.swing.JLabel("Quit Game");
	        
	     
	        //Adds text to Content Pane
	        getContentPane().add(text1);
	        getContentPane().add(text2);
	        getContentPane().add(text3);
	        getContentPane().add(text4);
	        getContentPane().add(text5);
	        
	        //Resize JFrame to fit its content
	       setSize(500,500);
	       setResizable(false);
	        
	        //Close Frame when and exits when X button is clicked
	        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
	        
	        
	    }
	    
	    public static void main(String [] args){
	        
	        //Use Event Dispatch Thread because most Swing components are not thread safe
	        java.awt.EventQueue.invokeLater(new Runnable(){
	           public void run(){
	               
	               //Creates new Frame and makes it visible
	               new HelloWorld("Text RPG").setVisible(true);
	           
	           }
	           
	        });
	        
	    }
	    
	}


Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Text Based Game with a Window

Posted 04 June 2014 - 03:23 PM

Know that swing has more components to choose from, it is a matter of finding the one which will match to your need.
But also know that you can always concatenate text so that you can have the old text and new one together... but every component has their typical use.
Was This Post Helpful? 0
  • +
  • -

#12 Lame_Brain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-June 14

Re: Text Based Game with a Window

Posted 04 June 2014 - 03:39 PM

If you guys are interested, here is what I have so far for the main menu. It prints out to the console at the moment, but it is a beginning! :clap:

Game Class
package code.main;

import java.util.Scanner;

public class Game {
	
	public static int gameState = 1;
	
	public static void main(String[] args){		
		String scrubbedInput;
		Scanner in = new Scanner(System.in);
		while(gameState > 0){  //MAIN MENU LOOP
			
			System.out.println("Game State = "+ gameState);
			
			System.out.println("TEXT RPG MAIN MENU"); //game menu
			System.out.println("[C]haracter Menu");
			System.out.println("[N]ew Game");
			System.out.println("[L]oad Game");
			System.out.println("[Q]uit");
			
			String input = in.nextLine(); //gets input from player
			scrubbedInput = input.toLowerCase();
			scrubbedInput = scrubbedInput.substring(0,1);
			System.out.println(scrubbedInput.substring(0,1));
			switch(scrubbedInput){
			case "c": gameState = 4; System.out.println("CHARACTER MENU");  //takes player to a menu where they can create, edit, or delte characters
				break;
			case "n": gameState = 3; System.out.println("NEW GAME");  //initializes first run variables and has user pick a character to be main character
				break;
			case "l": gameState = 2; System.out.println("LOAD GAME");	//loads save data and starts game with it
				break;
			case "q": gameState = -1; System.out.println("QUIT GAME");
				break;
			default:  gameState = 1; System.out.println("ERROR");
				break;
			}
		}
		System.exit(0);
		
	}	
	
}





Player Class
package code.main;

public class Player {
	private String name = new String();	
	private boolean lost;
	private int xPos, yPos, light, hunger, health, wounds, mana, fatigue, money;
	private int str, dex, con, iq, wil, per, strBonus, dexBonus, conBonus, iqBonus, wilBonus, perBonus;
	
	public void setName(String text) { this.name = text; }
	public void setLost(boolean value) { this.lost = value; }
	public void setStats(int s, int d, int c, int i, int w, int p){
		this.str = s;
		this.strBonus = (str/2)-5;
		this.dex = d;
		this.dexBonus = (dex/2)-5;
		this.con = c;
		this.conBonus = (con/2)-5;
		this.iq = i;
		this.iqBonus = (iq/2)-5;
		this.wil = w;
		this.wilBonus = (wil/2)-5;
		this.per = p;
		this.perBonus = (per/2)-5;
	}
	public void setXPos(int value) { this.xPos = value; }
	public void incXPos() { this.xPos += 1; }
	public void decXPos() { this.xPos -= 1; }
	public void setYPos(int value) { this.yPos = value; }
	public void incYPos() { this.yPos += 1; }
	public void decYPos() { this.yPos -= 1; }
	public void setLight(int value) { this.light = value; }
	public void setHunger(int value) { this.hunger = value; }
	public void setHealth(int value) { 
		this.health = value + this.conBonus;
		this.wounds = 0;
	}
	public void setWounds(int value) { this.wounds = value; }
	public void setMana(int value) { 
		this.mana = value + this.wilBonus;
		this.fatigue = 0;
	}
	public void setFatigue(int value) { this.fatigue = value; }
	public void incMoney(int value) { this.money += value; }
	public void decMoney(int value) { this.money -= value; }
	
	public String getName() {return this.name;}
	public boolean getLost() {return this.lost;}
	public int getXPos() {return this.xPos;}
	public int getYPos() {return this.yPos;}
	public int getLight() {return this.light;}
	public int getHunger() {return this.hunger;}
	public int getHealth() {return this.health;}
	public int getWounds() {return this.wounds;}
	public int getMana() {return this.mana;}
	public int getFatigue() {return this.fatigue;}
	public int getMoney() {return this.money;}
	public int getStr() {return this.str;}
	public int getStrBonus() {return this.strBonus;}
	public int getDex() {return this.dex;}
	public int getDexBonus() {return this.dexBonus;}
	public int getCon() {return this.con;}
	public int getConBonus() {return this.conBonus;}
	public int getIQ() {return this.iq;}
	public int getIQBonus() {return this.iqBonus;}
	public int getWil() {return this.wil;}
	public int getWilBonus() {return this.wilBonus;}
	public int getPer() {return this.per;}
	public int getPerBonus() {return this.perBonus;}
}



Room Class
package code.main;

public class Room {
	private String name, darkDesc, shortDesc, fullDesc;
	private int type, north, west, south, east;
	private boolean lit;
	
	public void setName(String text) { this.name = text; }
	public void setDesc(String dark, String brief, String full) { 
		this.darkDesc = dark;
		this.shortDesc = brief;
		this.fullDesc = full;
	}
	public void setDoors(int n, int w, int s, int e){
		this.north = n;
		this.west = w;
		this.south = s;
		this.east = e;
	}
	public void setType(int t) {this.type = t;}
	public void setLit(boolean l) {this.lit = l; }
	
	public String getName() {return this.name;}
	public String darkName() {return this.darkDesc;}
	public String shortName() {return this.shortDesc;}
	public String fullName() {return this.fullDesc;}
	public int northDoor() {return this.north;}
	public int westDoor() {return this.west;}
	public int southDoor() {return this.south;}
	public int eastDoor() {return this.east;}
	public int getType() {return this.type;}
	public boolean getLit() {return this.lit;}

}


Was This Post Helpful? 0
  • +
  • -

#13 drjoliv  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 60
  • Joined: 27-August 13

Re: Text Based Game with a Window

Posted 04 June 2014 - 03:58 PM

The code i posted was just an example of using Swing Components. And not specific to your game your going to have to do some reading now before you can even make a simple game :(/> . Its just the nature of it all But i will give you one more link for Menus and show you this code to use Menus


import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class HelloWorld extends javax.swing.JFrame {

    //Create text label 
    javax.swing.JLabel text = new javax.swing.JLabel("Hello World");

    HelloWorld(String s) {

        super(s);

        //Create and set JMenuBar
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        //Add Menu and Menu Items
        JMenu menu = new JMenu("Main");
        menu.add(new JMenuItem(new HelloAction("Hello")));
        menu.add(new JMenuItem(new HelloAction("Wazzup")));
        menu.add(new JMenuItem(new HelloAction("Have a good Day")));
        menu.add(new JMenuItem(new HelloAction("See Ya later")));

        menuBar.add(menu);

        //Adds text to Content Pane
        getContentPane().add(text);

        //Resize JFrame to fit its content
        pack();

        //Close Frame when and extis when X button is clicked
        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    }

    //Create Actions for your JMenuItems
    private class HelloAction extends AbstractAction {

        String name;

        public HelloAction(String s) {

            //Set Name field of the Action
            putValue(Action.NAME, (name = s));

        }

        //Each Action changes the text in the JLabel
        @Override
        public void actionPerformed(ActionEvent e) {
            text.setText(name); //To change body of generated methods, choose Tools | Templates.
        }
    }

    public static void main(String[] args) {

        //Use Event Dispatch Thread because most Swing compoents are not thread safe
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                //Creates new Frame and makes it visible
                new HelloWorld("Hello World").setVisible(true);

            }
        });
    }
}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1