Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:15:09+00:00 2026-06-11T21:15:09+00:00

I need some real help with my Java project. It’s a console controlled JFrame,

  • 0

I need some real help with my Java project. It’s a console controlled JFrame, where the commands you type in the console SHOULD react to the JFrame window, only it doesn’t. This is a big problem. Here’s my code (Engine.java):

package com.boldchicky.commandsrpg.Main;

import java.io.*;

public class Engine {
public static boolean running;
public static GenerateWorld gw;

public static void main(String[] args) throws IOException{
    gw = new GenerateWorld();
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    running = true;
    while(running){
        System.out.println("Enter a command:");
        String command = input.readLine();
        if(command.equalsIgnoreCase("move up")){
            gw.update(gw.getGraphics());
            GenerateWorld.x += GenerateWorld.dx;
            System.out.println("Moved!");
        } else {
            System.out.println(CommandControl.command(command));
        }
        if(CommandControl.add){
            String command2 = input.readLine();
            System.out.println(CommandControl.add(command2));
            CommandControl.add = false;
        }
        else if(CommandControl.remove){
            String command2 = input.readLine();
            System.out.println(CommandControl.remove(command2));
            CommandControl.remove = false;
        }
    }
    if(!running){
        System.exit(0);
    }
}
}

CommandControl.java:

package com.boldchicky.commandsrpg.Main;

import java.util.ArrayList;
import java.io.*;

public class CommandControl {
static ArrayList inv = new ArrayList();
public static boolean add = false;
public static boolean remove = false;
public static boolean move = false;
public static String commandReact = "";

public static String command(String command){
    BufferedReader input = new BufferedReader(new        InputStreamReader(System.in));
    if(command.equalsIgnoreCase("inventory")){
        if(inv.size() == 0){
            commandReact = "Empty!";
        }  else {
            commandReact = "";
            for(int i = 0; i<inv.size(); i++){
                commandReact += (String) inv.get(i) + "\n";
            }
        }
    }

    else if(command.equalsIgnoreCase("add")){
        add = true;
        commandReact = "What do you want to add?";
    }
    else if(command.equalsIgnoreCase("remove")){
        remove = true;
        commandReact = "Waht do you want to remove from your inventory?";
    }
    else{
        commandReact = "Please try again!";
    }
    return commandReact;
}

public static String add(String command){
    if(add){
        inv.add(command);
        commandReact = "Added!";
    }
    return commandReact;
}

public static String remove(String command){
    if(remove){
        if(inv.indexOf(command) != -1){
            inv.remove(inv.indexOf(command));
            commandReact = "Removed!";
        } else {
            commandReact = "Sorry! There is no " + command;
        }
    }
    return commandReact;
}
}

GenerateWorld.java:

package com.boldchicky.commandsrpg.Main;

import java.awt.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;

public class GenerateWorld extends JFrame{
public static int x = 0, y = 0, dx = 15, dy = 15;

public GenerateWorld() throws IOException{
    JFrame frame = new JFrame("Frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);

    Image background = null;
    Image player = null;
    String backgroundPath = "C:\\Users\\Jesse Du\\Documents\\java eclipse\\CommandsRPG\\res\\grass.png";
    String playerPath = "C:\\Users\\Jesse Du\\Documents\\java eclipse\\CommandsRPG\\res\\player.png";
    background = ImageIO.read(new File(backgroundPath));
    player = ImageIO.read(new File(playerPath));
    ImagePanel panel = new ImagePanel(background, player);
    frame.getContentPane().add(panel);
    frame.show();

    frame.setLocationRelativeTo(getRootPane());
    frame.setResizable(false);
    frame.setVisible(true);
}
}

ImagePanel.java:

package com.boldchicky.commandsrpg.Main;

import java.awt.*;

import javax.swing.JPanel;

public class ImagePanel extends JPanel{
private Image backgroundImage;
private Image player;

public ImagePanel(Image background, Image player){
    super();
    this.backgroundImage = background;
    this.player = player;
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(this.backgroundImage, 0, 0, null);
    g.drawImage(this.player, GenerateWorld.x, GenerateWorld.y, null);
}
}

THANK YOU! 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:15:10+00:00Added an answer on June 11, 2026 at 9:15 pm

    Your gw object, despite being an instance of JFrame, is not the JFrame which you are displaying, because in your GenerateWorld constructor, a JFrame local to the constructor itself is created, designed, and shown. Instead of creating a local JFrame, use the super constructor and apply the methods to the super instance.

    public GenerateWorld() throws IOException {
        super("Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // ... etc
    

    Also, in this situation, you should use repaint(), not update(Graphics), and you should call it after your modification rather than before.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to Java and I need some help to extract multiple
I've been wrestling with my limited knowledge of real programming and need some help...
I have hit a real problem. I need to do some Kmeans clustering for
This doesn't need to be a real time solution, but are there some log
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.