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! 😀
Your
gwobject, despite being an instance of JFrame, is not the JFrame which you are displaying, because in yourGenerateWorldconstructor, a JFrame local to the constructor itself is created, designed, and shown. Instead of creating a local JFrame, use thesuperconstructor and apply the methods to the super instance.Also, in this situation, you should use
repaint(), notupdate(Graphics), and you should call it after your modification rather than before.