A Basic Character Generator in Java

When I swiched from Daily Steampunk to Meta Punk it was partly due to me wanting to branch out and do more stuff other than Steampunk, blog about other hobbies and interests of mine.

As it states in the sub-headline of the blog, one of those is coding. I have been coding in my spare time for the last three years now and have earned a number of certificates in the process, one of them from Harvard, also as part of the EdX program I blogged about below.

The course consisted of a number of lectures, some literature and problem sets. These usually were pre-coded code with some parts missing that we had to code by ourselves.
The final project was different, here we got to do whatever we wanted. I, being a role player, chose to do a charcater generator. It is a pretty basic one (due to time constraints and the course did not ask for much either) but it was enough to pass with full points:

 

 

Another dead give-away that this is a very basic character generator is, that it only uses two classes, one for the GUI (graphical user interface) and one for the dice-rolling, i.e. generating the character.
I am aware that the generator class could have been split into several smaller classes for better readability and maintenance. These are the classes:

 

package gui;
import java.awt.event.ItemEvent;

/**
* @author Marcus
*
*/
public class Generators {
private static int strength;

private static int intelligence;

private static int dexterity;

private static int wisdom;

private static int charisma;

private static int constitution;

private static String results = „People“;

private static String s;

private static int num = 0;

public static int getNum() {
return num;
}

public static void setNum(int num) {
Generators.num = num;
}

static GUI_CharacterGenerator gt = new GUI_CharacterGenerator();

// all getters and setters

public static int getStrength() {
return strength;
}

public static void setStrength(int strength) {
strength = strength;
}

public static int getIntelligence() {
return intelligence;
}

public static void setIntelligence(int intelligence) {
intelligence = intelligence;
}

public static int getDexterity() {
return dexterity;
}

public static void setDexterity(int dexterity) {
dexterity = dexterity;
}

public static int getWisdom() {
return wisdom;
}

public static void setWisdom(int wisdom) {
wisdom = wisdom;
}

public static int getCharisma() {
return charisma;
}

public static void setCharisma(int charisma) {
charisma = charisma;
}

public static int getConstitution() {
return constitution;
}

private static  void setConstitution(int constitution) {
constitution = constitution;
}

public static String getResults() {
return results;
}

public static  void setResults(String results) {
results = results;
}

public static void randomNumbers() {
// TODO Auto-generated method stub
//System.out.println(„Hagara“);
if (gt.getClassCheck()==0||getNum()==0){
GUI_CharacterGenerator.setP(„You have to choose a character class first!“);
}

if (gt.getClassCheck()==1||getNum()==1){

strength=(((int)(Math.random()*6))+12);
intelligence=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
dexterity=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
constitution=(((int)(Math.random()*6))+((int)(Math.random()*6))+8);
wisdom=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
charisma=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
//setCounter(1);
//s = „pelepones“;
setResults(s);
//System.out.println(„Hagara2“);
GUI_CharacterGenerator.setP(„Your stats are: \nStrength:  „+getStrength()+“\nIntelligence:  „+getIntelligence()+
„\nDexterity:  „+getDexterity()+“\nConstitution:  „+getConstitution()+“\nWisdom:  „+getWisdom()+
„\nCharisma:  „+getCharisma()+ „\nHitpoints: 12“);

}

else if (gt.getClassCheck()==2||getNum()==2){

strength=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
intelligence=(((int)(Math.random()*6))+((int)(Math.random()*6))+8);
dexterity=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
constitution=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
wisdom=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
charisma=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
GUI_CharacterGenerator.setP(„Your stats are: \nStrength:  „+getStrength()+“\nIntelligence:  „+getIntelligence()+
„\nDexterity:  „+getDexterity()+“\nConstitution:  „+getConstitution()+“\nWisdom:  „+getWisdom()+
„\nCharisma:  „+getCharisma()+ „\nHitpoints: 4“);

}

else if (gt.getClassCheck()==3||getNum()==3){

strength=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
intelligence=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
dexterity=(((int)(Math.random()*6))+((int)(Math.random()*6))+8);
constitution=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
wisdom=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
charisma=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
GUI_CharacterGenerator.setP(„Your stats are: \nStrength:  „+getStrength()+“\nIntelligence:  „+getIntelligence()+
„\nDexterity:  „+getDexterity()+“\nConstitution:  „+getConstitution()+“\nWisdom:  „+getWisdom()+
„\nCharisma:  „+getCharisma()+ „\nHitpoints: 6“);

}

else if (gt.getClassCheck()==4||getNum()==4){

strength=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
intelligence=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
dexterity=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
constitution=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
wisdom=(((int)(Math.random()*6))+((int)(Math.random()*6))+8);
charisma=(((int)(Math.random()*6))+((int)(Math.random()*6))+6);
GUI_CharacterGenerator.setP(„Your stats are: \nStrength:  „+getStrength()+“\nIntelligence:  „+getIntelligence()+
„\nDexterity:  „+getDexterity()+“\nConstitution:  „+getConstitution()+“\nWisdom:  „+getWisdom()+
„\nCharisma:  „+getCharisma()+ „\nHitpoints: 8“);
}
setResults(s);

}

}

