Refactoring DescriptionPanel code and logic, pending further refinement and layout.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package Resources.CustomAssets;
|
||||
|
||||
import GUI.AppTheme;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.border.LineBorder;
|
||||
import java.awt.*;
|
||||
|
||||
public class BasicNavPanel extends JPanel implements AppTheme {
|
||||
|
||||
private DefaultButton backButton;
|
||||
private DefaultButton continueButton;
|
||||
private int continueRequirement;
|
||||
|
||||
public BasicNavPanel(DefaultButton backButton, DefaultButton continueButton, int continueRequirement) {
|
||||
|
||||
super(new GridLayout(1, 2, 30, 0));
|
||||
this.backButton = backButton;
|
||||
this.continueButton = continueButton;
|
||||
|
||||
this.continueRequirement = continueRequirement;
|
||||
createNavPanel();
|
||||
|
||||
}
|
||||
|
||||
public int getContinueRequirement() {
|
||||
|
||||
return continueRequirement;
|
||||
|
||||
}
|
||||
|
||||
public void decreaseContinueRequirement(int value) {
|
||||
|
||||
continueRequirement -= value;
|
||||
updateContinueRequirement();
|
||||
|
||||
}
|
||||
|
||||
public void increaseContinueRequirement(int value) {
|
||||
|
||||
continueRequirement += value;
|
||||
updateContinueRequirement();
|
||||
|
||||
}
|
||||
|
||||
private void updateContinueRequirement() {
|
||||
|
||||
removeAll();
|
||||
|
||||
createNavPanel();
|
||||
|
||||
}
|
||||
|
||||
private void createNavPanel() {
|
||||
|
||||
setBackground(darkestBrown);
|
||||
setBorder(new EmptyBorder(15, 150, 15, 150));
|
||||
add(backButton);
|
||||
|
||||
if(continueRequirement == 0) {
|
||||
|
||||
add(continueButton);
|
||||
|
||||
} else {
|
||||
|
||||
DefaultButton requiredStepsNeeds = new DefaultButton(Integer.toString(continueRequirement) + " Points Left");
|
||||
requiredStepsNeeds.select();
|
||||
requiredStepsNeeds.setBorder(new LineBorder(lightAccent, 1));
|
||||
requiredStepsNeeds.setBorderPainted(true);
|
||||
requiredStepsNeeds.setEnabled(false);
|
||||
add(requiredStepsNeeds);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package Resources.CustomAssets;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface CustomTheme {
|
||||
|
||||
public Color background = null;
|
||||
public Color foreground = null;
|
||||
|
||||
Color getBackground();
|
||||
|
||||
Color getForeground();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package Resources.CustomAssets;
|
||||
|
||||
import GUI.AppTheme;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
public class ImageInfoPanel extends InfoPanel implements AppTheme {
|
||||
|
||||
private JLabel[][] portraits;
|
||||
private int lastIndex;
|
||||
|
||||
public ImageInfoPanel(String[] categoryHolder, String[] holderDescriptions, Rectangle[] bounds, int panelIndex) {
|
||||
|
||||
super(categoryHolder, holderDescriptions, bounds, panelIndex);
|
||||
|
||||
createPortraits();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateInfo(int holderIndex) {
|
||||
|
||||
super.updateInfo(holderIndex, portraits[holderIndex][getNextPortrait(holderIndex)]);
|
||||
|
||||
}
|
||||
|
||||
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 createPortraits() {
|
||||
|
||||
String category = "";
|
||||
|
||||
if(panelIndex == 1) {
|
||||
|
||||
category = "Races";
|
||||
|
||||
} else if (panelIndex == 2) {
|
||||
|
||||
category = "Classes";
|
||||
|
||||
}
|
||||
|
||||
portraits = new JLabel[categoryHolder.length][];
|
||||
|
||||
for (int i = 0; i < categoryHolder.length; i++) {
|
||||
|
||||
File imagesDir;
|
||||
|
||||
if(OS.startsWith("Windows")) {
|
||||
|
||||
imagesDir = new File("dndBuilder\\src\\Resources\\Img\\" + category + "\\" + categoryHolder[i].toLowerCase() + "\\");
|
||||
|
||||
} else if (OS.startsWith("Mac")) {
|
||||
|
||||
imagesDir = new File("dndBuilder/src/Resources/Img/" + category + "/" + categoryHolder[i].toLowerCase() + "/");
|
||||
|
||||
} else if (OS.startsWith("Linux")) {
|
||||
|
||||
imagesDir = new File("dndBuilder/src/Resources/Img/" + category + "/" + categoryHolder[i].toLowerCase() + "/");
|
||||
|
||||
} 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(lightBrown);
|
||||
portraits[i][index].setBounds(bounds[3]);
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private JLabel image(File file) {
|
||||
|
||||
BufferedImage img;
|
||||
Image finalImg;
|
||||
JLabel picture;
|
||||
|
||||
try {
|
||||
|
||||
img = ImageIO.read(file);
|
||||
finalImg = img.getScaledInstance(250, 375, 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package Resources.CustomAssets;
|
||||
|
||||
import GUI.AppTheme;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
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 implements AppTheme {
|
||||
|
||||
protected JTextArea[] descriptions;
|
||||
protected Rectangle[] bounds;
|
||||
protected String[] holderDescriptions;
|
||||
protected String[] categoryHolder;
|
||||
protected int panelIndex;
|
||||
|
||||
public InfoPanel(String[] categoryHolder, String[] holderDescriptions, Rectangle[] bounds, int panelIndex) {
|
||||
|
||||
super();
|
||||
this.categoryHolder = categoryHolder;
|
||||
this.holderDescriptions = holderDescriptions;
|
||||
this.bounds = bounds;
|
||||
this.panelIndex = panelIndex;
|
||||
|
||||
panelSetup();
|
||||
|
||||
createDescriptions();
|
||||
|
||||
}
|
||||
|
||||
public void updateInfo(int holderIndex) {
|
||||
removeAll();
|
||||
panelSetup();
|
||||
|
||||
add(descriptions[holderIndex]);
|
||||
|
||||
repaint();
|
||||
revalidate();
|
||||
|
||||
}
|
||||
|
||||
protected void updateInfo(int holderIndex, JLabel portrait) {
|
||||
removeAll();
|
||||
panelSetup();
|
||||
|
||||
add(descriptions[holderIndex]);
|
||||
add(portrait);
|
||||
|
||||
repaint();
|
||||
revalidate();
|
||||
|
||||
}
|
||||
|
||||
private void panelSetup() {
|
||||
|
||||
setOpaque(false);
|
||||
setBounds(bounds[0]);
|
||||
setLayout(null);
|
||||
|
||||
JLabel subHeader = new JLabel("Description");
|
||||
subHeader.setFont(subHeaderFont);
|
||||
subHeader.setHorizontalAlignment(SwingConstants.LEFT);
|
||||
subHeader.setBounds(bounds[1]);
|
||||
subHeader.setForeground(darkestBrown);
|
||||
|
||||
add(subHeader);
|
||||
|
||||
}
|
||||
|
||||
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(true);
|
||||
tmpDesc.setEditable(false);
|
||||
tmpDesc.setFocusable(false);
|
||||
tmpDesc.setFont(paragraphFont);
|
||||
tmpDesc.setBounds(bounds[2]);
|
||||
tmpDesc.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
tmpDesc.setBackground(medBrown);
|
||||
tmpDesc.setForeground(darkestBrown);
|
||||
|
||||
descriptions[i] = tmpDesc;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user