10 Replies - 598 Views - Last Post: 08 February 2018 - 12:26 PM Rate Topic: -----

#1 Smuge316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-October 17

how do i make my ide console print to a window

Posted 08 February 2018 - 04:21 AM

I have made a text based game in java and the game its self works exactly how i want it to work but, i need the games outputs to be printed to DOS like window instead of the IDE's console, im currently using
system.out.prinln 
to make something print and i'm assuming i will have to change this.
Is This A Good Question/Topic? 0
  • +

Replies To: how do i make my ide console print to a window

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 04:50 AM

Programs are run in consoles, not in IDEs so it will print to the console. IDEs are for development only
Was This Post Helpful? 0
  • +
  • -

#3 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 05:03 AM

Open a command prompt window and execute the program there.
java TheClassName
The output from the println statements will print on the console.
Was This Post Helpful? 0
  • +
  • -

#4 Smuge316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-October 17

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 06:48 AM

i think you've misunderstood what im stuck with. im trying to use jtextfield and jtextareas to work as a DOS like window where my text based game can work in a seperate window.

This is the main code i have
/*
@author Alex Chadwick
*/
package monsterrun;

import java.util.Random;
import java.util.Scanner;


public class MonsterRun {
    public static void main(String[] args) {
       
        //system objects
        Scanner in = new Scanner(System.in);
        Random rand = new Random();
        
        // Game variables
        String[] enemies = { "Skeleton",
            "goblin", "Assassin", "Wolf" , "Swarm Of Bats" , "Kobold" ,
            "Orc" , "Uruk" , "Redcap" ,  "Grung" ,  "Neogi" , 
            "Gnoll" , "Imp" ,  "Mephit" , "Swarm Of Rats" , "Gremlin" ,
            "Zombie" , "Clay Golem" , "Stone Golem" ,"Netherling","Fogseeker",
            "Cultist" , "Bandit" ,  "Ghoul" ,  "Gargoyle" ,  "Tunneling Worm",
            "Black Slime" , "Swarm Of Spiders" ,};
        
        //enemy variables
        int maxEnemyHealth = 30;
        int enemyAttackDamage = 50;
        
        //boss variable
        /*String[] boss = { "balrog",
          "Dragon", "Master Assassin", "DireWolf", "Vampire" };
        int maxBossHealth = 300;
        int minBossHealth = 100;
        int maxBossAttackDMG = 50;
        int minBossAttackDMG = 10;
       */         
                
        //player variables
        int health = 100;
        int attackDamage = 24;
        int numHealthPots = 3; // number of potion you start with
        int healthPotionHealAmount = 30;
        int healthPotiondropChance = 70; //drop chance/percentage
        int xp = 20; 
        int xpTotal = 0;
        int gold = 10; 
        int goldTotal = 0; 
        int totalScore = 0;
        
        //game variables
        /* for every 10 enemies killed summon a boss
        */
        
        
        boolean running = true;
        
        System.out.println(" MonsterRun Dungeon");
        System.out.println("#############################################################################");
        System.out.println("#                                                                           #");
        System.out.println("#                                Welcome To                                 #");
        System.out.println("#                                                                           #");
        System.out.println("#  #     #                                          ######                  #");
        System.out.println("#  ##   ##  ####  #    #  ####  ##### ###### #####  #     # #    # #    #   #");
        System.out.println("#  # # # # #    # ##   # #        #   #      #    # #     # #    # ##   #   #");
        System.out.println("#  #  #  # #    # # #  #  ####    #   #####  #    # ######  #    # # #  #   #");
        System.out.println("#  #     # #    # #  # #      #   #   #      #####  #   #   #    # #  # #   #");
        System.out.println("#  #     # #    # #   ## #    #   #   #      #   #  #    #  #    # #   ##   #");
        System.out.println("#  #     #  ####  #    #  ####    #   ###### #    # #     #  ####  #    #   #");
        System.out.println("#                                                                           #");
        System.out.println("#############################################################################");
        System.out.println("-----------------------------------------------------------------------------");
        System.out.println("You are Jerry the farmer.");
        System.out.println("Whilst Jerry was out of town selling his wares, when he return");
        System.out.println("to see his hovel trashed and his daughter missing, he assumed the worst,");
        System.out.println("owever he wouldn't give looking for his daughter. Its up to you to go to");
        System.out.println("the Cave where the monsters live, slay all the monsters and rescue your");
        System.out.println("daughter. And a little bit of gold from slaying the monsters always helps.");
        // the story of the game ^
        
        
        GAME:
        while(running) {
            System.out.println("------------------------------------------------------------------------");
            
            int enemyHealth = rand.nextInt(maxEnemyHealth);
            String enemy = enemies[rand.nextInt(enemies.length)];
            System.out.println("\t# " + enemy + " has appeared! #\n");
            /*  #skeleton has appeared. The comand above will take a random name from the
		enemies string so you could fight any of the enemys in the string.
	    */
           
                while(enemyHealth > 0 ){
                    System.out.println("\t Your HP is:" + health);
                    System.out.println("\t" + enemy + "'s HP is:" +enemyHealth);
                    System.out.println("\n\t What would you like to do?");
                    System.out.println("\t1. Attack!");
                    System.out.println("\t2. Drink health potion");
                    System.out.println("\t3. Run Away!");
                        if(health < 1){
                            System.out.println("");
                            break;
                        }
                    String input = in.nextLine();
                    switch (input) {
                        case "1":
                            int damageDealt = rand.nextInt(attackDamage);
                            int damageTaken = rand.nextInt(enemyAttackDamage);
                                enemyHealth -= damageDealt;
                                health -= damageTaken;
                            System.out.println("\t> You strike the " + enemy + " for " + damageDealt + " damage.");
                            System.out.println("\t> You recieve " + damageTaken + " in retalliation!");
                            if(health < 1) {
                                System.out.println("\t> You have taken too much damage, you are to weak too continue!");
                            }   break;

                        case "2":
                            if(numHealthPots > -0) {
                                health += healthPotionHealAmount;
                                    numHealthPots--;
                                System.out.println("\t> You drink a health potion, healing yourself for " +healthPotionHealAmount + "."
                                        + "\n\t> You now have " + health + " HP "
                                                + "\n\t> You have " +numHealthPots + " health potions left. \n");
                            }
                            else {
                                System.out.println("\t> You don't have any health potions left, defeat an enemy for a chance to get one");
                            }   break;
                        case "3":
                            System.out.println("\tYou run away from" + enemy );
                            continue GAME;
                        default:
                            System.out.println("\tInvalid Command");
                            break;
                    }
                }   
            
            if(health < 1){
                System.out.println("You Crawl out of the dungeon, bearly escaping with your life.");
                break;
            } 
            
            
            // the code below activates after you reduce the enemy to 0 health
            System.out.println("---------------------------------------------------------");
            int xpGained = rand.nextInt(xp);
                xpTotal += xpGained;
            int goldGained = rand.nextInt(gold);
                goldTotal += goldGained;
            System.out.println("#" + enemy + " was defeated! #");
            System.out.println("# you gained " + xpGained + "xp for defeating the " + enemy);
            System.out.println("# The " + enemy + " Dropped " + goldGained + " Gold");
            System.out.println("# You have " + health + " Hp left. #");
            System.out.println("# Your Xp is " + xpTotal + " in total");
            System.out.println("# You have " + goldTotal + " Gold in total");
            totalScore = xpTotal += goldTotal;
            
            if(rand.nextInt(100) < healthPotiondropChance) {
                numHealthPots++;
                System.out.println("# The " + enemy + " dropped a health potion #");
                System.out.println("# You now have " + numHealthPots + " health potion(s). #");
            }
            System.out.println("---------------------------------------------");
            System.out.println("What would you like to do now?");
            System.out.println("1. Continue fighting");
            System.out.println("2. Exit Dungeon");
           
            String input = in.nextLine();
             
            while(!input.equals("1") && !input.equals("2")) {
                System.out.println("Invalid Command!");
                input = in.nextLine(); 
            }
            
            if(input.equals("1")){
                System.out.println("You continue on your advanture!");
            } 
           else if (input.equals("2")) {
                System.out.println("You exit the dungeon, successful from your adventure");
                break;
            }
            
        }
        System.out.println("Your total Score is " + totalScore);
        System.out.println("#############");
        System.out.println("# GAME OVER #");
        System.out.println("#############");
        
    }
    
}




