Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello , i have a problem with displaying text when the user input a color it onl

ID: 3757965 • Letter: H

Question

Hello , i have a problem with displaying text

when the user input a color it only shows the default which is You are in "color" Room

i want it to desplay this text depending on each user input which is this:

The colors:

From the green room, there are doors to the brown, pink and blue rooms.
From the pink room, there are doors to the green, brown and blue rooms.
From the brown room, there are doors to the pink, green and red rooms.
From the blue room there are doors to the green, pink and yellow rooms
From the red room, there are doors to the brown and yellow rooms.
From the yellow room there are doors to the red, blue and gold rooms.
From the gold room, there is a door to the yellow room.

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.*;
import java.util.Vector;

public class Project3 extends JFrame implements ActionListener {

private static int xpos = 0, ypos = 0;
private static int xsize = 800, ysize = 500;

private JPanel northPanel, centerPanel;
private JButton pushButton, popButton, dumpButton, exitButton;
private JTextField colorField;
private JTextField codeField;
private JTextArea outputArea;

private Map<String, Vector<String>> validColorsPerColor;
private Stack<Room> roomStack;
private Room currentRoom;

public static void main(String[] args) {
Project3 tpo = new Project3();
}

public Project3() {
this.initValidColorsPerColor();
this.currentRoom = new Room("green");

addScreenComponents();

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

setSize(xsize, ysize);
setLocation(xpos, ypos);
setVisible(true);

}

public Stack<Room> getRoomStack() {
if (this.roomStack == null) {
this.roomStack = new Stack<Room>();
}

return this.roomStack;
}

private void initValidColorsPerColor() {
this.validColorsPerColor = new HashMap<String, Vector<String>>();

Vector<String> greenVector = new Vector<String>();

greenVector.add("brown");
greenVector.add("pink");
greenVector.add("blue");
  

this.validColorsPerColor.put("green", greenVector);

Vector<String> pinkVector = new Vector<String>();
  
pinkVector.add("green");
pinkVector.add("brown");
pinkVector.add("blue");
this.validColorsPerColor.put("pink",pinkVector);
  

Vector<String> brownVector = new Vector<String>();
brownVector.add("pink");
brownVector.add("green");
brownVector.add("red");
this.validColorsPerColor.put("brown", brownVector);

Vector<String> blueVector = new Vector<String>();
blueVector.add("green");
blueVector.add("pink");
blueVector.add("yellow");
this.validColorsPerColor.put("blue", blueVector);

Vector<String> redVector = new Vector<String>();
redVector.add("brown");
redVector.add("yellow");
this.validColorsPerColor.put("red", redVector);

Vector<String> yellowVector = new Vector<String>();
yellowVector.add("red");
yellowVector.add("blue");
yellowVector.add("gold");
this.validColorsPerColor.put("yellow", yellowVector);

Vector<String> goldVector = new Vector<String>();
goldVector.add("yellow");
this.validColorsPerColor.put("gold", goldVector);
}

public boolean isAdjacentRoom(String sourceRoomColor, String targetRoomColor) {
return this.validColorsPerColor.get(sourceRoomColor).contains(
targetRoomColor);
}

public void addScreenComponents() {
northPanel = new JPanel();
northPanel.add(new JLabel("Enter A Color: "));
colorField = new JTextField("", 15);
northPanel.add(colorField);
northPanel.add(new JLabel("And A Code: "));
codeField = new JTextField("", 5);
northPanel.add(codeField);

pushButton = new JButton("Push");
northPanel.add(pushButton);
pushButton.addActionListener(this);
popButton = new JButton("Pop");
northPanel.add(popButton);
popButton.addActionListener(this);
dumpButton = new JButton("Dump");
northPanel.add(dumpButton);
dumpButton.addActionListener(this);
exitButton = new JButton("Exit");
northPanel.add(exitButton);
exitButton.addActionListener(this);

getContentPane().add("North", northPanel);

centerPanel = new JPanel();
outputArea = new JTextArea("Who Dares Enter.... The Temple of Gloom!",
20, 60);
centerPanel.add(outputArea);
getContentPane().add(centerPanel, "Center");

}

public void resetGame() {
this.getRoomStack().clear();
this.currentRoom = new Room("green");
this.outputArea.setText("you died - starting again in room green");
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == exitButton) {
dispose();
System.exit(0);
}

if (e.getSource() == popButton) {
if (this.getRoomStack().isEmpty()) {
outputArea.setText("stack is empty");
return;
}

String newcolor = colorField.getText();
int code = !codeField.getText().isEmpty() ? Integer
.parseInt(codeField.getText()) : 0;

Room lastStackElement = this.getRoomStack().pop();

if (newcolor != lastStackElement.getColor()) {
outputArea.setText("invalid color");
this.resetGame();
return;
}

if (code != lastStackElement.getCode()) {
outputArea.setText("invalid code");
this.resetGame();
return;
}

outputArea.setText("returning to " + newcolor + " Room");
}

if (e.getSource() == pushButton) {
String newcolor = colorField.getText();
if (!this.isAdjacentRoom(this.currentRoom.getColor(), newcolor)) {
outputArea.setText("color is bad");
this.resetGame();
return;
}

int newCode = !codeField.getText().isEmpty() ? Integer
.parseInt(codeField.getText()) : 0;
try {
this.currentRoom.setCode(newCode);
} catch (RuntimeException exception) {
outputArea.setText("code has to be a 3-digit code");
return;
}

this.getRoomStack().push(this.currentRoom);
this.currentRoom = new Room(newcolor);

outputArea.setText("You are in " + newcolor + " Room");

}
  
  
  
if (e.getSource() == dumpButton) {
Stack<Room> tmpRoomStack = new Stack<Room>();
System.out.println("Stack Contents Dump: ");
while (!this.getRoomStack().isEmpty()) {
Room tmpRoom = this.getRoomStack().pop();
System.out.format("Color: %s | Code: %s ", tmpRoom.getColor(),
tmpRoom.getCode());
tmpRoomStack.push(tmpRoom);
}

while (!tmpRoomStack.isEmpty()) {
Room tmpRoom = tmpRoomStack.pop();
this.getRoomStack().push(tmpRoom);
}
}
}

