Problems with illegal start of expression, and class, interface or enum...
Hi. I'm having problems with my little experiment. In class, we where supposed to grab or make some kind of simple interface, and add our own quirk to it.
I toyed around with some things and settled for a toolbar approach, and now i'm having some difficulties. I wanted to make some kind of rpg-ish thing, with a player class and a monster, and a three buttons. One that restores the 'health' status of the player class, one that spawns a monster class and compares the player 'strength' status with that of the monster, and one that doesn't do anything until 10 monster classes has been defeated. And a textinput for easter eggs ;D
Everything's been running fine enough until now. At the end of the code, i've received two errors; "illegal start of expression", and "class, interface or enum expected." Two errors to which i am completely dumbstruck as to the cause. Searched around in the code, but I can't find anything immediately
wrong with it.
Oh, and this is by no means the finished code. I haven't quite figured out a way to randomize the stats of the monster class yet ;^.^
Here it is:
package Rollspel;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.net.URL;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class player{
int level;
int xp;
int Class;
int str;
int Int;
int dex;
int health;
int killcount;
public player(int tmp_level,int tmp_xp,int tmp_Class,int tmp_str,int tmp_Int,int tmp_dex,int tmp_health,int tmp_killcount){
level=tmp_level;
xp=tmp_xp;
Class=tmp_Class;
str=tmp_str;
Int=tmp_Int;
dex=tmp_dex;
health=tmp_health;
killcount=tmp_killcount;
}
publicvoid setlevel(int newValue){
level = newValue;
}
publicvoid setxp(int newValue){
xp=newValue;
}
publicvoid setClass(int newValue){
Class=newValue;
}
publicvoid setstr(int newValue){
str=newValue;
}
publicvoid setInt(int newValue){
Int=newValue;
}
publicvoid setdex(int newValue){
dex=newValue;
}
publicvoid sethealth(int newValue){
health=newValue;
}
publicvoid setkillcount(int newValue){
killcount=newValue;
}
public player(){
level=1;
xp=0;
Class=3;
str=20;
Int=30;
dex=20;
health=str*dex/2;
killcount=0;
if (xp==10){
level++;
str=str*3/2;
dex=dex*3/2;
System.out.println("Congrats! Lvl 2" );
}
if (xp==20){
level++;
str=str*3/2;
dex=dex*3/2;
}
}
}
class Monster{
int health2;
int str2;
int dex2;
public Monster(int tmp_health2,int tmp_str2,int tmp_dex2){
health2=tmp_health2;
str2=tmp_str2;
dex2=tmp_dex2;
}
publicvoid sethealth2(int newValue){
health2=newValue;
}
publicvoid setstr2(int newValue){
str2=newValue;
}
publicvoid setdex2(int newValue){
dex2=newValue;
}
public Monster (){
health2 = str2*dex2;
str2 = 15;
dex2 = 15;
}
}
publicclass Spelrutanextends JPanel
implements ActionListener{
protected JTextArea textArea;
protected String newline ="\n";
staticfinalprivate String PREVIOUS ="Rest";
staticfinalprivate String UP ="North";
staticfinalprivate String NEXT ="East";
staticfinalprivate String SOMETHING_ELSE ="prata";
staticfinalprivate String TEXT_ENTERED ="fusk";
staticfinalprivate String EXIT ="slut";
public Spelrutan(){
super(new BorderLayout());
JToolBar toolBar =new JToolBar("Still draggable");
addButtons(toolBar);
toolBar.setFloatable(false);
toolBar.setRollover(true);
textArea =new JTextArea(5, 30);
textArea.setEditable(false);
JScrollPane scrollPane =new JScrollPane(textArea);
setPreferredSize(new Dimension(450, 130));
add(toolBar, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
protectedvoid addButtons(JToolBar toolBar){
JButton button =null;
button = makeNavigationButton("Back24", PREVIOUS,
"Head back and rest",
"Rest");
toolBar.add(button);
button = makeNavigationButton("Up24", UP,
"To adventure, And beyond!",
"North");
toolBar.add(button);
button = makeNavigationButton("Forward24", NEXT,
"Go east, you bum!",
"Eastwards");
toolBar.addSeparator();
button =new JButton("Say it!");
button.setActionCommand(SOMETHING_ELSE);
button.setToolTipText("S鋑 vad ditt hj鋜ta k鋘ner f鰎");
button.addActionListener(this);
toolBar.add(button);
button =new JButton("Slut");
button.setActionCommand(EXIT);
button.setToolTipText("Ska du redan sluta?");
button.addActionListener(this);
toolBar.add(button);
JTextField textField =new JTextField("");
textField.setColumns(10);
textField.addActionListener(this);
textField.setActionCommand(TEXT_ENTERED);
toolBar.add(textField);
}
protected JButton makeNavigationButton(String imageName,
String actionCommand,
String toolTipText,
String altText){
String imgLocation ="toolbarButtonGraphics/navigation/"
+ imageName
+".gif";
URL imageURL = Spelrutan.class.getResource(imgLocation);
JButton button =new JButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
button.addActionListener(this);
if (imageURL !=null){
button.setIcon(new ImageIcon(imageURL, altText));
}else{
button.setText(altText);
}
return button;
}
publicvoid actionPerformed(ActionEvent e){
/*
Object o=e.getSource();
if(e = north)
{
}
*/
player p1=new player();
String cmd = e.getActionCommand();
String description =null;
if (PREVIOUS.equals(cmd)){
description ="You rest. Health restored!";
Monster random=new Monster ();
}elseif (UP.equals(cmd)){
Monster bajs=new Monster();
description ="You head north, and you get ambushed by a monster!";
if (p1.str>bajs.str2){
p1.killcount=p1.killcount++;
description ="You head north, and you get ambushed by a monster! After a long battle,You win! Congrats! " + p1.killcount +" monsters down! :D";
}
elseif(p1.str==bajs.str2){
description="You head north, and you get ambushed by a monster! You and the monster are equal in power. You stall eachother, the monster get's bored and walks away";
}
elseif(p1.str<bajs.str2){
p1.health=p1.health--;
description="You head north, and you get ambushed by a monster! The monster overpowers you and spits in your eye! You flee backwards, defeated and humiliated";
}
}elseif (NEXT.equals(cmd)){
if (p1.killcount><1){
description ="You've defeated all the bastards! good work! Game over then!";
}
elseif (p1.killcount>=1){
description ="You head eastwards.";
}
}
elseif (SOMETHING_ELSE.equals(cmd)){
description ="That didn't seem to do anything";
}elseif (EXIT.equals(cmd)){
system.stop(0);
}elseif (TEXT_ENTERED.equals(cmd)){
JTextField tf = (JTextField)e.getSource();
String text = tf.getText();
tf.setText("");
description ="You say: "
+ newline +"\""
+ text +"\"";
}
displayResult(""
+ description);
}
publicvoid displayResult(String actionDescription){
textArea.append(actionDescription + newline);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
privatestaticvoid createAndShowGUI(){
//Skapar f鰊stret
JFrame frame =new JFrame("The day of the rock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Skapar och s鋞ter upp.
Spelrutan newContentPane =new Spelrutan();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//Visar f鰊stret
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable());{
publicvoid run();{
createAndShowGUI();
}
};
}
}
the error that I'm getting is at the end of the code. I'd appriciate any help I could get!
[17975 byte] By [
Helirosa] at [2007-11-26 15:49:34]

Hej hopp.
Well, thanks to you it did compile just fine. But now i've run into a different problem alltogheter. Now none of the buttons show up, at all, and I can't for the world figure out what the problem is. ;^.^
Here's the code now.
package Rollspel;
import javax.swing.JToolBar;//en hel massa importeringar ;^.^
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.net.URL;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class player { //klassen player. Hurra! hurra! En hj鋖te h鋜 tr鋎a.
int level;
int xp;
int Class;
int str;
int Int;
int dex;
int health;
int maxhealth;
int killcount;
int rekna;
public player(int tmp_level, int tmp_xp, int tmp_Class, int tmp_str, int tmp_Int, int tmp_dex, int tmp_health, int tmp_maxhealth, int tmp_killcount, int tmp_rekna) { //en default konstruktor eller n錿t. Jag vet inte riktigt
level=tmp_level; //jag har med det h鋜 f鰎 att jag 鋜 s?van cid c++. ;^.^
xp=tmp_xp;
Class=tmp_Class;
str=tmp_str;
Int=tmp_Int;
dex=tmp_dex;
health=tmp_health;
killcount=tmp_killcount;
rekna=tmp_rekna;
}
public void setlevel(int newValue){
level = newValue;
}
public void setxp(int newValue){
xp=newValue;
}
public void setClass(int newValue){
Class=newValue;
}
public void setstr(int newValue){
str=newValue;
}
public void setInt(int newValue){
Int=newValue;
}
public void setdex(int newValue){
dex=newValue;
}
public void sethealth(int newValue){
health=newValue;
}
public void setkillcount(int newValue){
killcount=newValue;
}
public void setrekna(int newValue){
rekna=newValue;
}
public player(){ //en liten konstruktor eller n錿t. S鋞ter basv鋜dena f鰎 klassen player om man inte specificerar n錱onting annat.
level=1;//Jag t鋘kte g鰎a n錱on form av karakt鋜s skapare innan man b鰎jar spelet, men det blev f鰎 advancerat.
xp=0;
Class=3;
str=20;
Int=30;
dex=20;
health=str*dex/2;
maxhealth=str*dex/2;
killcount=0;
rekna=0;
if (xp==10) { //h鋜 vill jag allts?att classen player ska bli b鋞tre,
level++;//Om r鋞t antal xp har sammlats ihopp.
str=str*3/2;
dex=dex*3/2;
System.out.println( "Congrats! Lvl 2" );
}
if (xp==20) {//h鋜 med
level++;
str=str*3/2;
dex=dex*3/2;
}
}
}
class Random {//h鋜 鋜 ett tamt f鰎s鰇 till att slumpa fram siffror till monster klassens integers
int[] nisse = new int[] { 4, 5, 10, 12, 14 };
int niss = nisse[new java.util.Random().nextInt(nisse.length)];
}
class Monster { //h鋜 鋜 klassen monster. Hans tv?integers, str2 och dex2, som ska slumpas varje g錸g han kallas p?
int health2;
int str2;
int dex2;
public Monster(int tmp_health2, int tmp_str2, int tmp_dex2) {
health2=tmp_health2;
str2=tmp_str2;
dex2=tmp_dex2;
}
public void sethealth2(int newValue){
health2=newValue;
}
public void setstr2(int newValue){
str2=newValue;
}
public void setdex2(int newValue){
dex2=newValue;
}
public Monster () {
health2 = str2*dex2;
str2 = 15; //dessa tv?allts?/font>
dex2 = 15;
}
}
public class Spelrutan extends JPanel //h鋜 鋜 sj鋖va panelen allts?;D
implements ActionListener {
protected JTextArea textArea;
protected String newline = "\n";
static final private String PREVIOUS = "Rest";
static final private String UP = "North";
static final private String NEXT = "East";
static final private String SOMETHING_ELSE = "prata";
static final private String TEXT_ENTERED = "fusk";
static final private String EXIT = "Rols royce";
public Spelrutan() {
super(new BorderLayout());
JToolBar toolBar = new JToolBar("Still draggable");
addButtons(toolBar);
toolBar.setFloatable(false);
toolBar.setRollover(true);
textArea = new JTextArea(5, 30);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
setPreferredSize(new Dimension(450, 330));
add(toolBar, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
protected void addButtons(JToolBar toolBar) {
JButton button = null;
button = makeNavigationButton("Back24", PREVIOUS, //knapp
"Head back and rest",
"Rest");
toolBar.add(button);
button = makeNavigationButton("Up24", UP, // knapp
"To adventure, And beyond!",
"North");
toolBar.add(button);
button = makeNavigationButton("Forward24", NEXT,//knapp
"Go east, you bum!",
"Eastwards");
toolBar.add(button);
toolBar.addSeparator();
button = new JButton("Say it!"); //knapp
button.setActionCommand(SOMETHING_ELSE);
button.setToolTipText("S鋑 vad ditt hj鋜ta k鋘ner f鰎");
button.addActionListener(this);
toolBar.add(button);
button = new JButton("La Fini"); //knapp
button.setActionCommand(EXIT);
button.setToolTipText("Ska du redan sluta?");
button.addActionListener(this);
toolBar.add(button);
JTextField textField = new JTextField("");//Textf鋖lt
textField.setColumns(10);
textField.addActionListener(this);
textField.setActionCommand(TEXT_ENTERED);
toolBar.add(textField);
}
protected JButton makeNavigationButton(String imageName,
String actionCommand,
String toolTipText,
String altText) {
String imgLocation = "toolbarButtonGraphics/navigation/"
+ imageName
+ ".gif";
URL imageURL = Spelrutan.class.getResource(imgLocation);
JButton button = new JButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
button.addActionListener(this);
if (imageURL != null) {
button.setIcon(new ImageIcon(imageURL, altText));
} else {
button.setText(altText);
}
return button;
}
public void actionPerformed(ActionEvent e) { //en action lyssnare. Fungerar genom en if-else satts. Eller n鋝tan.
/*
Object o=e.getSource();
if(e = north)
{
}
*/
player p1=new player(); //H鋜 kallar vi p?klassen player, och skapar en ny s錮an.
p1.rekna=p1.killcount;
String cmd = e.getActionCommand();
String description = null;
if (PREVIOUS.equals(cmd)) { //N鋜 den h鋜 knappen anv鋘ds s?錿erst鋖ls integern health till ursprungsv鋜de, maxhealth.
description = "You rest. Health restored!";
p1.health=p1.maxhealth;
} else if (UP.equals(cmd)) { //N鋜 den h鋜 knappen anv鋘ds s?kallar man p?klassen Monster och g鰎 en ny instans av den.
//Jag kallar denna f鰎 bajs, f鰎 comic-relief. Klassens tv?integers skall allts?slumpas. Gah.
Monster bajs=new Monster();
p1.killcount=p1.rekna;
description = "You head north, and you get ambushed by a monster!";
if (p1.str>bajs.str2) { //Om player classens str 鋜 st鰎re 鋘 monster klassens str2 s?vinner player.
p1.killcount=p1.rekna +1; //Player klassens killcount g錼 upp med ett.
description = "You head north, and you get ambushed by a monster! After a long battle,You win! Congrats! " + p1.killcount + " monsters down! :D";
}
else if(p1.str==bajs.str2){ //Om player klassens str och Monster klassens str2 鋜 lika stora s?h鋘der ingenting.
description="You head north, and you get ambushed by a monster! You and the monster are equal in power. You stall eachother, the monster get's bored and walks away";
}
else if(p1.str<bajs.str2){ //Om player klassens str 鋜 mindre 鋘 monster klassens str2 s?g錼 playerklassens integer "health" ned med ett
p1.health=p1.health--;
description="You head north, and you get ambushed by a monster! The monster overpowers you and spits in your eye! You flee backwards, defeated and humiliated";
p1.rekna=p1.killcount;}
else if (NEXT.equals(cmd)) { //Om den h鋜 knappen anv鋘ds innan player klassens killcount 鋜 under tio s?h鋘der ingenting.
if (p1.killcount>9) { //Men om den 鋜 10 eller mer s?vinner man.
description = "You've defeated all the bastards! good work! Game over then!";
}
else if (p1.killcount<=1) {
description = "You head eastwards.";
}
}
else if (SOMETHING_ELSE.equals(cmd)) { //Jag vet inte vad jag har den h鋜 knappen till. F鰎s鰇te f?den att fungera som enter
description = "That didn't seem to do anything";// till textf鋖tet, men det gick ju inte.
}else if (EXIT.equals(cmd)) { //H鋜 st鋘gs programmet ned.
System.exit(0);
} else if (TEXT_ENTERED.equals(cmd)) { //H鋜 skriver man in n錱onting i textf鋖tet.
JTextField tf = (JTextField)e.getSource(); //Jag t鋘ker mig att om man skriver in r鋞t sak
String text = tf.getText();//s?h鋘der det n錱ot. Som en liten easter egg.
tf.setText("");//Men det f錼 bli senare, om jag orkar ;D
description = "You say: "
+ newline +"\""
+ text + "\"";
}
displayResult(""
+ description);
}
}
public void displayResult(String actionDescription) {
textArea.append(actionDescription + newline);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
private static void createAndShowGUI() {
//Skapar f鰊stret
JFrame frame = new JFrame("The day of the rock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Skapar och s鋞ter upp.
Spelrutan newContentPane = new Spelrutan();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//Visar f鰊stret
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) { //Main argumentet.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
That's that. Any help is appriciated, or a nudge in the right direction will do aswell. Thank you.
oh, and for the record. I have no idea why i can't log onto my account <.<