race GUI refactoring nearly completed with InfoPanel implementation
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user