race GUI refactoring nearly completed with InfoPanel implementation
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package GUI.Panels;
|
package GUI.Panels;
|
||||||
|
|
||||||
import GUI.Panels.SubPanels.DescriptionPanel;
|
import GUI.Panels.SubPanels.*;
|
||||||
import GUI.Panels.SubPanels.ImagePanel;
|
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
@@ -13,11 +12,9 @@ public class ChooseRacePanel extends JPanel {
|
|||||||
private String[] raceOptions;
|
private String[] raceOptions;
|
||||||
|
|
||||||
private String[] raceDescriptions;
|
private String[] raceDescriptions;
|
||||||
private ImagePanel imgPanel;
|
|
||||||
private DescriptionPanel descPanel;
|
|
||||||
|
|
||||||
// DEBUG: raceChoice to be deprecated after testing.
|
private InfoPanel infoPanel;
|
||||||
private String raceChoice;
|
|
||||||
private int raceIndex;
|
private int raceIndex;
|
||||||
private JButton backButton;
|
private JButton backButton;
|
||||||
private JButton continueButton;
|
private JButton continueButton;
|
||||||
@@ -29,10 +26,8 @@ public class ChooseRacePanel extends JPanel {
|
|||||||
super();
|
super();
|
||||||
this.backButton = backButton;
|
this.backButton = backButton;
|
||||||
this.continueButton = continueButton;
|
this.continueButton = continueButton;
|
||||||
raceOptions = new String[]{"Dragonborn", "Dwarf", "Elf", "Gnome", "HalfElf", "Halfling", "Halforc", "Human", "Tiefling"};
|
createRaces();
|
||||||
createRaceDescriptions();
|
this.infoPanel = new InfoPanel(raceOptions, raceDescriptions);
|
||||||
imgPanel = new ImagePanel(raceOptions);
|
|
||||||
descPanel = new DescriptionPanel(raceDescriptions);
|
|
||||||
|
|
||||||
panelSetup();
|
panelSetup();
|
||||||
|
|
||||||
@@ -40,7 +35,9 @@ public class ChooseRacePanel extends JPanel {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createRaceDescriptions() {
|
private void createRaces() {
|
||||||
|
|
||||||
|
raceOptions = new String[]{"Dragonborn", "Dwarf", "Elf", "Gnome", "HalfElf", "Halfling", "Halforc", "Human", "Tiefling"};
|
||||||
|
|
||||||
raceDescriptions = new String[]{
|
raceDescriptions = new String[]{
|
||||||
|
|
||||||
@@ -61,11 +58,6 @@ public class ChooseRacePanel extends JPanel {
|
|||||||
racePanel.setOpaque(false);
|
racePanel.setOpaque(false);
|
||||||
racePanel.setLayout(null);
|
racePanel.setLayout(null);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
infoPanel.add(imgPanel);
|
|
||||||
infoPanel.add(descPanel);
|
|
||||||
|
|
||||||
racePanel.add(infoPanel);
|
racePanel.add(infoPanel);
|
||||||
|
|
||||||
racePanel.add(createRaceButtonPanel());
|
racePanel.add(createRaceButtonPanel());
|
||||||
@@ -120,10 +112,8 @@ public class ChooseRacePanel extends JPanel {
|
|||||||
|
|
||||||
e -> {
|
e -> {
|
||||||
|
|
||||||
raceChoice = raceName;
|
|
||||||
raceIndex = index;
|
raceIndex = index;
|
||||||
imgPanel.updateImage(raceIndex);
|
infoPanel.updateInfo(raceIndex);
|
||||||
descPanel.updateDescription(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;
|
package GUI.Panels.SubPanels;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.border.BevelBorder;
|
import javax.swing.border.BevelBorder;
|
||||||
import java.awt.*;
|
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 {
|
public class InfoPanel extends JPanel {
|
||||||
|
|
||||||
@@ -10,13 +15,61 @@ public class InfoPanel extends JPanel {
|
|||||||
private String[] holderDescriptions;
|
private String[] holderDescriptions;
|
||||||
private JLabel[][] portraits;
|
private JLabel[][] portraits;
|
||||||
private String[] categoryHolder;
|
private String[] categoryHolder;
|
||||||
|
|
||||||
|
private static String OS = null;
|
||||||
private int lastIndex;
|
private int lastIndex;
|
||||||
|
|
||||||
public InfoPanel(String[] categoryHolder, String[] holderDescriptions) {
|
public InfoPanel(String[] categoryHolder, String[] holderDescriptions) {
|
||||||
|
|
||||||
super();
|
super();
|
||||||
|
this.categoryHolder = categoryHolder;
|
||||||
|
this.holderDescriptions = holderDescriptions;
|
||||||
|
|
||||||
|
if (OS == null) {
|
||||||
|
|
||||||
|
OS = System.getProperty("os.name");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
panelSetup();
|
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() {
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user