ImagePanel class implemented, with pseudo-random portraits chosen.

This commit is contained in:
Joshua-Ashton01
2022-11-15 14:39:09 -07:00
parent 801ebe1b15
commit 4e2f12c05f
10 changed files with 132 additions and 35 deletions
+26 -12
View File
@@ -1,14 +1,21 @@
package GUI.Panels;
import GUI.img.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ChooseRacePanel extends JPanel {
private String[] raceOptions = {"Dragonborn", "Dwarf", "Elf", "Gnome", "HalfElf", "Halfling", "Halforc", "Human", "Tiefling"};
private ImagePanel imgPanel;
// DEBUG: raceChoice to be deprecated after testing.
private String raceChoice;
private int raceIndex;
private JButton backButton;
private JButton continueButton;
@@ -19,6 +26,7 @@ public class ChooseRacePanel extends JPanel {
super();
this.backButton = backButton;
this.continueButton = continueButton;
imgPanel = new ImagePanel(raceOptions);
panelSetup();
@@ -37,9 +45,9 @@ public class ChooseRacePanel extends JPanel {
racePanel.setOpaque(false);
racePanel.setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setBounds(47, 67, 223, 301);
racePanel.add(textPane);
imgPanel.setBounds(47, 67, 223, 301);
racePanel.add(imgPanel);
racePanel.add(createRaceButtonPanel());
@@ -56,11 +64,9 @@ public class ChooseRacePanel extends JPanel {
raceBtnPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
raceBtnPanel.setBounds(311, 6, 409, 394);
String[] raceOptions = {"Dragonborn", "Dwarf", "Elf", "Gnome", "HalfElf", "Halfling", "Halforc", "Human", "Tiefling"};
for (int i = 0; i < raceOptions.length; i++) {
for (int i = 0; i <raceOptions.length; i++) {
raceBtnPanel.add(createButton(raceOptions[i]));
raceBtnPanel.add(createButton(raceOptions[i], i));
}
@@ -88,12 +94,18 @@ public class ChooseRacePanel extends JPanel {
}
private JButton createButton(String raceName) {
private JButton createButton(String raceName, int index) {
JButton button = new JButton(raceName);
button.addActionListener(
e -> raceChoice = raceName
e -> {
raceChoice = raceName;
raceIndex = index;
imgPanel.updateImage(raceIndex);
}
);
@@ -101,10 +113,12 @@ public class ChooseRacePanel extends JPanel {
}
public String getRaceChoice() {
public int getRaceChoice() {
return raceChoice;
return raceIndex;
}
}
@@ -1,23 +0,0 @@
package GUI.Panels.img;
import javax.swing.*;
import java.io.File;
public class ImagePanel extends JPanel {
private JLabel[] dragonbornPortraits;
public ImagePanel() {
super();
}
private void createPortraitsArray() {
}
}
+106
View File
@@ -0,0 +1,106 @@
package GUI.img;
import javax.imageio.ImageIO;
import javax.swing.*;
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[] holder;
private int lastIndex;
public ImagePanel(String[] holder) {
super();
this.holder = holder;
createPortraitsArray();
updateImage(0); // Default selection of the 0th element.
}
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[holder.length][];
for (int i = 0; i < holder.length; i++) {
File imagesDir = new File("dndBuilder/src/GUI/img/" + holder[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;
}
}

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

Before

Width:  |  Height:  |  Size: 9.0 KiB

After

Width:  |  Height:  |  Size: 9.0 KiB

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

Before

Width:  |  Height:  |  Size: 111 KiB

After

Width:  |  Height:  |  Size: 111 KiB