I am trying to make a textArea for my input, but it broke my code. It does not draw the characters or grid any more, and all I see is a small button in the top right corner. Screenshot of output
Here’s my code with the JTextArea:
JavaScript
x
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DnD extends JPanel implements ActionListener {
public static final long serialVersionUID = 1;
public Scanner sc = new Scanner(System.in);
public JFrame window = new JFrame("D&D");
public ArrayList<Person> charactersList = new ArrayList<Person>();
public ArrayList<Person> others = new ArrayList<Person>();
public int colorR = 0;
public int colorG = 0;
public int colorB = 0;
public JButton button;
public JTextArea textArea;
public String textInput = "";
public void run(DnD dnd){
button = new JButton("Enter");
button.addActionListener(dnd);
this.window.setSize(1280, 700);
this.window.setLocation(0, 0);
this.window.setUndecorated(true);
this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.window.add(dnd);
textArea = new JTextArea(20, 1);
textArea.setBounds(1180, 0, 100, 20);
button.setBounds(1205, 20, 50, 25);
this.window.add(button);
this.window.add(textArea);
this.window.setVisible(true);
int turn = 0;
SwingUtilities.updateComponentTreeUI(this.window);
this.window.invalidate();
this.window.validate();
this.window.repaint();
boolean going = true;
Environment environment = new Environment();
while(going){
System.out.println("Entering new room? Enter a bool.");
if(sc.nextBoolean()){
charactersList = environment.refreshOrder(charactersList, sc, this);
others = environment.renderRoom(others, sc, this);
this.repaint();
}
sc.nextLine();
turn ++;
System.out.print("Turn number " + turn + "n");
for(int currentPlayer = 0; currentPlayer < charactersList.size(); currentPlayer++){
System.out.println(charactersList.get(currentPlayer).name + "'s turn");
System.out.println("You have " + this.charactersList.get(currentPlayer).hp + " hp this turn.");
charactersList.get(currentPlayer).Turn(sc, charactersList, this);
this.window.repaint();
}
}
}
public void actionPerformed(ActionEvent e){
String event = e.getActionCommand();
if(event.equals("Enter")){
this.textInput = textArea.getText();
}
}
public void setup(Scanner sc){
boolean going = true;
int x; int y; String name; int size; int hp; int speed; int ac; int r; int g; int b;
while(going){
System.out.print("Name: ");
name = sc.nextLine();
System.out.print("HP: ");
hp = sc.nextInt();
System.out.print("X: ");
x = sc.nextInt();
System.out.print("Y: ");
y = sc.nextInt();
System.out.print("Size: ");
size = sc.nextInt();
System.out.print("Speed: ");
speed = sc.nextInt();
System.out.print("AC: ");
ac = sc.nextInt();
System.out.print("R: ");
r = sc.nextInt();
System.out.print("G: ");
g = sc.nextInt();
System.out.print("B: ");
b = sc.nextInt();
sc.nextLine();
this.charactersList.add(new Person(x, y, name, size, hp, speed, new Color(r, g, b), ac));
System.out.print("Add another character? Enter a boolean: ");
going = sc.nextBoolean();
sc.nextLine();
}
}
public ArrayList<Person> setup(Scanner sc, ArrayList<Person> charactersLists){
boolean going = true;
int x; int y; String name; int size; int hp; int speed; int ac; int r; int g; int b;
while(going){
System.out.print("Name: ");
name = sc.nextLine();
System.out.print("HP: ");
hp = sc.nextInt();
System.out.print("X: ");
x = sc.nextInt();
System.out.print("Y: ");
y = sc.nextInt();
System.out.print("Size: ");
size = sc.nextInt();
System.out.print("Speed: ");
speed = sc.nextInt();
System.out.print("AC: ");
ac = sc.nextInt();
System.out.print("R: ");
r = sc.nextInt();
System.out.print("G: ");
g = sc.nextInt();
System.out.print("B: ");
b = sc.nextInt();
sc.nextLine();
charactersLists.add(new Person(x, y, name, size, hp, speed, new Color(r, g, b), ac));
System.out.print("Add another character? Enter a boolean: ");
going = sc.nextBoolean();
sc.nextLine();
}
return charactersLists;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(colorR, colorG, colorB));
g2d.fillRect(0, 0, 1280, 700);
g2d.setColor(new Color(255 - colorR, 255 - colorG, 255 - colorB));
for(int i = 0; i < 1280; i += 10){
g2d.setColor(new Color(255, 255, 255));
if(i % 20 == 10){
g2d.setColor(new Color(150, 150, 150));
}
g2d.drawLine(i, 0, i, 700);
}
for(int i = 0; i < 700; i += 10){
g2d.setColor(new Color(255, 255, 255));
if(i % 20 == 10){
g2d.setColor(new Color(150, 150, 150));
}
g2d.drawLine(0, i, 1280, i);
}
Person temp = null;
for(int i = 0; i < this.charactersList.size(); i++){
temp = charactersList.get(i);
g2d.setColor(temp.color);
g2d.fillRect(temp.x, temp.y, temp.length, temp.length);
}
for(int i = 0; i < this.others.size(); i++){
temp = others.get(i);
g2d.setColor(temp.color);
g2d.fillRect(temp.x, temp.y, temp.width, temp.height);
}
}
public static void main(String args[]){
DnD dnd = new DnD();
Scanner sc = new Scanner(System.in);
dnd.setup(sc);
dnd.run(dnd);
sc.close();
}
}
What can I do to make my code work with the JTextArea? I want to replace the scanner with it, if I can get it to work.
Advertisement
Answer
A JFrame (which is what your window is) uses a BorderLayout by default, which has a single area in the middle that expands. If you want more than one item in there, add a JPanel into the JFrame first, and then add them to the JPanel instead.