this is the Jframe code that i currently have
package javaswingtutorial;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Panel{
    
    JTextArea A1 = new JTextArea(45,80);
    JTextField T1 = new JTextField(40);
   

    public static void main(String args[]){
        JFrame myFrame = new JFrame("MonsterRun");
        myFrame.setSize(640,480);
        myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        myFrame.setLocationRelativeTo(null);
        myFrame.setBackground(Color.black);
    }
     
}



Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 06:59 AM

Sorry, this reads a whole lot like "I need to create a Java GUI but can't be bothered to learn about it."

If you actually want to create a Java GUI, at least get a Hello World programming running. See how far you can go from there.

From what you have, you need to add those components to the JFrame. Again, this will be easier to understand if you just take a little time and understand the basics.

https://docs.oracle....ents/frame.html
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 07:26 AM

Do, instead of

Quote

i need the games outputs to be printed to DOS like window instead of the IDE's console


you mean

"i need the games outputs to be printed to DOS like window instead of the console"

?
Was This Post Helpful? 0
  • +
  • -

#7 Smuge316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-October 17

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 10:52 AM

ive gotten to the point where i have a jTextField and a JTextArea where i want them but i still cant get it so the text displays in the JTextArea. i found that i could use
textArea.append();
but i dont know how to implement this.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 11:17 AM

The answer to my question is ..?
Was This Post Helpful? 0
  • +
  • -

#9 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 11:17 AM

Quote

how to implement this

Look at the API doc for the append() method to see what kind of args it takes.
Then try some tests to see what it does.
Was This Post Helpful? 0
  • +
  • -

#10 Smuge316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-October 17

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 12:10 PM

View Postg00se, on 08 February 2018 - 11:17 AM, said:

The answer to my question is ..?

yes. i want it so my game is in a DOS like window, so i can input into the JtextField and get the output in the JtextArea
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2600
  • View blog
  • Posts: 4,153
  • Joined: 21-June 11

Re: how do i make my ide console print to a window

Posted 08 February 2018 - 12:26 PM

DOS-like windows don't have JTextFields and JTextAreas. A DOS-like window is what you get when you run your application in a terminal (more or less DOS-like depending on which terminal - if you run it in Windows' command prompt, it'll be as DOS-like as it gets).

You can rewrite your application to use a JTextArea and a JTextField, but if all you want is a DOS-like window, that'd be overkill (and not actually all that DOS-like unless you heavily change the behavior and optics of both components). It'll also be more work than you might think because blocking calls like readLine don't fit with the event-driven paradigm of GUI programming, so you'll have to rewrite your entire logic to be event driven.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1