I have the following problem For Java GUI project you will i
I have the following problem:
For Java GUI project, you will implement a simple Character Creation Screen as found in many different RPGs (role playing games.) This screen will allow the user to pick and save a character’s specifications from a list of options.
The interface must have the following:
(Radio Buttons) The user may select among three options for the character’s Gender:
Male
Female
Hermaphrodite
(A Drop-Down List) The user may select from among the following Professions for the character:
Warrior, Barbarian, Monk, Mage, Thief
(A Text Field) The user may enter his/her character’s Name, which must be at least one character long, but may be as long as 10 characters
(Sliders and a label) The user has 100 skill points to spend across 4 different passive skills. The user will see a label with “100” on it originally. o Whenever he/she slides a slider to increase the skill points in that skill, the points are taken from the 100 points (or however many are remaining.)
When the user decreases the skill points in a particular area, they are returned to the “points left to spend” pool, and this is reflected in the label as well. The remaining points will always be displayed in this label.
If the player runs out of points to spend, he/she should not be able to increase the points in any skill area until points are returned to the pool, and are available to be spent
The four (4) skill areas are as follows:
Intelligence, Dexterity, Strength, and Wisdom
There will be a Menu System for opening and saving a saved character.
The menu bar should contain two titles: File and Options
Under the File Menu, there are two options:
Open a saved character
Save a character
Under the Options Menu, there are two options:
Reset All
Exit File
Open (a Saved Character)
With this option, you should be able to open a saved character file, for example, Bob.player or Sue.player
The character’s information will be read into the program and all the components on the GUI will be set accordingly, as if the user had just entered them. If the user makes changes, these changes should overwrite the current file (upon a save.)
File Save (a Character)
This option allows the user to save the current player he/she is working on. The file will contain information that represents the state of the GUI and must be readable by the Open option. The file must be saved as character_name.player.
You should prompt the user and say “Are you sure you want to save this charcter?” before they actually save it. Your dialog prompt should allow the user to Cancel the save if he/she so desires.
Reset All
Reset All sets all the GUI components to their zero state. Note that this option does not automatically save the character data.
Exit
This is self-explanatory. Close the Character Creation application.
How do you I do this with the following code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
public class Character extends JFrame
{
//Constants for window width and height
private final int WINDOW_WIDTH = 1500;
private final int WINDOW_HEIGHT = 250;
//Character gender
private JRadioButton male;
private JRadioButton female;
private JRadioButton hermaphrodite ;
//Character Name and Class
private JTextField characterName;
private JComboBox proffesion;
//Character Stats
private JSlider intelligence;
private JSlider dexterity;
private JSlider strength;
private JSlider wisdom;
//Groups gender
private ButtonGroup characterGender;
//Panel groups
private JPanel gender;
private JPanel namePanel;
private JPanel intelPanel;
private JPanel dexPanel;
private JPanel strengthPanel;
private JPanel wisdomPanel;
// CONSTRUCTOR
public Character()
{
//Window title
setTitle(\"Character Sheet\");
//Set size
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
//Default close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Calling buildPanel method
buildPanels();
//Grid Layout
setLayout(new FlowLayout());
//Adding the panel to JFrame
add(gender);
add(namePanel);
add(intelPanel);
add(dexPanel);
add(strengthPanel);
add(wisdomPanel);
//Visibility
setVisible(true);
// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();
// Add the menubar to the frame
setJMenuBar(menuBar);
// Define and add two drop down menu to the menubar
JMenu fileMenu = new JMenu(\"File\");
JMenu editMenu = new JMenu(\"Option\");
menuBar.add(fileMenu);
menuBar.add(editMenu);
// Create and add simple menu item to one of the drop down menu
JMenuItem resetAction = new JMenuItem(\"Reset all\");
JMenuItem openAction = new JMenuItem(\"Open a saved character\");
JMenuItem exitAction = new JMenuItem(\"Exit\");
JMenuItem saveAction = new JMenuItem(\"Save\");
// Create a Menu ButtonGroup
ButtonGroup bg = new ButtonGroup();
fileMenu.add(openAction);
fileMenu.add(saveAction);
editMenu.add(resetAction);
editMenu.add(exitAction);
// Create JSLider
JSlider intelligence = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
JSlider dexterity = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
JSlider strength = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
JSlider wisdom = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
}
// Building the Panels,
private void buildPanels()
{
//Initializing butons
male = new JRadioButton(\"Male\");
female = new JRadioButton(\"Female\");
hermaphrodite = new JRadioButton(\"Hermaphrodite\");
characterGender = new ButtonGroup();
characterGender.add(male);
characterGender.add(female);
characterGender.add(hermaphrodite);
//Initializing TextField and JComboBox
characterName = new JTextField(10);
String[] classes = { \"Warrior\", \"Barbarian\", \"Monk\", \"Mage\", \"Thief\" };
JComboBox proffesion = new JComboBox(classes);
//Initializing JSlider
intelligence = new JSlider();
dexterity = new JSlider();
strength = new JSlider();
wisdom = new JSlider();
//Initializing the panels
gender = new JPanel();
namePanel = new JPanel();
intelPanel = new JPanel();
dexPanel = new JPanel();
strengthPanel= new JPanel();
wisdomPanel = new JPanel();
//Setting borders of the panels
gender.setBorder(BorderFactory.createTitledBorder(\"Gender\"));
namePanel.setBorder(BorderFactory.createTitledBorder(\"Name and Class\"));
intelPanel.setBorder(BorderFactory.createTitledBorder(\"Intelligence\"));
dexPanel.setBorder(BorderFactory.createTitledBorder(\"Dexterity\"));
strengthPanel.setBorder(BorderFactory.createTitledBorder(\"Strength\"));
wisdomPanel.setBorder(BorderFactory.createTitledBorder(\"Wisdom\"));
//Adding the objecs to the respective panels
gender.add(male);
gender.add(female);
gender.add(hermaphrodite);
namePanel.add(characterName);
namePanel.add(proffesion);
//Creating Stats and
int startValue = 0;
intelPanel.add(intelligence);
intelligence.setValue(startValue);
intelligence.setMajorTickSpacing(25);
intelligence.setMinorTickSpacing(25);
intelligence.setPaintTicks(true);
intelligence.setPaintLabels(true);
dexPanel.add(dexterity);
dexterity.setValue(startValue);
dexterity.setMajorTickSpacing(25);
dexterity.setMinorTickSpacing(25);
dexterity.setPaintTicks(true);
dexterity.setPaintLabels(true);
strengthPanel.add(strength);
strength.setValue(startValue);
strength.setMajorTickSpacing(25);
strength.setMinorTickSpacing(25);
strength.setPaintTicks(true);
strength.setPaintLabels(true);
wisdomPanel.add(wisdom);
wisdom.setValue(startValue);
wisdom.setMajorTickSpacing(25);
wisdom.setMinorTickSpacing(25);
wisdom.setPaintTicks(true);
wisdom.setPaintLabels(true);
//Associating action listener
// Open.addActionListener(new OpenButtonListener());
//Save.addActionListener(new SaveButtonListener());
//Reset.addActionListener(new ResetButtonListener());
//Exit.addActionListener(new ExitButtonListener());
}//End buildPanels
/**
Action listener
*/
private class OpenButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent openaction)
{
}
}
private class SaveButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent saveaction)
{
}
}
private class ResetButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent resetaction)
{
}
}
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent exitaction)
{
}
}
public static void main(String[] args)throws IOException
{
new Character();
}
}
Solution
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw{
JFrame frame;
Canvas canvas;
BufferStrategy bufferStrategy;
private int WIDTH = 1500;
private int HEIGHT = 250;
Draw(){
frame = new JFrame(\"Basic Game\");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
panel.setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, WIDTH, HEIGHT);
canvas.setIgnoreRepaint(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
panel.add(canvas);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
canvas.requestFocus();
canvas.setBackground(Color.black);
canvas.addKeyListener(new ButtonHandler());
}
void render() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, WIDTH, HEIGHT);
render(g);
g.dispose();
bufferStrategy.show();
}
protected void render(Graphics2D g){
}
}






