protected JButton ab, hb, fb, ib;
public JJRealm_Adventures ()
{
ImageIcon leftButtonIcon = createImageIcon ("left.gif");
ab = new JButton ("Attack", leftButtonIcon);
ab.setMnemonic (KeyEvent.VK_A);
ab.setActionCommand ("attack");
hb = new JButton ("Heal", leftButtonIcon);
hb.setMnemonic (KeyEvent.VK_H);
hb.setActionCommand ("heal");
fb = new JButton ("Flee", leftButtonIcon);
//Use the default text position of CENTER, TRAILING (RIGHT).
fb.setMnemonic (KeyEvent.VK_F);
fb.setActionCommand ("flee");
ib = new JButton ("Item", leftButtonIcon);
//Use the default text position of CENTER, TRAILING (RIGHT).
ib.setMnemonic (KeyEvent.VK_I);
ib.setActionCommand ("item");
//Listen for actions on buttons 1 and 3.
ab.addActionListener (this);
hb.addActionListener (this);
fb.addActionListener (this);
ib.addActionListener (this);
ab.setToolTipText ("Click this button to attack");
hb.setToolTipText ("Click this button to defend");
fb.setToolTipText ("Click this button to flee");
ib.setToolTipText ("Click this button to access an item");
//Add Components to this container, using the default FlowLayout.
add (ab);
add (hb);
add (fb);
add (ib);
}
public void actionPerformed (ActionEvent e)
{
if ("attack".equals (e.getActionCommand ()))
{
monsterHP = (monsterHP - (playerATK - monsterAP));
playerHP = (playerHP - (monsterATK - playerAP));
drawPlayerGrid ();
playerStats ();
c.clear ();
}
else if ("heal".equals (e.getActionCommand ()))
{
playerHP = playerHP + 15;
c.clear ();
}
else if ("flee".equals (e.getActionCommand ()))
{
c.clear ();
c.setFont (Battle);
c.drawString ("you have fled", 50, 50);
c.getChar ();
condition = 5;
}
else if ("item".equals (e.getActionCommand ()))
{
}
if (monsterHP <= 0)
{
ab.setEnabled (false);
fb.setEnabled (false);
ib.setEnabled (false);
}
else if (playerHP <= 0)
{
ab.setEnabled (false);
fb.setEnabled (false);
ib.setEnabled (false);
}
else if (condition == 4)
{
ab.setEnabled (true);
fb.setEnabled (true);
ib.setEnabled (false);
}
if (race == "cleric")
{
hb.setEnabled (true);
}
else
{
hb.setEnabled (false);
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon (String path)
{
java.net.URL imgURL = JJRealm_Adventures.class.getResource (path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI ()
{
//Create and set up the window.
JFrame frame = new JFrame ("Battle");
frame.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);
//Create and set up the content pane.
JJRealm_Adventures newContentPane = new JJRealm_Adventures ();
newContentPane.setOpaque (true); //content panes must be opaque
frame.setContentPane (newContentPane);
//Display the window.
frame.pack ();
frame.setVisible (true);
}
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
createAndShowGUI ();
}
}
);
|