Refactored choosing race and class into a single class. discovered issue when only one image exists for a race or class, the last index for the randomly chosen snapshot image is set to 0 and will forever be stuck in a while loop. Need to implement some kind of safeguards.

This commit is contained in:
Josh Ashton
2022-11-15 22:43:20 -07:00
parent d8c2617680
commit 69f644b676
21 changed files with 255 additions and 370 deletions
+138
View File
@@ -0,0 +1,138 @@
package GUI.Panels;
import GUI.AppTheme;
import GUI.Panels.SubPanels.*;
import Resources.CustomAssets.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BasicPanel extends JPanel implements AppTheme {
private DefaultButton[] optionButtons;
private String[] options;
private String[] descriptions;
private InfoPanel infoPanel;
private DefaultButton backButton;
private DefaultButton continueButton;
private int choiceIndex;
private String panelTitle;
public BasicPanel(DefaultButton backButton, DefaultButton continueButton, String[] options, String[] descriptions, String panelTitle) {
super();
this.backButton = backButton;
this.continueButton = continueButton;
this.options = options;
this.descriptions = descriptions;
this.panelTitle = panelTitle;
this.infoPanel = new InfoPanel(options, descriptions);
choiceIndex = 0;
panelSetup();
createMasterPanel();
}
private void createMasterPanel() {
JLabel title = new JLabel(panelTitle);
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setFont(headerFont);
title.setForeground(darkestBrown);
add(title, BorderLayout.NORTH);
JPanel racePanel = new JPanel();
racePanel.setOpaque(false);
racePanel.setLayout(null);
racePanel.add(infoPanel);
racePanel.add(createButtonPanel());
add(racePanel);
add(createNavPanel(), BorderLayout.SOUTH);
}
private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(options.length, 1, 0, 0));
buttonPanel.setBackground(lightBrown);
buttonPanel.setBounds(585, 45, 135, 355);
optionButtons = new DefaultButton[options.length];
for (int i = 0; i < optionButtons.length; i++) {
optionButtons[i] = createButton(options[i], i);
if (i == 0) {
optionButtons[i].select();
}
}
for (int i = 0; i < options.length; i++) {
buttonPanel.add(optionButtons[i]);
}
return buttonPanel;
}
private void panelSetup() {
setBackground(lightBrown);
setLayout(new BorderLayout(0, 0));
}
private JPanel createNavPanel() {
JPanel panel = new JPanel(new GridLayout(1, 2, 30, 0));
panel.setBackground(darkestBrown);
panel.setBorder(new EmptyBorder(15, 150, 15, 150));
panel.add(backButton);
panel.add(continueButton);
return panel;
}
private DefaultButton createButton(String raceName, int index) {
DefaultButton button = new DefaultButton(raceName);
button.addActionListener(e -> updateButtons(choiceIndex, index));
return button;
}
private void updateButtons(int lastIndex, int newIndex) {
optionButtons[lastIndex].deSelect();
optionButtons[newIndex].select();
choiceIndex = newIndex;
infoPanel.updateInfo(choiceIndex);
}
public int getChoice() {
return choiceIndex;
}
}
@@ -1,203 +0,0 @@
package GUI.Panels;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
public class ChooseClassPanel extends JPanel {
/**
* Create the panel.
*/
public ChooseClassPanel(JButton backButton, JButton contButton) {
setBackground(new Color(222, 184, 135));
setBorder(new BevelBorder(BevelBorder.RAISED, new Color(160, 82, 45), new Color(165, 42, 42), new Color(0, 0, 0), null));
setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = createLbl();
add(lblNewLabel, BorderLayout.NORTH);
//Creates back and continue panel
JPanel backContPanel = createBackContinuePanel(backButton, contButton);
add(backContPanel, BorderLayout.SOUTH);
JPanel innerPanel = new JPanel();
innerPanel.setOpaque(false);
innerPanel.setLayout(null);
JPanel infoPanel = createInfoPanel();
innerPanel.add(infoPanel);
JPanel btnPanel = createBtnPanel();
innerPanel.add(btnPanel);
add(innerPanel, BorderLayout.CENTER);
}
private JPanel createBtnPanel() {
JPanel btnPanel = new JPanel();
btnPanel.setBackground(new Color(165, 42, 42));
btnPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
btnPanel.setBounds(311, 6, 409, 394);
JButton artificerBtn = new JButton("Artificer");
artificerBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(artificerBtn);
JButton barbarianBtn = new JButton("Barbarian");
barbarianBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(barbarianBtn);
JButton bardBtn = new JButton("Bard");
bardBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(bardBtn);
JButton clericBtn = new JButton("Cleric");
clericBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(clericBtn);
JButton druidBtn = new JButton("Druid");
druidBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(druidBtn);
JButton fighterBtn = new JButton("Figther");
fighterBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(fighterBtn);
JButton monkBtn = new JButton("Monk");
monkBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(monkBtn);
JButton paladinBtn = new JButton("Paladin");
paladinBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(paladinBtn);
JButton rangerBtn = new JButton("Ranger");
rangerBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(rangerBtn);
JButton rogueBtn = new JButton("Rogue");
rogueBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(rogueBtn);
JButton sorcererBtn = new JButton("Sorcerer");
sorcererBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(sorcererBtn);
JButton warlockBtn = new JButton("Warlock");
warlockBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(warlockBtn);
JButton wizardBtn = new JButton("Wizard");
wizardBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnPanel.add(wizardBtn);
return btnPanel;
}
private JPanel createInfoPanel() {
JPanel infoPanel = new JPanel();
infoPanel.setOpaque(false);
infoPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
infoPanel.setBounds(6, 6, 293, 394);
infoPanel.setLayout(null);
JLabel infoLbl = new JLabel("Information");
infoLbl.setFont(new Font("Bodoni 72", Font.PLAIN, 20));
infoLbl.setHorizontalAlignment(SwingConstants.CENTER);
infoLbl.setBounds(92, 6, 105, 16);
infoPanel.add(infoLbl);
JTextPane infoTextPane = new JTextPane();
infoTextPane.setOpaque(false);
infoTextPane.setBounds(6, 21, 281, 367);
infoPanel.add(infoTextPane);
return infoPanel;
}
/**
* @return
*/
private JPanel createBackContinuePanel(JButton backButton, JButton continueButton) {
JPanel backContPanel = new JPanel();
backContPanel.setBackground(new Color(205, 133, 63));
JButton backBtn = new JButton("<--- Back ");
backBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
// backContPanel.add(backBtn);
backContPanel.add(backButton);
JButton continueBtn = new JButton("Continue --->");
continueBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
// backContPanel.add(continueBtn);
backContPanel.add(continueButton);
return backContPanel;
}
/**
* @return
*/
private JLabel createLbl() {
JLabel lblNewLabel = new JLabel("Step 2: Choose Class");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Bodoni 72 Oldstyle", Font.PLAIN, 33));
return lblNewLabel;
}
}
@@ -1,141 +0,0 @@
package GUI.Panels;
import GUI.AppTheme;
import GUI.Panels.SubPanels.*;
import Resources.CustomAssets.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.*;
public class ChooseRacePanel extends JPanel implements AppTheme {
private String[] raceOptions;
private String[] raceDescriptions;
private InfoPanel infoPanel;
private int raceIndex;
private JButton backButton;
private JButton continueButton;
private DefaultButton[] raceOptionButtons;
public ChooseRacePanel(JButton backButton, DefaultButton continueButton) {
super();
this.backButton = backButton;
this.continueButton = continueButton;
raceIndex = 0;
createRaces();
this.infoPanel = new InfoPanel(raceOptions, raceDescriptions);
panelSetup();
createMasterPanel();
}
private void createRaces() {
raceOptions = new String[]{"Dragonborn", "Dwarf", "Elf", "Gnome", "HalfElf", "Halfling", "Halforc", "Human", "Tiefling"};
raceDescriptions = new String[]{
"Born of dragons, as their name proclaims, the dragonborn walk proudly through a world that greets them with fearful incomprehension. Shaped by draconic gods or the dragons themselves, dragonborn originally hatched from dragon eggs as a unique race, combining the best attributes of dragons and humanoids. Some dragonborn are faithful servants to true dragons, others form the ranks of soldiers in great wars, and still others find themselves adrift, with no clear calling in life."
};
}
private void createMasterPanel() {
JLabel title = new JLabel("Step 1: Choose Race");
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setFont(headerFont);
title.setForeground(darkestBrown);
add(title, BorderLayout.NORTH);
JPanel racePanel = new JPanel();
racePanel.setOpaque(false);
racePanel.setLayout(null);
racePanel.add(infoPanel);
racePanel.add(createRaceButtonPanel());
add(racePanel);
add(createNavPanel(), BorderLayout.SOUTH);
}
private JPanel createRaceButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(raceOptions.length, 1, 0, 0));
buttonPanel.setBackground(lightBrown);
buttonPanel.setBounds(585, 45, 135, 355);
raceOptionButtons = new DefaultButton[raceOptions.length];
for (int i = 0; i < raceOptionButtons.length; i++) {
raceOptionButtons[i] = createButton(raceOptions[i], i);
}
for (int i = 0; i < raceOptions.length; i++) {
buttonPanel.add(raceOptionButtons[i]);
}
return buttonPanel;
}
private void panelSetup() {
setBackground(lightBrown);
setLayout(new BorderLayout(0, 0));
}
private JPanel createNavPanel() {
JPanel panel = new JPanel(new GridLayout(1, 2, 30, 0));
panel.setBackground(darkestBrown);
panel.setBorder(new EmptyBorder(15, 150, 15, 150));
panel.add(backButton);
panel.add(continueButton);
return panel;
}
private DefaultButton createButton(String raceName, int index) {
DefaultButton button = new DefaultButton(raceName);
button.addActionListener(e -> updateButtons(raceIndex, index));
return button;
}
private void updateButtons(int lastIndex, int newIndex) {
raceOptionButtons[lastIndex].deSelect();
raceOptionButtons[newIndex].select();
raceIndex = newIndex;
infoPanel.updateInfo(raceIndex);
}
public int getRaceChoice() {
return raceIndex;
}
}
@@ -17,22 +17,16 @@ public class InfoPanel extends JPanel implements AppTheme {
private String[] holderDescriptions;
private JLabel[][] portraits;
private String[] categoryHolder;
private static String OS = null;
private static int numInfoPanelObjects = 0;
private int lastIndex;
public InfoPanel(String[] categoryHolder, String[] holderDescriptions) {
super();
numInfoPanelObjects++;
this.categoryHolder = categoryHolder;
this.holderDescriptions = holderDescriptions;
if (OS == null) {
OS = System.getProperty("os.name");
}
panelSetup();
createDescriptions();
@@ -46,9 +40,12 @@ public class InfoPanel extends JPanel implements AppTheme {
removeAll();
panelSetup();
add(descriptions[0]);
panelSetup();
add(descriptions[holderIndex]);
System.out.println();
add(portraits[holderIndex][getNextPortrait(holderIndex)]);
repaint();
@@ -56,17 +53,18 @@ public class InfoPanel extends JPanel implements AppTheme {
}
private int getNextPortrait(int holderIndex) {
Random ran = new Random();
int bound = portraits[holderIndex].length;
int newIndex = ran.nextInt(bound);
while(newIndex == lastIndex) {
/* while(newIndex == lastIndex) {
newIndex = ran.nextInt(bound);
}
}*/
lastIndex = newIndex;
@@ -116,6 +114,18 @@ public class InfoPanel extends JPanel implements AppTheme {
private void createPortraits() {
String category = "";
if(numInfoPanelObjects == 1) {
category = "Races";
} else if (numInfoPanelObjects == 2) {
category = "Classes";
}
portraits = new JLabel[categoryHolder.length][];
for (int i = 0; i < categoryHolder.length; i++) {
@@ -124,15 +134,15 @@ public class InfoPanel extends JPanel implements AppTheme {
if(OS.startsWith("Windows")) {
imagesDir = new File("dndBuilder\\src\\Resources\\Img\\" + categoryHolder[i] + "\\");
imagesDir = new File("dndBuilder\\src\\Resources\\Img\\" + category + "\\" + categoryHolder[i].toLowerCase() + "\\");
} else if (OS.startsWith("Mac")) {
imagesDir = new File("dndBuilder/src/Resources/Img/" + categoryHolder[i] + "/");
imagesDir = new File("dndBuilder/src/Resources/Img/" + category + "/" + categoryHolder[i].toLowerCase() + "/");
} else if (OS.startsWith("Linux")) {
imagesDir = new File("dndBuilder/src/Resources/Img/" + categoryHolder[i] + "/");
imagesDir = new File("dndBuilder/src/Resources/Img/" + category + "/" + categoryHolder[i].toLowerCase() + "/");
} else {
@@ -146,7 +156,6 @@ public class InfoPanel extends JPanel implements AppTheme {
int index = 0;
for (File file : dir) {
portraits[i][index] = image(file);
portraits[i][index].setOpaque(true);
portraits[i][index].setBackground(lightBrown);
@@ -156,8 +165,10 @@ public class InfoPanel extends JPanel implements AppTheme {
}
}
}
private JLabel image(File file) {