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 2 of 2

Thread: How to put my code onto the window.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to put my code onto the window.

    I have made a text-adventure, I got text, input, ect. I have a window that pops up now, but I want to know how to put the text into the console.

    Game.java
    package main;
     
    /*
     * Shzylo's Text Adventure v1.0
     * 
     */
    import java.util.Scanner;
    import java.awt.*;
     
    import javax.swing.*;
     
    public class Game extends Variables implements Runnable {
    	private static final long serialVersionUID = 1L;
     
    	private JFrame frame = new JFrame();
     
    	public int height = 350;
    	public int width = 650;
     
        public Game() {
        	Dimension size = new Dimension( width, height);
        	setPreferredSize(size);
        }
     
        public static void main(String[] args) {
        	Game game = new Game();
        	game.frame.setResizable(false);
        	game.frame.setTitle("Shzylo's Text Adventure v0.1 BETA");
        	game.frame.add(game);
        	game.frame.pack();
    		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game.frame.setLocationRelativeTo(null);
    		game.frame.setVisible(true);
     
    		Main.start();
        }
     
    	public void run() {
    		while(running) {
    		}
     
    	}
     
    }


    Main.java
    package main;
     
    public class Main extends Variables {
    	private static final long serialVersionUID = 1L;
    	static int x = 0;
    	static int y = 0;
     
    	static int stickValue = 1;
     
    	static boolean stickObject = true;
     
        public static void start() {
        	running = true;
            System.out.println("You wake up lost, washed ashore on a beach. All you see in sight is a stick, a rock, and lots of sand.");
            System.out.println("Where would you like to go? North, South, East, West");
            System.out.println("type 'help' if you need help.");
            string = i.nextLine();
     
            do {
            if(string.equalsIgnoreCase("north")) {
                north();  
     
            } else if(string.equalsIgnoreCase("south")) {
                south();
     
            } else if(string.equalsIgnoreCase("east")) {
                east();    
     
            } else if(string.equalsIgnoreCase("west")) {
            	stickValue--;
     
            	if(stickValue == 0) {
            		stickObject = false;
            		if(stickObject = false) {
            			west();
            		}
            	}
     
            } else if(string.equalsIgnoreCase("help")) {
                help();
     
            } else {
            	System.out.println("I do not recognize that command..");
            	sleep();
            	System.out.println("Please choose an action:");
            	string = i.nextLine();
     
            }
            } while(x == y);
        }
     
        public static void north() {
            System.out.println("You have traveled north into a forest, where you see a turtle resting on a rock, nothing special in this direction.");
            sleep();
            System.out.println("Please choose an action:");
            string = i.nextLine();
        }
        public static void south() {
            System.out.println("I wouldn't go that way, there is an aweful lot of water there.");
            sleep();
            System.out.println("Please choose an action:");
            string = i.nextLine();
        }
        public static void east() {
            System.out.println("Oh, a snake, it's staring and hissing at you. I would get some defense before I go there.");
            sleep();
            System.out.println("Please choose an action:");
            string = i.nextLine();
        }
        public static void west() {
            System.out.println("I see rope, and some bass in a small pool of water");
            sleep();
            System.out.println("Please choose an action:");
            string = i.nextLine();
        }
        public static void help() {
        	System.out.println("Where would you like to go? North, South, East, West");
        	sleep();
        	string = i.nextLine();
        }
     
     
        public static void stop() {
        	running = false;
        }
    }


    Variables.java
    package main;
     
    import java.awt.Canvas;
    import java.util.Scanner;
     
    public class Variables extends Canvas {
    	private static final long serialVersionUID = 1L;
     
        static Scanner i = new Scanner(System.in); 
        public static String string = "";
        private Thread thread;
     
        protected static boolean running = false;
     
        String itemName;
        int value;
     
        public static void sleep() {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public static void space() {
            System.out.println("");
        }
    }


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

    Default Re: How to put my code onto the window.

    how to put the text into the console.
    Are you talking about using the System.out.print() method? It writes to the console.

    Or are you asking how to write Swing GUI? See the tutorial:
    http://docs.oracle.com/javase/tutori...ing/index.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  2. Beginner - Where to put code to connect to database
    By newbiee in forum JDBC & Databases
    Replies: 1
    Last Post: May 17th, 2012, 05:40 AM
  3. help taking code from java class put it in JSP page.
    By anavagomez in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2012, 10:57 AM
  4. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM
  5. 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

Tags for this Thread