Click to See Complete Forum and Search --> : Java Applet Classes Help


X5Alexander
April 23rd, 2009, 09:07 AM
We are making a simple game using a java applet, using Eclipse. We have two classes and they both run at the same time. We want the second class to run only when a button is clicked.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class JavaFinal extends Applet implements ActionListener
{
static final long serialVersionUID = -1L;

double mag = 5;
String magStr;
double str = 5;
String strStr;
double dex = 5;
String dexStr;
int statPoints = 10;

Image logo;

String name;

Label heroNameLabel = new Label("Enter the name of your hero: ");
Label blankLabel = new Label(" ");
TextField heroNameField = new TextField(10);
TextField magField = new TextField(10);
TextField strField = new TextField(10);
TextField dexField = new TextField(10);

Button allocateStatsMag;
Button allocateStatsStr;
Button allocateStatsDex;
Button allocationDone;
Button adventure;



public void init()
{
add(heroNameLabel);
add(heroNameField);
add(blankLabel);

add(magField);
allocateStatsMag = new Button("Magic + 1");
add(allocateStatsMag);
String magStr = new Double(mag).toString();
magField.setText(magStr);
allocateStatsMag.addActionListener(this);

add(strField);
allocateStatsStr = new Button("Strength + 1");
add(allocateStatsStr);
String strStr = new Double(str).toString();
strField.setText(strStr);
allocateStatsStr.addActionListener(this);

add(dexField);
allocateStatsDex = new Button("Dexterity + 1");
add(allocateStatsDex);
String dexStr = new Double(dex).toString();
dexField.setText(dexStr);
allocateStatsDex.addActionListener(this);

allocationDone = new Button("FINISHED :]");
add(allocationDone);
allocationDone.addActionListener(this);

adventure = new Button("Adventure Here!");
add(adventure);
adventure.addActionListener(this);
adventure.setVisible(false);

logo = getImage(getDocumentBase(), "SpookyRavenManor.gif");

new MyShop();
}

public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
if(source == allocateStatsMag && statPoints>0)
{
String magStr = new Double(mag).toString();
mag = Double.parseDouble(magStr);
mag = mag + 1;
magStr = new Double(mag).toString();
magField.setText(magStr);
statPoints--;


}

if(source == allocateStatsStr && statPoints>0)
{
String strStr = new Double(str).toString();
str = Double.parseDouble(strStr);
str = str + 1;
strStr = new Double(str).toString();
strField.setText(strStr);
statPoints--;


}

if(source == allocateStatsDex && statPoints>0)
{
String dexStr = new Double(dex).toString();
dex = Double.parseDouble(dexStr);
dex = dex + 1;
dexStr = new Double(dex).toString();
dexField.setText(dexStr);
statPoints--;


}
if(source == allocationDone && statPoints == 0)
{
allocateStatsMag.setVisible(false);
allocateStatsStr.setVisible(false);
allocateStatsDex.setVisible(false);
magField.setVisible(false);
strField.setVisible(false);
dexField.setVisible(false);
heroNameField.setVisible(false);
heroNameLabel.setVisible(false);
blankLabel.setVisible(false);
allocationDone.setVisible(false);
adventure.setVisible(true);
String name = heroNameField.getText();
System.out.println(name);

//No weapon//
//Dagger = 1//
//longsword = 2//
//greatsword = 3//
//crossbow = 4//
//longbow = 5//
//shurikens = 6//
//wand = 7//
//staff = 8//
//beam katana = 9//
int weapon = 1;
if(weapon == 1)
{
str = str+3;
System.out.println(str);
}


}
if(source == adventure)
{
adventure.setVisible(false);
statPoints=1;
repaint();

MyShop myShop = new MyShop();


}
}

public void paint(Graphics g)
{
if(statPoints == 0)
{
g.drawImage(logo,20,20,this);
}


}


class MyShop extends Applet
{
static final long serialVersionUID = -1L;

public MyShop()
{
System.out.println("test1");
testmethod();
}

public void testmethod()
{
System.out.println("test2");
}


}

}

keang
April 24th, 2009, 11:26 AM
AFAIA you can't stop the second applet from loading when the page is displayed but you could get it to display a blank screen until it receives notification (http://www.wutka.com/hackingjava/ch10.htm) to run from applet 1.

X5Alexander
April 27th, 2009, 08:01 AM
Many thanks