public class Room {

protected String color;
protected int code;

Room(String color) {
this.setColor(color);
}

public String getColor() {
return this.color;
}

public void setColor(String color) {
this.color = color;
}

public int getCode() {
return this.code;
}

public void setCode(int code) {
if (code > 1000 || code < 99) {
throw new RuntimeException("has to be a 3 digit code");
}

this.code = code;
}
}

public class Stack<T> extends Vector<T> {

public T pop() {
T toReturn = this.lastElement();
this.removeElementAt(this.size() - 1);

return toReturn;
}

public void push(T element) {
this.addElement(element);
}
  
}
public void displayText(){
this.getRoomStack().push(this.currentRoom);
String pinkVector = colorField.getText();
outputArea.setText(",pasdasdasdasfsdf");
this.getRoomStack().push(this.currentRoom);
this.currentRoom = new Room(pinkVector);

outputArea.setText("You are in " + pinkVector + " Room");

}
}

Explanation / Answer

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Project3 extends JFrame implements ActionListener
{

   private static int xpos = 0, ypos = 0;
   private static int xsize = 800, ysize = 500;

   private JPanel northPanel, centerPanel;
   private JButton pushButton, popButton, dumpButton, exitButton;
   private JTextField colorField;
   private JTextField codeField;
   private JTextArea outputArea;

   private Map<String, Vector<String>> validColorsPerColor;
   private Stack<Room> roomStack;
   private Room currentRoom;

   public static void main(String[] args)
   {
       Project3 tpo = new Project3();
   }

   public Project3()
   {
       this.initValidColorsPerColor();
       this.currentRoom = new Room("green");

       addScreenComponents();

       addWindowListener(new WindowAdapter()
       {
           public void windowClosing(WindowEvent e)
           {
               System.exit(0);
           }
       });

       setSize(xsize, ysize);
       setLocation(xpos, ypos);
       setVisible(true);

   }

   public Stack<Room> getRoomStack()
   {
       if (this.roomStack == null)
       {
           this.roomStack = new Stack<Room>();
       }

       return this.roomStack;
   }

   private void initValidColorsPerColor()
   {
       this.validColorsPerColor = new HashMap<String, Vector<String>>();

       Vector<String> greenVector = new Vector<String>();

       greenVector.add("brown");
       greenVector.add("pink");
       greenVector.add("blue");

       this.validColorsPerColor.put("green", greenVector);

       Vector<String> pinkVector = new Vector<String>();

       pinkVector.add("green");
       pinkVector.add("brown");
       pinkVector.add("blue");
       this.validColorsPerColor.put("pink", pinkVector);

       Vector<String> brownVector = new Vector<String>();
       brownVector.add("pink");
       brownVector.add("green");
       brownVector.add("red");
       this.validColorsPerColor.put("brown", brownVector);

       Vector<String> blueVector = new Vector<String>();
       blueVector.add("green");
       blueVector.add("pink");
       blueVector.add("yellow");
       this.validColorsPerColor.put("blue", blueVector);

       Vector<String> redVector = new Vector<String>();
       redVector.add("brown");
       redVector.add("yellow");
       this.validColorsPerColor.put("red", redVector);

       Vector<String> yellowVector = new Vector<String>();
       yellowVector.add("red");
       yellowVector.add("blue");
       yellowVector.add("gold");
       this.validColorsPerColor.put("yellow", yellowVector);

       Vector<String> goldVector = new Vector<String>();
       goldVector.add("yellow");
       this.validColorsPerColor.put("gold", goldVector);
   }

   public boolean isAdjacentRoom(String sourceRoomColor,
           String targetRoomColor)
   {
       System.out.println(this.validColorsPerColor.get(sourceRoomColor)
               .contains(targetRoomColor));
       return this.validColorsPerColor.get(sourceRoomColor).contains(
               targetRoomColor);
   }

   public void addScreenComponents()
   {
       northPanel = new JPanel();
       northPanel.add(new JLabel("Enter A Color: "));
       colorField = new JTextField("", 15);
       northPanel.add(colorField);
       northPanel.add(new JLabel("And A Code: "));
       codeField = new JTextField("", 5);
       northPanel.add(codeField);

       pushButton = new JButton("Push");
       northPanel.add(pushButton);
       pushButton.addActionListener(this);
       popButton = new JButton("Pop");
       northPanel.add(popButton);
       popButton.addActionListener(this);
       dumpButton = new JButton("Dump");
       northPanel.add(dumpButton);
       dumpButton.addActionListener(this);
       exitButton = new JButton("Exit");
       northPanel.add(exitButton);
       exitButton.addActionListener(this);

       getContentPane().add("North", northPanel);

       centerPanel = new JPanel();
       outputArea = new JTextArea(
               "Who Dares Enter.... The Temple of Gloom!", 20, 60);
       centerPanel.add(outputArea);
       getContentPane().add(centerPanel, "Center");

   }

   public void resetGame()
   {
       this.getRoomStack().clear();
       this.currentRoom = new Room("green");
       this.outputArea.setText("you died - starting again in room green");
   }

   public void actionPerformed(ActionEvent e)
   {

       if (e.getSource() == exitButton)
       {
           dispose();
           System.exit(0);
       }

       if (e.getSource() == popButton)
       {
           if (this.getRoomStack().isEmpty())
           {
               outputArea.setText("stack is empty");
               return;
           }

           String newcolor = colorField.getText();
           int code = !codeField.getText().isEmpty() ? Integer
                   .parseInt(codeField.getText()) : 0;

           Room lastStackElement = this.getRoomStack().pop();

           if (newcolor != lastStackElement.getColor())
           {
               outputArea.setText("invalid color");
               this.resetGame();
               return;
           }

           if (code != lastStackElement.getCode())
           {
               outputArea.setText("invalid code");
               this.resetGame();
               return;
           }

           outputArea.setText("returning to " + newcolor + " Room");
       }

       if (e.getSource() == pushButton)
       {
           String newcolor = colorField.getText();
           if (!this.isAdjacentRoom(this.currentRoom.getColor(), newcolor
                   .toLowerCase()))
           {
               outputArea.setText("color is bad");
               this.resetGame();
               return;
           }

           int newCode = !codeField.getText().isEmpty() ? Integer
                   .parseInt(codeField.getText()) : 0;
           try
           {
               this.currentRoom.setCode(newCode);
           }
           catch (RuntimeException exception)
           {
               outputArea.setText("code has to be a 3-digit code");
               return;
           }

           this.getRoomStack().push(this.currentRoom);
           // this.currentRoom = new Room(newcolor);

           outputArea.setText("You are in " + newcolor + " Room");
           displayTextValues();
       }

       if (e.getSource() == dumpButton)
       {
           Stack<Room> tmpRoomStack = new Stack<Room>();
           System.out.println("Stack Contents Dump: ");
           while (!this.getRoomStack().isEmpty())
           {
               Room tmpRoom = this.getRoomStack().pop();
               System.out.format("Color: %s | Code: %s ", tmpRoom
                       .getColor(), tmpRoom.getCode());
               tmpRoomStack.push(tmpRoom);
           }

           while (!tmpRoomStack.isEmpty())
           {
               Room tmpRoom = tmpRoomStack.pop();
               this.getRoomStack().push(tmpRoom);
           }
       }
   }

   private void displayTextValues()
   {
       Set<String> keys = this.validColorsPerColor.keySet();
       Iterator iterator = keys.iterator();

       String str = " ";
       while (iterator.hasNext())
       {
           String keyValue = (String) iterator.next();
           str += "From " + keyValue + " room, there are doors to the ";
           Vector<String> values = this.validColorsPerColor.get(keyValue);
           System.out.println(values.size());
           System.out.println(values.toString());
           for (int j = 0; j < values.size(); j++)
           {

               if (j == values.size() - 1)
                   str += " and " + values.get(j) + " rooms.";
               else
                   str += values.get(j) + ", ";

           }
           str += " ";
       }
       outputArea.append(str);
   }

   public class Room
   {

       protected String color;
       protected int code;

       Room(String color)
       {
           this.setColor(color);
       }

       public String getColor()
       {
           return this.color;
       }

       public void setColor(String color)
       {
           this.color = color;
       }

       public int getCode()
       {
           return this.code;
       }

       public void setCode(int code)
       {
           if (code > 1000 || code < 99)
           {
               throw new RuntimeException("has to be a 3 digit code");
           }

           this.code = code;
       }
   }

   public class Stack<T> extends Vector<T>
   {

       public T pop()
       {
           T toReturn = this.lastElement();
           this.removeElementAt(this.size() - 1);

           return toReturn;
       }

       public void push(T element)
       {
           this.addElement(element);
       }

   }

   public void displayText()
   {
       this.getRoomStack().push(this.currentRoom);
       String pinkVector = colorField.getText();
       outputArea.setText(",pasdasdasdasfsdf");
       this.getRoomStack().push(this.currentRoom);
       this.currentRoom = new Room(pinkVector);

       outputArea.setText("You are in " + pinkVector + " Room");

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote