[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Expecting Statement or Type

Hi java Gods!

I am getting several errors specfically an expecting statement error on the subclass message and a type identifier error on the "String.valueOf() line"

Any suggestions?

(I am new at Java)

Thanks.....

Here is the code:

import java.awt.*;
public class Spots extends java.applet.Applet  {

final int MAXSPOTS = 10;
int xspots[ ] = new int[MAXSPOTS];
int yspots[ ] = new int[MAXSPOTS];
int currspots = 0;

public void init( ) {
setBackground(Color.white);
}
public boolean mouseDown(Event evt, int x, int y) {
if (currspots < MAXSPOTS)
   addspots(x,y);
else
returnclass message;
return true;
}
void addspots(int x, int y) {
xspots[currspots] = x;
yspots[currspots] = y;
currspots++;
repaint( );
}
public void paint(Graphics g) {
g.setColor(Color.blue);
for (int i= 0; i<currspots; i++) {
g.fillOval(xspots[i] -10, yspots[i] -10,20,20);
    }
}
public class message extends Spots {
String message = new String( );
Color g = new Color(250,0,0);
set Font = (new Font("Helvetica",Font.BOLD,36));
g.drawString(String.valueOf(message),5,30);
        }
}
0
pyramid
Asked:
pyramid
  • 3
  • 2
1 Solution
 
remboCommented:

Well, I see what's wrong, but I'm not sure what you're
really trying to do.

returnclass message;

is not a valid statement.  

Then on 35, you're doing:

g.drawString(String.valueOf(message),5,30);

However, your String is empty.  

When you create a new String, you have to put
something in it.  

Also, why are you extending your applet class?  

I think you want code more like this (It works):

 import java.awt.*;
 public class Spots extends java.applet.Applet {
        final int MAXSPOTS = 10;
        int xspots[ ] = new int[MAXSPOTS];
        int yspots[ ] = new int[MAXSPOTS];
        int currspots = 0;

 public void init( ) {
        setBackground(Color.white);
 }

 public boolean mouseDown(Event evt, int x, int y) {
        if (currspots < MAXSPOTS)
                addspots(x,y);

        return true;
 }

 void addspots(int x, int y) {
        xspots[currspots] = x;
        yspots[currspots] = y;
        currspots++;
        repaint( );
 }

 public void paint(Graphics g) {
        g.setColor(Color.blue);
        for (int i= 0; i<currspots; i++) {
                g.fillOval(xspots[i] -10, yspots[i] -10,20,20);
   }
}
  }


0
 
pyramidAuthor Commented:
Hi.

I already have that part of the code working.

I am trying to put a test in the code that will give me a message in a template. When I reach over the limit of spots it will write on the applet "Too Many Spots".

any suggestions?
0
 
pyramidAuthor Commented:
Perhaps I can use import to bring new class in and activate it

any comment??
0
 
remboCommented:
Here's a new version that should do what you want:

 import java.awt.*;
 public class Spots extends java.applet.Applet {
        final int MAXSPOTS = 10;
        int xspots[ ] = new int[MAXSPOTS];
        int yspots[ ] = new int[MAXSPOTS];
        int currspots = 0;
        boolean over = false;
        Color errColor;
        Font errFont;
        String errMsg;
        int errX = 0, errY = 0;


 public void init( ) {
        setBackground(Color.white);
 }

 public boolean mouseDown(Event evt, int x, int y) {
        if (currspots < MAXSPOTS)
                addspots(x,y);
    else {
                errX = x;
                errY = y;
                error("Too many spots!");
        }

        return true;
 }

 public void error(String s)
 {
        errMsg = s;
        errColor = new Color(250,0,0);
        errFont = (new Font("Helvetica",Font.BOLD,36));
        over = true;
        repaint();
 }

 void addspots(int x, int y) {
        xspots[currspots] = x;
        yspots[currspots] = y;
        currspots++;
        repaint( );
 }

 public void paint(Graphics g) {
        g.setColor(Color.blue);
        for (int i= 0; i<currspots; i++) {
                g.fillOval(xspots[i] -10, yspots[i] -10,20,20);
    }

        if (over) {
                g.setColor(errColor);
                g.setFont(errFont);
                g.drawString(errMsg, errX, errY);
        }
 }


}


0
 
pyramidAuthor Commented:
Thank you for helping me! Your code was very enlightening. I knew there was an issue of how Java used boolean. It did confuse me.

Again

Thanks.....
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now