And this is the GUI:

package gui;

//import GUI_CharacterGenerator;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import javax.swing.BoxLayout;

import java.awt.FlowLayout;

import javax.swing.SpringLayout;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ItemListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class GUI_CharacterGenerator extends JFrame {

private static String p;
private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI_CharacterGenerator frame = new GUI_CharacterGenerator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public GUI_CharacterGenerator() {
//Create a file chooser
final JFileChooser fc = new JFileChooser();
Generators gen = new Generators();
setTitle(„D&D Character Generator“);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 828, 487);

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu mnFile = new JMenu(„File“);
menuBar.add(mnFile);

JMenuItem mntmSave = new JMenuItem(„Save“);
mntmSave.addMouseListener(new MouseAdapter() {
JFileChooser FC = new JFileChooser();
@Override
public void mouseReleased(MouseEvent e) {

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(„/home/me/Documents“));
int retrival = chooser.showSaveDialog(null);
if (retrival == JFileChooser.APPROVE_OPTION) {
try {
FileWriter fw = new FileWriter(chooser.getSelectedFile()+“.txt“);
//fw.write(„Test“);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(getP());
bw.close();

} catch (Exception ex) {
ex.printStackTrace();
}

}

}

});

mnFile.add(mntmSave);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel CharacterImage = new JLabel(„“);
CharacterImage.setBounds(0, -2, 451, 432);
CharacterImage.setIcon(new ImageIcon(GUI_CharacterGenerator.class.getResource(„/images/default_griffin.png“)));
contentPane.add(CharacterImage);

JTextArea CharacterTextArea = new JTextArea();
CharacterTextArea.setEditable(false);
CharacterTextArea.setBounds(550, 46, 252, 296);
CharacterTextArea.setText(„Select a character class!“);
contentPane.add(CharacterTextArea);
CharacterTextArea.setVisible(false);

JButton btnCreateYourCharacter = new JButton(„Create your character“);
btnCreateYourCharacter.setBounds(550, 353, 160, 23);
contentPane.add(btnCreateYourCharacter);
btnCreateYourCharacter.setEnabled(false);
btnCreateYourCharacter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
//createCharacterButtonActionPerformed(evt);
gen.randomNumbers();
//wtf();
CharacterTextArea.setText(p);

}
});

JComboBox characterChoices = new JComboBox();
characterChoices.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
//characterChoicesItemStateChanged(evt);

if (evt.getItem().toString()==“Make your choice!“){
btnCreateYourCharacter.setEnabled(false);

// System.out.println(„Warrior“);
CharacterImage.setIcon(new javax.swing.ImageIcon(getClass().getResource(„/images/default_griffin.png“)));
CharacterTextArea.setText(„You must choose a class first, before you can create a character.“);
classCheck=0;
}

if (evt.getItem().toString()==“Warrior“){
btnCreateYourCharacter.setEnabled(true);
CharacterTextArea.setVisible(true);
// System.out.println(„Warrior“);
setClassCheck(1);
gen.setNum(1);
CharacterImage.setIcon(new javax.swing.ImageIcon(getClass().getResource(„/images/female_fighter.png“)));
CharacterTextArea.setText(„The Warrior is skilled in combat, and usually \ncan make use of some of the most powerful \nheavy armor and weaponry in the game. \nAs such, the warrior is well-rounded \nphysical combatant. \nSometimes, the Warrior (or more often \nthe Knight, if it is a separate class) \nmay be able to learn basic magic, but its \ncapabilities in this field are somewhat limited.“);
classCheck=1;
}

