I have a project which I have been working on for the past few days and I have recently reached a point where I don't understand what to do. My program must have a main menu with "Add, remove, List Record #, List All, Sort, Exit" functions.
I am supposed to also include a "Database" class which must implement:
addItem(), removeItem(), listItems(), listParticularItem(), sortItems().
My program is basically a horrible fail of a "game" which basically allows you to name a character, pick a race and class. I am also expected to allow the user to be able to create multiple characters and these characters have to be made in an "Array".
Main Class:
/**
* @(#)Game.java
*
* Game application
*
* @author
* @version 1.00 2010/1/7
*/
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
public class Game
{
String name;
public Game(String nm)
{
name = nm;
// age = yearsold;
// b1 = new Book("Comp Sci", 234, "Hard cover");
// bin1 = new Binder("Blue", 3, "Five Star");
}
public String getName()
{
return name;
}
public void setName(String lawl)
{
name = lawl;
}
public static void main(String[] args)
{
Game[] Chars = new Game[5];
//Database[] charsDB = new Database[5];
Chars[0] = new Game("");
new GUI1(Chars);
}
}
GUI Class:
/**
* @(#)GUI.java
*
*
* @author
* @version 1.00 2010/1/7
*/
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
public class GUI1 extends JFrame
{
JFrame mainwindow, namewindow, racewindow, classwindow;
JPanel mainpanel, namepanel, racepanel, classpanel;
JLabel imagelbl, nameBgLbl,nameFlLbl, racelbl, racewinLbl, scrollLbl, raceinfoLbl, nameShow;
JButton start, exit, nextrace, toClass, show;
JRadioButton human,orc,elf;
JTextField nametext;
String name;
int guinum;
Game[] newChar = new Game[5];
public GUI1(Game[] gameChar)
{
newChar= gameChar;
//Frames
mainwindow = new JFrame("Game");
mainwindow.setSize(500,500);
mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
namewindow = new JFrame("Create a new character");
namewindow.setSize(500,500);
namewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
racewindow = new JFrame("Pick a race");
racewindow.setSize(400,501);
racewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
classwindow = new JFrame("Pick a class");
classwindow.setSize(500,500);
classwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Panels
mainpanel = new JPanel();
mainpanel.setBounds(0,0,500,500);
mainpanel.setBackground(Color.red);
mainpanel.setLayout(null);
mainwindow.getContentPane().add(mainpanel);
namepanel = new JPanel();
namepanel.setBackground(Color.red);
namepanel.setBounds(0,0,500,500);
namepanel.setLayout(null);
namewindow.getContentPane().add(namepanel);
racepanel = new JPanel();
racepanel.setBackground(Color.white);
racepanel.setBounds(0,0,500,501);
racepanel.setLayout(null);
racewindow.getContentPane().add(racepanel);
classpanel = new JPanel();
classpanel.setBackground(Color.red);
classpanel.setBounds(0,0,400,400);
classpanel.setLayout(null);
classwindow.getContentPane().add(classpanel);
//Buttons
ImageIcon startBt = new ImageIcon(getClass().getResource("create.png"));
start = new JButton("");
start.setIcon(startBt);
start.setBounds(0,0,101,35);
start.addActionListener(new MyAction());
mainpanel.add(start);
ImageIcon exitBt = new ImageIcon(getClass().getResource("exit.png"));
exit = new JButton("");
exit.setIcon(exitBt);
exit.setBounds(411,0,84,38);
exit.addActionListener(new MyAction());
mainpanel.add(exit);
ImageIcon nameok = new ImageIcon(getClass().getResource("nameok.png"));
nextrace = new JButton("");
nextrace.setBounds(223,316,66,29);
nextrace.setIcon(nameok);
nextrace.addActionListener(new MyAction());
namepanel.add(nextrace);
toClass = new JButton("Next");
toClass.setBounds(200,240,100,20);
toClass.addActionListener(new MyAction());
racepanel.add(toClass);
show = new JButton("show");
show.setBounds(0,0,100,30);
show.addActionListener(new MyAction());
classpanel.add(show);
//Radio Buttons
human = new JRadioButton("Human");
human.setBounds(0,0,100,15);
human.setOpaque(false);
human.addActionListener(new MyRace());
racepanel.add(human);
orc = new JRadioButton("Orc");
orc.setBounds(0,15,100,15);
orc.setOpaque(false);
orc.addActionListener(new MyRace());
racepanel.add(orc);
elf = new JRadioButton("Elf");
elf.setBounds(0,30,100,15);
elf.setOpaque(false);
elf.addActionListener(new MyRace());
racepanel.add(elf);
ButtonGroup bg = new ButtonGroup();
bg.add(human);
bg.add(orc);
bg.add(elf);
//TextFields
nametext = new JTextField(15);
nametext.setBounds(180,239,147,23);
nametext.setFont(new Font("Arial", Font.PLAIN, 13));
nametext.setForeground(Color.white);
nametext.setDocument(new JTextFieldLimit(12));
nametext.setOpaque(false);
namepanel.add(nametext);
//Images
raceinfoLbl = new JLabel();
raceinfoLbl.setBounds(145,-27,220,250);
raceinfoLbl.setHorizontalAlignment(JLabel.CENTER);
racepanel.add(raceinfoLbl);
racelbl = new JLabel();
racelbl.setBounds(120,225,300,300);
racelbl.setHorizontalAlignment(JLabel.CENTER);
racepanel.add(racelbl);
ImageIcon mainpic = new ImageIcon(getClass().getResource("Main.png"));
imagelbl = new JLabel("",mainpic,JLabel.CENTER);
imagelbl.setBounds(0,-5,500,500);
mainpanel.add(imagelbl);
ImageIcon nameBackground = new ImageIcon(getClass().getResource("namecharBg.png"));
nameBgLbl = new JLabel("",nameBackground,JLabel.CENTER);
nameBgLbl.setBounds(-5,0,500,500);
namepanel.add(nameBgLbl);
ImageIcon scrollInfo= new ImageIcon(getClass().getResource("Scroll.png"));
scrollLbl = new JLabel();
scrollLbl.setIcon(scrollInfo);
scrollLbl.setBounds(125,0,250,250);
racepanel.add(scrollLbl);
ImageIcon raceBg= new ImageIcon(getClass().getResource("racebg.png"));
racewinLbl = new JLabel();
racewinLbl.setIcon(raceBg);
racewinLbl.setBounds(0,-8,500,500);
racepanel.add(racewinLbl);
nameShow = new JLabel();
nameShow.setBounds(125,0,250,250);
classpanel.add(nameShow);
//Visibility
mainwindow.setVisible(true);
mainwindow.setResizable(false);
racewindow.setResizable(false);
namewindow.setResizable(false);
}
//Limit text in JTextfield constructor
public class JTextFieldLimit extends PlainDocument
{
private int limit;
// optional uppercase conversion
private boolean toUppercase = false;
JTextFieldLimit(int limit)
{
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper)
{
super();
this.limit = limit;
toUppercase = upper;
}
public void insertString
(int offset, String str, AttributeSet attr)
throws BadLocationException
{
if (str == null) return;
if ((getLength() + str.length()) <= limit)
{
if (toUppercase) str = str.toUpperCase();
super.insertString(offset, str, attr);
}
}
}// end of limit text constructors
//Action Listener
public class MyAction implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource() == start)
{
mainwindow.setVisible(false);
namewindow.setVisible(true);
}
if(e.getSource()==exit)
{
System.exit(0);
}
if(e.getSource()==nextrace)
{
String name = nametext.getText();
newChar[0].setName(name);
//d1.add(name);
nametext.setText(" ");
mainwindow.setVisible(false);
namewindow.setVisible(false);
racewindow.setVisible(true);
}
if(e.getSource()==toClass)
{
mainwindow.setVisible(false);
namewindow.setVisible(false);
racewindow.setVisible(false);
classwindow.setVisible(true);
}
if(e.getSource()==show)
{
nameShow.setText(newChar[0].getName());
}
}
}
public class MyRace implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource() == human)
{
racelbl.setIcon(new ImageIcon("Human.png"));
raceinfoLbl.setText("<html><b>The noble warriors of humanity employ both a strong military and powerful magics in the defense of their shining kingdoms. Human's are best suited for players who prefer a balance in all areas.</b></html>");
}
if(e.getSource() == orc)
{
racelbl.setIcon(new ImageIcon("Orc.png"));
raceinfoLbl.setText("<html><b>The green skinned Orc is a race which is feared for their brute strength and raw melee power along with great accuracy, they can make a great ranged class. Orc's have amazing strength and are made for players who enjoy rushing into the front lines and dealing major hits.</b></html>");
}
if(e.getSource() == elf)
{
racelbl.setIcon(new ImageIcon("Elf.png"));
raceinfoLbl.setText("<html><b>The ancient and wise Elves were believed to be around since the beginning of time. Players who want to be outstanding in the control of their magic, or want to use their shadowy powers in conjunction with melee powers should attempt playing Elves.</b></html>");
}
}
}
}
//Junk Code
//setText(newChar[0].getName());
Database Class:
/**
* @(#)Database.java
*
*
* @author
* @version 1.00 2010/1/14
*/
public class Database {
Game[] Chars;
public Database(int num)
{
Chars = new Game[num];
}
}
I honestly don't understand the database concept so my database class is really not completed and this is why I have come here for assistance. Some how I am supposed to allow the user to create a character, which has a name, race and class and then allow the user to create more characters if they prefer. After they are completed I am to let them have functions which can sort these characters or what not.
Help would greatly be appreciated.
Thanks,
GoldenDrago (Ray)