Jump to content

Problem with GridBag layout

- - - - -

  • Please log in to reply
13 replies to this topic

#1
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.File;



@SuppressWarnings("serial")

public class PetBattling extends JFrame {

	private PetBattling() {

		this.setTitle("Pet Battling");

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		this.setSize(600, 600);

		this.setResizable(false);

		this.setLayout(new GridBagLayout());

		ImageIcon heart = new ImageIcon("pictures/heart.png");

		ImageIcon xp = new ImageIcon("pictures/xp.png");

		JLabel playerHP = new JLabel("Health", heart, SwingConstants.LEFT);

		JLabel enemyHP = new JLabel("Health", heart, SwingConstants.LEFT);

		JLabel exp = new JLabel("Experience", xp, SwingConstants.LEFT);

		addToFrame(playerHP, 0, 0, 3, 1);

		addToFrame(enemyHP, 3, 0, 3, 1);

		addToFrame(exp, 0, 1, 3, 1);

		int whatIsEnemy = (int)((Math.random() * 1/*TODO amount of monsters*/) + 1);

		

		File dataFile = new File("files/data.txt");

		FileRead reader = new FileRead(dataFile);

		String line = reader.readLine();

		String[] data = line.split("\t");

		JLabel player = new JLabel(getCreatureImage(data[0]));

		JLabel enemy = new JLabel(getCreatureImage(whatIsEnemy));

		addToFrame(player, 0, 2, 3, 4);

		addToFrame(enemy, 3, 2, 3, 4);

		

		this.setVisible(true);

	}

	public static void start() {

		new PetBattling();

	}

	private void addToFrame(Component p, int x, int y, int width, int height) {

		GridBagConstraints gbc = new GridBagConstraints();

		gbc.gridx = x;

		gbc.gridy = y;

		gbc.gridwidth = width;

		gbc.gridheight = height;

		gbc.weightx = 100;

		gbc.weighty = 100;

		gbc.insets = new Insets(5, 5, 5, 5);

		gbc.anchor = GridBagConstraints.CENTER;

		gbc.fill = GridBagConstraints.NONE;

		this.add(p, gbc);

	}

	private ImageIcon getCreatureImage(String ID) {

		int IDint = Integer.parseInt(ID);

		switch (IDint) {

		case 1:

			return new ImageIcon("pictures/creature/1");

		default:

			return new ImageIcon("pictures/creature/1");

		}

	}

	private ImageIcon getCreatureImage(int ID) {

		switch (ID) {

		case 1:

			return new ImageIcon("pictures/creature/1");

		default:

			return new ImageIcon("pictures/creature/1");

		}

	}

}


When I run it, I get a heart and then the word health twice in the first row, and in the second row a get an experience picture and the word experience. (the attatchments aren't working :( :( :()

#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
You describe what it does but You don't say what you want it to look like.

#3
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
I want it to look like:
(heartpicture) Health (heartpicture) Health
(exppictre)Experience

(player's picture) (enemy's picture)
" "
" "

but it looks like
(heartpicture) Health (heartpicture) Health
(exppictre)Experience

#4
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Where is the definition for the FileRead class?

#5
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
in another class file, i'll post it

---------- Post added at 10:01 AM ---------- Previous post was at 10:01 AM ----------

import java.io.*;

public class FileRead {
BufferedReader reader;
public FileRead(File f) {
try {
reader = new BufferedReader(new FileReader(f));
} catch (IOException e) {}
}

//Methods to read//

public String readLine() {
String line;
try {
line = reader.readLine();
} catch (IOException e) {
line = "";
}
return line;
}

//Other methods//

public void skip(int i) {
try {
reader.skip((long)i);
} catch (IOException e) {}
}
}

#6
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
When I fixed it to compile and execute I get:

Quote

(heartpicture) Health (heartpicture) Health
(exppictre)Experience

(player's picture) (enemy's picture)
Your code as posted will not execute with the java command.
How do you execute it?

#7
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
from a main class, i'll post it

---------- Post added at 10:05 AM ---------- Previous post was at 10:05 AM ----------

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


@SuppressWarnings("serial")
public class Main extends JFrame implements ActionListener{
JButton tictactoe, petbattle;

public static void main(String[] args) {
new Main();
}

private Main() {
this.setTitle("Minigames");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(500, 65);
this.setResizable(false);

JPanel buttonPanel = new JPanel();
tictactoe = new JButton("Tic Tac Toe");
buttonPanel.add(tictactoe);
petbattle = new JButton("Pet Battling");
buttonPanel.add(petbattle);
this.add(buttonPanel);
tictactoe.addActionListener(this);
petbattle.addActionListener(this);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(tictactoe)) {
TicTacToe.start();
} else if (e.getSource().equals(petbattle)) {
PetBattling.start();
}
}
}

#8
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
When I changed the start() method to a main() method, the code produced what I said above.

You can call the main method almost the same way you are calling the start method. It only needs a String[] arg extra.
That would allow you to simplify the testing and only require one source file.

Attached Files



#9
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
I did what you said and the same thing happened.

#10
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Does the image I posted look like what you get when you execute the code?

#11
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
No, it looks like the same thing as the beginning.

#12
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Is what I posted what you want to see?
What is different between when I execute your code and when you execute it?
One difference could be that my code finds and loads the images and your code does not load the images.
Check that the names for the images are correct.
One way to test if the image filename is correct is to use the File class to test if the file exists:
File testing = new File(<THEFILENAMEHERE>);
System.out.println("THEFILENAMEHERE exists=" + testing.exists() + " path=" + testing.getPath()); //<<< check correct method names




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users