if (evt.getItem().toString()==“Mage“){
//System.out.println(„Mage“);
CharacterTextArea.setVisible(true);
btnCreateYourCharacter.setEnabled(true);
CharacterImage.setIcon(new javax.swing.ImageIcon(getClass().getResource(„/images/male_mage.png“)));
CharacterTextArea.setText(„Mages are considered to be spellcasters \nwho wield powerful spells, but are \noften physically weak as a trade-off. \nMages are commonly confused with similar \noffensive spellcasting classes such as \nthe Warlock and the Necromancer. \nHowever, a Mage’s power is based on the \narcane and a Warlock or Necromancer’s \npower is based on darkness or death. \nMages are primarily based on wizards from \nassorted fantasy literature.“);
classCheck=2;
gen.setNum(2);

}

if (evt.getItem().toString()==“Rogue“){
// System.out.println(„Rogue“);
CharacterTextArea.setVisible(true);
btnCreateYourCharacter.setEnabled(true);
CharacterImage.setIcon(new javax.swing.ImageIcon(getClass().getResource(„/images/male_rogue.png“)));
CharacterTextArea.setText(„The rogue can be the rather typical thief.\n Someone who makes his living \non the streets of the city, picking pockets \nand snatching goods from the market. \nMore experienced thieves might \nbecome burglars or other more \nspecialized kinds of rogues. \nSome may even train to become \nassassins or bards.“);
classCheck=3;
gen.setNum(3);
}

if (evt.getItem().toString()==“Cleric“){
// System.out.println(„Cleric“);
CharacterTextArea.setVisible(true);
btnCreateYourCharacter.setEnabled(true);
CharacterImage.setIcon(new javax.swing.ImageIcon(getClass().getResource(„/images/cleric.jpg“)));
CharacterTextArea.setText(„The cleric is a healer, usually a priest\n and a holy warrior, originally modeled \non or inspired by the Military Orders. \nClerics are sometimes members of \nreligious orders. \nMost clerics have powers to heal wounds, \nprotect their allies and sometimes \nresurrect the dead, as well as summon, \nmanipulate and banish undead. „);
classCheck=4;
gen.setNum(4);
}

if(evt.getItem().toString()== „Make your choice!“){
CharacterImage.setIcon(new javax.swing.ImageIcon(getClass().getResource(„/images/default_griffin.png“)));
}

}
});
characterChoices.setModel(new DefaultComboBoxModel(new String[] {„Select a Class“, „Warrior“, „Mage“, „Rogue“, „Cleric“, „Select!“}));
characterChoices.setSelectedIndex(0);
characterChoices.setBounds(398, 48, 142, 20);
characterChoices.setToolTipText(„Choose your class“);

contentPane.add(characterChoices);

JButton btnReset = new JButton(„Reset“);
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CharacterTextArea.setText(„“);
//jLabel1.setIcon(new javax.swing.ImageIcon(„C:\\Users\\Marcus\\Pictures\\default_griffin.png“));
characterChoices.setSelectedIndex(0);
//CharacterTextArea.setText(„“);
CharacterTextArea.setVisible(false);
btnCreateYourCharacter.setEnabled(false);
CharacterImage.setIcon(new ImageIcon(GUI_CharacterGenerator.class.getResource(„/images/default_griffin.png“)));

}
});
btnReset.setBounds(714, 353, 73, 23);
contentPane.add(btnReset);

String p = „Nothing to see here“;

}

Generators gen = new Generators();
private int classCheck;

public int getClassCheck() {
return classCheck;
}

public void setClassCheck(int classCheck) {
this.classCheck = classCheck;
}

public static String getP() {
return p;
}

public static void setP(String p) {
GUI_CharacterGenerator.p = p;
}

}

 

Now, if you copy this code and run it, you will notice the images do not appear, this is because I did not include them. I do not know who the artist is since I do not know anymore, where I got them from. I think it was from DeviantArt, but I was unabe to find them there. Anyway, I used them within the limits of fair use and if you happen to know who it was that created the character images and the Griffon, let me know.