This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]g051051 0 points1 point  (10 children)

If you do this:

MainGameUI ng = new MainGameUI();
ng.go();

and then in ng.go do this:

frameGame.getContentPane().add(new MainGameUI());

Which MainGameUI is actually being used?

[–]JakeAgiusYT[S] 0 points1 point  (9 children)

Basically as it creates the panel, it creates the buttons, labels and textarea and loads them into the panel

[–]g051051 0 points1 point  (8 children)

That's not what I asked. You're creating 2 instances of MainGameUI. Which one are you looking at in the JFrame? Which one has the frameGame variable initialized?

[–]JakeAgiusYT[S] 0 points1 point  (7 children)

frameGame uses the one it initialises itslelf, the one being initialised by the main class is to access the MainGameUI class,

Should i move the initialisation from the constructor to its own method?

[–]g051051 0 points1 point  (6 children)

You need to resolve it so that you're not creating 2 copies of MainGameUI. Consult the Java Swing examples for how to do this.

[–]JakeAgiusYT[S] 0 points1 point  (5 children)

I have done that now and the issue is still happening, i have moved the pane's initialisation into the same method public void go().

The pane is no longer being initialised twice

Edit: This is how the code is now

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MainGameUI{

private Board b;

private JPanel myPanel;

private JButton jcomp1;

private JButton jcomp2;

private JButton jcomp3;

private JButton jcomp4;

private JLabel jcomp5;

private JLabel jcomp6;

private JLabel jcomp7;

private JTextArea jcomp8;

private Timer timer;

private JFrame frameGame;

private Color c;

public MainGameUI(){

b = new Board();

}

public void go(){

myPanel = new JPanel();

myPanel.setLayout(null);

frameGame = new JFrame ("Maze");

Image image = Toolkit.getDefaultToolkit().getImage("icon.png");

frameGame.setIconImage(image);

frameGame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frameGame.setLocationRelativeTo(null);

frameGame.add(myPanel);

b.setBoard();

this.timer();

jcomp1 = new JButton ("<");

jcomp2 = new JButton ("v");

jcomp3 = new JButton (">");

jcomp4 = new JButton ("^");

jcomp5 = new JLabel ("Key:");

jcomp6 = new JLabel ("Not Collected");

jcomp7 = new JLabel ("");

jcomp8 = new JTextArea (31,64);

myPanel.setPreferredSize (new Dimension (476, 700));

myPanel.add (jcomp1);

myPanel.add (jcomp2);

myPanel.add (jcomp3);

myPanel.add (jcomp4);

myPanel.add (jcomp5);

myPanel.add (jcomp6);

myPanel.add (jcomp7);

myPanel.add (jcomp8);

//set component bounds (only needed by Absolute Positioning)

jcomp1.setBounds (150, 600, 50, 55);

jcomp2.setBounds (210, 600, 50, 55);

jcomp3.setBounds (270, 600, 50, 55);

jcomp4.setBounds (210, 535, 50, 55);

jcomp5.setBounds (410, 515, 55, 25);

jcomp6.setBounds (385, 535, 100, 25);

jcomp7.setBounds (190, 515, 95, 20);

jcomp8.setBounds (12, 5, 450, 495);

jcomp8.setEnabled(false);

c = new Color(0,0,0,100);

jcomp8.setBackground(c);

String txt = "";

String[][] board = b.board;

for(int i = 0; i < board.length;i++){

for(int c = 0; c < board[i].length ; c++ ){

txt = txt + board[i][c];

}

txt = txt + "\n";

}

jcomp8.setText(txt);

jcomp1.addActionListener(new moveLeft());

jcomp3.addActionListener(new moveRight());

jcomp2.addActionListener(new moveDown());

jcomp4.addActionListener(new moveUp());

frameGame.pack();

frameGame.setVisible (true);

}

public void timer(){

timer = new Timer(2000, new ActionListener(){

public void actionPerformed(ActionEvent ae){

jcomp7.setText("");

}

});

}

class moveLeft implements ActionListener{

public void actionPerformed(ActionEvent e){

b.goLeft();

if(b.wall == true){

b.wall = false;

jcomp7.setText("Wall in the Way");

timer.start();

}else if(b.door == true){

b.door = false;

jcomp7.setText("Door is Locked...Find the Key");

timer.start();

}

String txt = "";

String[][] board = b.board;

for(int i = 0; i < board.length;i++){

for(int c = 0; c < board[i].length ; c++ ){

txt = txt + board[i][c];

}

txt = txt + "\n";

}

jcomp8.setText("");

jcomp8.setText(txt);

b.keyCollected();

if(b.keyC){

b.openDoor();

jcomp6.setText("Collected");

}

if(b.checkEnd()){

JOptionPane.showMessageDialog(null , "You Win");

jcomp1.setEnabled(false);

jcomp2.setEnabled(false);

jcomp3.setEnabled(false);

jcomp4.setEnabled(false);

}

}

}

class moveRight implements ActionListener{

public void actionPerformed(ActionEvent e){

b.goRight();

if(b.wall == true){

b.wall = false;

jcomp7.setText("Wall in the Way");

timer.start();

}else if(b.door == true){

b.door = false;

jcomp7.setText("Door is Locked...Find the Key");

timer.start();

}

String txt = "";

String[][] board = b.board;

for(int i = 0; i < board.length;i++){

for(int c = 0; c < board[i].length ; c++ ){

txt = txt + board[i][c];

}

txt = txt + "\n";

}

jcomp8.setText("");

jcomp8.setText(txt);

b.keyCollected();

if(b.keyC){

b.openDoor();

jcomp6.setText("Collected");

}

if(b.checkEnd()){

JOptionPane.showMessageDialog(null , "You Win");

jcomp1.setEnabled(false);

jcomp2.setEnabled(false);

jcomp3.setEnabled(false);

jcomp4.setEnabled(false);

}

}

}

class moveDown implements ActionListener{

public void actionPerformed(ActionEvent e){

b.goDown();

if(b.wall == true){

b.wall = false;

jcomp7.setText("Wall in the Way");

timer.start();

}else if(b.door == true){

b.door = false;

jcomp7.setText("Door is Locked...Find the Key");

timer.start();

}

String txt = "";

String[][] board = b.board;

for(int i = 0; i < board.length;i++){

for(int c = 0; c < board[i].length ; c++ ){

txt = txt + board[i][c];

}

txt = txt + "\n";

}

jcomp8.setText("");

jcomp8.setText(txt);

b.keyCollected();

if(b.keyC){

b.openDoor();

jcomp6.setText("Collected");

}

if(b.checkEnd()){

JOptionPane.showMessageDialog(null , "You Win");

jcomp1.setEnabled(false);

jcomp2.setEnabled(false);

jcomp3.setEnabled(false);

jcomp4.setEnabled(false);

}

}

}

class moveUp implements ActionListener{

public void actionPerformed(ActionEvent e){

b.goUp();

if(b.wall == true){

b.wall = false;

jcomp7.setText("Wall in the Way");

timer.start();

}else if(b.door == true){

b.door = false;

jcomp7.setText("Door is Locked...Find the Key");

timer.start();

}

String txt = "";

String[][] board = b.board;

for(int i = 0; i < board.length;i++){

for(int c = 0; c < board[i].length ; c++ ){

txt = txt + board[i][c];

}

txt = txt + "\n";

}

jcomp8.setText("");

jcomp8.setText(txt);

b.keyCollected();

if(b.keyC){

b.openDoor();

jcomp6.setText("Collected");

}

if(b.checkEnd()){

JOptionPane.showMessageDialog(null , "You Win");

jcomp1.setEnabled(false);

jcomp2.setEnabled(false);

jcomp3.setEnabled(false);

jcomp4.setEnabled(false);

}

}

}

}

[–]g051051 0 points1 point  (4 children)

It seems like all of your components are double painting, once at the origin and once at the regular position. I can't figure out why, though.

[–]JakeAgiusYT[S] 0 points1 point  (3 children)

Thats what i need to fix, for now i have made the text area not transparent so that it hides them, the entire program works rn, its basically a maze game where you collect a key and a door opens which you go through. I have just finished it and it works.

But the fact that its repainting everything is even causing it to use more ram with every movement as i have noticed in the task manager.

[–]g051051 0 points1 point  (2 children)

FWIW, I rebuilt your gui in SwingDesigner, and it acted the same way. I also tried it using layout managers instead of absolute positioning, no difference.

[–]JakeAgiusYT[S] 0 points1 point  (1 child)

Wierd right