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 :( :( :()


Sign In
Create Account


Back to top










