race GUI refactoring nearly completed with InfoPanel implementation

This commit is contained in:
Josh Ashton
2022-11-15 17:13:26 -07:00
parent 1eda149582
commit 31384565fc
4 changed files with 179 additions and 205 deletions
+10 -20
View File
@@ -1,7 +1,6 @@
package GUI.Panels;
import GUI.Panels.SubPanels.DescriptionPanel;
import GUI.Panels.SubPanels.ImagePanel;
import GUI.Panels.SubPanels.*;
import java.awt.*;
@@ -13,11 +12,9 @@ public class ChooseRacePanel extends JPanel {
private String[] raceOptions;
private String[] raceDescriptions;
private ImagePanel imgPanel;
private DescriptionPanel descPanel;
// DEBUG: raceChoice to be deprecated after testing.
private String raceChoice;
private InfoPanel infoPanel;
private int raceIndex;
private JButton backButton;
private JButton continueButton;
@@ -29,18 +26,18 @@ public class ChooseRacePanel extends JPanel {
super();
this.backButton = backButton;
this.continueButton = continueButton;
raceOptions = new String[]{"Dragonborn", "Dwarf", "Elf", "Gnome", "HalfElf", "Halfling", "Halforc", "Human", "Tiefling"};
createRaceDescriptions();
imgPanel = new ImagePanel(raceOptions);
descPanel = new DescriptionPanel(raceDescriptions);
createRaces();
this.infoPanel = new InfoPanel(raceOptions, raceDescriptions);
panelSetup();
panelSetup();
add(createMasterPanel());
}
private void createRaceDescriptions() {
private void createRaces() {
raceOptions = new String[]{"Dragonborn", "Dwarf", "Elf", "Gnome", "HalfElf", "Halfling", "Halforc", "Human", "Tiefling"};
raceDescriptions = new String[]{
@@ -61,11 +58,6 @@ public class ChooseRacePanel extends JPanel {
racePanel.setOpaque(false);
racePanel.setLayout(null);
infoPanel.add(imgPanel);
infoPanel.add(descPanel);
racePanel.add(infoPanel);
racePanel.add(createRaceButtonPanel());
@@ -120,10 +112,8 @@ public class ChooseRacePanel extends JPanel {
e -> {
raceChoice = raceName;
raceIndex = index;
imgPanel.updateImage(raceIndex);
descPanel.updateDescription(raceIndex);
infoPanel.updateInfo(raceIndex);
}
@@ -1,70 +0,0 @@
package GUI.Panels.SubPanels;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
public class DescriptionPanel extends JTextArea {
private JTextArea[] descriptions;
private String[] holderDescriptions;
public DescriptionPanel(String[] holderDescriptions) {
super();
panelSetup();
this.holderDescriptions = holderDescriptions;
createDescriptionPanels();
updateDescription(0);
}
private void panelSetup() {
setOpaque(true);
setBackground(new Color(238, 232, 170));
setBounds(6, 229, 281, 159);
}
public void updateDescription(int holderIndex) {
removeAll();
add(descriptions[holderIndex]);
repaint();
revalidate();
}
private void createDescriptionPanels() {
descriptions = new JTextArea[holderDescriptions.length];
for (int i = 0; i < holderDescriptions.length; i++) {
JTextArea tmpDesc = new JTextArea();
tmpDesc.setText(holderDescriptions[i]);
tmpDesc.setWrapStyleWord(true);
tmpDesc.setLineWrap(true);
tmpDesc.setOpaque(false);
tmpDesc.setEditable(false);
tmpDesc.setFocusable(false);
tmpDesc.setFont(new Font("Bodoni 72", Font.PLAIN, 10));
/* setFont(new Font("Bodoni 72", Font.PLAIN, 10));
setHorizontalAlignment(SwingConstants.LEFT);
setBounds(6, 21, 281, 196);*/
descriptions[i] = tmpDesc;
}
}
}
@@ -1,115 +0,0 @@
package GUI.Panels.SubPanels;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
public class ImagePanel extends JPanel {
private JLabel[][] portraits;
private String[] categoryHolder;
private int lastIndex;
public ImagePanel(String[] categoryHolder) {
super();
this.categoryHolder = categoryHolder;
createPortraitsArray();
updateImage(0); // Default selection of the 0th element.
}
private void panelSetup() {
setOpaque(false);
setBounds(6, 21, 281, 196);
setLayout(null);
}
public void updateImage(int holderIndex) {
removeAll();
Random ran = new Random();
int bound = portraits[holderIndex].length;
int newIndex = ran.nextInt(bound);
while(newIndex == lastIndex) {
newIndex = ran.nextInt(bound);
}
lastIndex = newIndex;
add(portraits[holderIndex][newIndex]);
repaint();
revalidate();
}
private void createPortraitsArray() {
portraits = new JLabel[categoryHolder.length][];
for (int i = 0; i < categoryHolder.length; i++) {
File imagesDir = new File("dndBuilder/src/GUI/img/" + categoryHolder[i] + "/");
File[] dir = imagesDir.listFiles();
portraits[i] = new JLabel[dir.length];
int index = 0;
for (File file : dir) {
portraits[i][index] = image(file);
index++;
}
}
}
private JLabel image(File file) {
BufferedImage img;
Image finalImg;
JLabel picture;
try {
img = ImageIO.read(file);
finalImg = img.getScaledInstance(250, 250, Image.SCALE_SMOOTH);
} catch (Exception e) {
System.out.println("Error reading file.");
img = null;
finalImg = null;
}
if(Objects.isNull(finalImg)) {
picture = new JLabel("ERROR: Image not found.");
} else {
picture = new JLabel(new ImageIcon(finalImg));
}
return picture;
}
}
@@ -1,8 +1,13 @@
package GUI.Panels.SubPanels;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Objects;
import java.util.Random;
public class InfoPanel extends JPanel {
@@ -10,13 +15,61 @@ public class InfoPanel extends JPanel {
private String[] holderDescriptions;
private JLabel[][] portraits;
private String[] categoryHolder;
private static String OS = null;
private int lastIndex;
public InfoPanel(String[] categoryHolder, String[] holderDescriptions) {
super();
this.categoryHolder = categoryHolder;
this.holderDescriptions = holderDescriptions;
if (OS == null) {
OS = System.getProperty("os.name");
}
panelSetup();
createDescriptions();
createPortraits();
updateInfo(0); // Default selection of the 0th elements.
}
public void updateInfo(int holderIndex) {
removeAll();
panelSetup();
add(descriptions[holderIndex]);
add(portraits[holderIndex][getNextPortrait(holderIndex)]);
repaint();
revalidate();
}
private int getNextPortrait(int holderIndex) {
Random ran = new Random();
int bound = portraits[holderIndex].length;
int newIndex = ran.nextInt(bound);
while(newIndex == lastIndex) {
newIndex = ran.nextInt(bound);
}
lastIndex = newIndex;
return newIndex;
}
private void panelSetup() {
@@ -34,4 +87,120 @@ public class InfoPanel extends JPanel {
}
private void createDescriptions() {
descriptions = new JTextArea[holderDescriptions.length];
for (int i = 0; i < holderDescriptions.length; i++) {
JTextArea tmpDesc = new JTextArea();
tmpDesc.setText(holderDescriptions[i]);
tmpDesc.setWrapStyleWord(true);
tmpDesc.setLineWrap(true);
tmpDesc.setOpaque(false);
tmpDesc.setEditable(false);
tmpDesc.setFocusable(false);
tmpDesc.setFont(new Font("Bodoni 72", Font.PLAIN, 10));
tmpDesc.setBounds(6, 21, 281, 196);
descriptions[i] = tmpDesc;
}
}
private void createPortraits() {
portraits = new JLabel[categoryHolder.length][];
for (int i = 0; i < categoryHolder.length; i++) {
File imagesDir;
/*
WINDOWS:
attempted
dndBuilder/src/GUI/img/category/
dndBuilder/src/GUI/img/category
/dndBuilder/src/GUI/img/category
/dndBuilder/src/GUI/img/category/
GUI/img/category/
/GUI/img/category/
src/GUI/img/category/
/src/GUI/img/category/
*/
if(OS.startsWith("Windows")) {
imagesDir = new File("dndBuilder\\src\\GUI\\img\\" + categoryHolder[i] + "\\");
} else if (OS.startsWith("Mac")) {
imagesDir = new File("dndBuilder/src/GUI/img/" + categoryHolder[i] + "/");
} else if (OS.startsWith("Linux")) {
imagesDir = new File("dndBuilder/src/GUI/img/" + categoryHolder[i] + "/");
} else {
System.out.println("Unable to detect operating system.");
return;
}
File[] dir = imagesDir.listFiles();
portraits[i] = new JLabel[dir.length];
int index = 0;
for (File file : dir) {
portraits[i][index] = image(file);
portraits[i][index].setOpaque(true);
portraits[i][index].setBackground(new Color(238, 232, 170));
portraits[i][index].setBounds(6, 229, 281, 159);
index++;
}
}
}
private JLabel image(File file) {
BufferedImage img;
Image finalImg;
JLabel picture;
try {
img = ImageIO.read(file);
finalImg = img.getScaledInstance(281, 196, Image.SCALE_SMOOTH);
} catch (Exception e) {
System.out.println("Error reading file.");
img = null;
finalImg = null;
}
if(Objects.isNull(finalImg)) {
picture = new JLabel("ERROR: Image not found.");
} else {
picture = new JLabel(new ImageIcon(finalImg));
}
return picture;
}
}