I am confused at what is going wrong here. I believe its something wrong with the way JFrame is set up. There is also a generator class but I think if I can get the launcher working then the Generator will come after. Any help is greatly appreciated!
here is my source code:
main:
public class Main {
static Launcher launcher;
public static void main(String[]args){
launcher = new Launcher();
}
}
Launcher:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
@SuppressWarnings("serial")
public class Launcher extends JFrame {
public static Font titleFont;
public static Font secondFont;
public static Font generateFont;
public static int levelHeight;
public static int levelWidth;
public static String tileWidth;
public static String tileHeight;
public static String destination;
public static String levelSource;
public static String tilesetSource;
public static int tilesetWidth;
public static int tilesetHeight;
public static Box box;
public static JPanel panel;
public static JPanel panel1;
public static JPanel panel2;
public static JPanel panel3;
public static JPanel panel4;
public static JPanel panel5;
public static JPanel panel6;
public static JLabel lbltitle;
public static JLabel lblsecondTitle;
public static JLabel lblemptyString;
public static JLabel lblemptyString2;
public static JLabel lblcomment;
public static JLabel lblauthor;
public static JLabel lblTileWidth;
public static JLabel lblTileHeight;
public static JLabel lblDestinationFile;
public static JLabel lblLevelSourceFile;
public static JLabel lblTileSetSourceFile;
public static JLabel lblHelp;
public static JButton generate;
public static JTextField tfTileWidth;
public static JTextField tfTileHeight;
public static JTextField tfDestinationFile;
public static JTextField tfLevelSourceFile;
public static JTextField tfTileSetSourceFile;
public static int dWidth;
public static int dHeight;
public static int dTileWidth;
public static int dTileHeight;
public static int dTilesetHeight;
public static int dTilesetWidth;
public static File fDestination;
public static File fLevelSource;
public static File fTileSetSource;
public static File tsdirectory;
public static File lmdirectory;
public static File[] tsList;
public static File[] lmList;
public static String[] tslist;
public static String[] lmlist;
public static int i;
public static int checkInputs;
private JComboBox<String> comboBox;
private JComboBox<String> comboBox1;
public Launcher(){
super("Dafon's World: Level Generator");
init();
}
public void init(){
i = 0;
checkInputs = 0;
setFrame();
setDirectories();
setFonts();
setJLabels();
setJButton();
setJTextFields();
setJPanels();
setBox();
addListeners();
add(box, BorderLayout.NORTH);
add(lblauthor, BorderLayout.SOUTH);
setVisible(true);
System.out.println("init() completed\n");
}
public void setFrame(){
setSize(400, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setFocusable(true);
setLocationRelativeTo(null);
System.out.println("Frame set");
}
public void setDirectories(){
lmdirectory = new File("Resources/LevelMaps");
tsdirectory = new File("Resources/TileSets");
comboBox = new JComboBox<String>();
comboBox1 = new JComboBox<String>();
tsList = tsdirectory.listFiles();
tslist = new String[tsList.length];
for (int i = 0; i < tsList.length; ++i){
tslist[i] = tsList[i].getName();
comboBox.addItem(tslist[i]);
}
lmList = lmdirectory.listFiles();
lmlist = new String[lmList.length];
for (int i = 0; i < lmList.length; ++i){
lmlist[i] = lmList[i].getName();
comboBox1.addItem(lmlist[i]);
}
System.out.println("Directories set");
}
public void setJPanels(){
panel = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel6 = new JPanel();
panel.add(lblTileWidth);
panel.add(tfTileWidth);
panel2.add(lblTileHeight);
panel2.add(tfTileHeight);
panel3.add(lblTileSetSourceFile);
panel3.add(comboBox);
panel4.add(lblLevelSourceFile);
panel4.add(comboBox1);
panel5.add(lblDestinationFile);
panel5.add(tfDestinationFile);
panel6.add(lblHelp);
System.out.println("JPanels set");
}
public void setFonts(){
titleFont = new Font("Verdana", 1, 38);
secondFont = new Font("Verdana", 2, 28);
generateFont = new Font("Verdana", 0, 16);
System.out.println("Fonts Set");
}
public void setJLabels(){
lbltitle = new JLabel("Dafon's World:");
lbltitle.setFont(titleFont);
lbltitle.setAlignmentX(JLabel.CENTER_ALIGNMENT);
lblsecondTitle = new JLabel("Level Generator");
lblsecondTitle.setFont(secondFont);
lblsecondTitle.setAlignmentX(JLabel.CENTER_ALIGNMENT);
lblemptyString = new JLabel(" ");
lblemptyString.setFont(secondFont);
lblemptyString2 = new JLabel(" ");
lblemptyString2.setFont(secondFont);
lblauthor = new JLabel("Created by Brett A. Blashko", SwingConstants.RIGHT);
lblcomment = new JLabel("** Please you enter all of the fields below **");
lblcomment.setForeground(Color.red);
lblcomment.setAlignmentX(CENTER_ALIGNMENT);
lblTileWidth = new JLabel("Tile Width: ");
lblTileHeight = new JLabel("Tile Height: ");
lblDestinationFile = new JLabel("TileMap Destination Filename (.txt):");
lblLevelSourceFile = new JLabel("Level Source Filename (.png):");
lblTileSetSourceFile = new JLabel("TileSet Source Filename (.png):");
lblHelp = new JLabel();
lblHelp.setText("**One or more fields have been filled in incorrectly**");
lblHelp.setForeground(Color.red);
lblHelp.setAlignmentX(JLabel.CENTER_ALIGNMENT);
System.out.println("JLabels set");
}
public void setJButton(){
generate = new JButton("Generate!");
generate.setAlignmentX(CENTER_ALIGNMENT);
generate.setLayout(null);
generate.setPreferredSize(new Dimension(600, 40));
generate.setFont(generateFont);
}
public void setJTextFields(){
tfTileWidth = new JTextField("", 12);
tfTileHeight = new JTextField("", 12);
tfDestinationFile = new JTextField("", 12);
tfLevelSourceFile = new JTextField("", 12);
tfTileSetSourceFile = new JTextField("", 12);
System.out.println("JTextFields set");
}
public void setBox(){
box = Box.createVerticalBox();
box.add(lbltitle);
box.add(lblsecondTitle);
box.add(lblemptyString);
box.add(lblcomment);
box.add(panel);
box.add(panel2);
box.add(panel3);
box.add(panel4);
box.add(panel5);
box.add(lblemptyString2);
box.add(generate);
box.add(lblHelp).setVisible(false);
System.out.println("Box set");
}
public void addListeners(){
comboBox.addActionListener(new comboBoxListener());
comboBox1.addActionListener(new comboBoxListener());
generate.addActionListener(new ButtonPressedListener());
tfTileWidth.addFocusListener(new tfTextListener());
tfTileHeight.addFocusListener(new tfTextListener());
tfDestinationFile.addFocusListener(new tfTextListener());
tfLevelSourceFile.addFocusListener(new tfTextListener());
tfTileSetSourceFile.addFocusListener(new tfTextListener());
System.out.println("Listeners created");
}
public class comboBoxListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == comboBox){
i = 2;
String s = "Resources/TileSets/" + comboBox.getItemAt(comboBox.getSelectedIndex());
fTileSetSource = new File(s);
comboBox.setBackground(Color.green);
checkInputs++;
}
if(e.getSource() == comboBox1){
i = 3;
String s = "Resources/LevelMaps/" + comboBox1.getItemAt(comboBox1.getSelectedIndex());
fLevelSource = new File(s);
comboBox1.setBackground(Color.green);
checkInputs++;
}
}
}
public class tfTextListener implements FocusListener{
public void focusGained(FocusEvent e) {
if(e.getSource() == tfTileWidth){
tfTileWidth.setBackground(Color.white);
i = 0;
}
if(e.getSource() == tfTileHeight){
tfTileHeight.setBackground(Color.white);
i = 1;
}
if(e.getSource() == tfDestinationFile){
tfDestinationFile.setBackground(Color.white);
i = 4;
}
}
public void focusLost(FocusEvent e) {
if(e.getSource() == tfTileWidth){
tileWidth = tfTileWidth.getText();
stringType(tileWidth);
}
if(e.getSource() == tfTileHeight){
tileHeight = tfTileHeight.getText();
stringType(tileHeight);
}
if(e.getSource() == tfTileSetSourceFile){
tilesetSource = tfTileSetSourceFile.getText();
stringType(tilesetSource);
}
if(e.getSource() == tfLevelSourceFile){
levelSource = tfLevelSourceFile.getText();
stringType(levelSource);
}
if(e.getSource() == tfDestinationFile){
destination = tfDestinationFile.getText();
stringType(destination);
}
}
}
public static void stringType(String str) {
int length = 0;
length = str.length();
if (i >= 0 && i < 2) {
try {
int d = (int)Double.parseDouble(str);
if(i == 0){
dTileWidth = d;
tfTileWidth.setBackground(Color.green);
checkInputs++;
}else if(i == 1){
dTileHeight = d;
tfTileHeight.setBackground(Color.green);
checkInputs++;
}
System.out.println("Entry is valid number");
} catch (NumberFormatException nfe) {
System.out.println("Entry is not valid number");
if(i == 0){
tfTileWidth.setBackground(Color.red);
}else if(i == 1){
tfTileHeight.setBackground(Color.red);
}
}
} else {
length = str.length();
if (str.matches("[a-zA-Z._0-9]+") && length >= 4 && length != 0) {
if(i == 2){
if (str.substring(length - 4, length).equals(".png")) {
System.out.println("Entry is a valid String");
String file = ("Resources/TileSets/" + str);
fTileSetSource = new File(file);
if(fTileSetSource.exists()) {
tfTileSetSourceFile.setBackground(Color.green);
checkInputs++;
}else{
tfTileSetSourceFile.setBackground(Color.red);
}
}
}else if(i == 3){
if (str.substring(length - 4, length).equals(".png")) {
System.out.println("Entry is a valid String");
String file = ("Resources/LevelMaps/" + str);
fLevelSource = new File(file);
if(fLevelSource.exists()) {
tfLevelSourceFile.setBackground(Color.green);
checkInputs++;
}else{
tfLevelSourceFile.setBackground(Color.red);
}
}
}else if(i == 4){
if (str.substring(length - 4, length).equals(".txt")) {
System.out.println("Entry is a valid String");
tfDestinationFile.setBackground(Color.green);
String file = ("Resources/TileMaps/" + str);
fDestination = new File(file);
checkInputs++;
}
}
} else {
System.out.println("Entry is not a valid String");
if(i == 2){
tfTileSetSourceFile.setBackground(Color.red);
}else if(i == 3){
tfLevelSourceFile.setBackground(Color.red);
}else if(i == 4){
tfDestinationFile.setBackground(Color.red);
}
}
}
}
public class ButtonPressedListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(checkInputs >= 5){
dispose();
Generator gen = new Generator();
gen.setDefaultCloseOperation(EXIT_ON_CLOSE);
}else{
box.getComponent(11).setVisible(true);
repaint();
}
}
}
public void lsetvisible(boolean b){
setVisible(b);
}
}