Applet, use seperate class  
Author Message
michael_jd





PostPosted: 2006-12-11 10:52:00 Top

java-programmer, Applet, use seperate class Hi I am making a applet that uses a seperate class, but my IDE tells me
that when I try to use my seperate class, that it cannot resolve the
symbol (ie cant find it) ,or if I try to import the class, that it
expects a '.'. Here's the code

import java.awt.*;

public class Buiding
{
private int width, height, topBuffer, sideBuffer, widthOfWindow,
heightOfWindow,
heightOfThird;

public Buiding(int width, int height)
{
this.width = width; this.height = height;
topBuffer = height/20;
sideBuffer = width/20;
widthOfWindow = width/10;
heightOfWindow = height/10;
heightOfThird = height/3;
}

public void draw(int x, int y, Graphics page)
{
int origionalX = x, origionalY = y;
page.drawRect(x, y, width, height);

int randomXWidth = x - widthOfWindow;
int randomX = (int)(Math.random()*randomXWidth);

int randomYWidth = y + heightOfThird - 2*topBuffer -
heightOfWindow;
int randomY = (int)(Math.random()*randomYWidth);

randomX += x;
randomY += y;

page.fillRect(randomX,randomY,widthOfWindow,heightOfWindow);
}
}

import java.applet.Applet;
import java.awt.*;
import Building; //stops here

public class test extends Applet
{
private Building test; // or here if the above line is not there

public void init()
{
test = new Building(100,100);
}

public void paint(Graphics g)
{
test.draw(10,10,g);
}
}

Thanks for any help

 
Andrew Thompson





PostPosted: 2006-12-11 11:15:00 Top

java-programmer >> Applet, use seperate class michael_jd wrote:
> Hi I am making a applet that uses a seperate class, but my IDE tells me
> that when I try to use my seperate class, that it cannot resolve the
> symbol (ie cant find it) ,or if I try to import the class, that it
> expects a '.'. Here's the code

The basic problem here is hat these classes are in the
'default' package. And classes in the default package
cannot be 'import'ed.
...
> import java.applet.Applet;
> import java.awt.*;
> import Building; //stops here
>
> public class test extends Applet

This class (also being in the default package)
may not need to 'import' the Building class at all,
*however*, it is probably best to put these classes
into a package and proceed from there.

Here is a good document that goes into how to
compile and run classes from the both the
default package, as well as other packages..
<http://rabbitbrush.frazmtn.com/classpath.html>

Andrew T.

 
Thomas Hawtin





PostPosted: 2006-12-11 21:27:00 Top

java-programmer >> Applet, use seperate class michael_jd wrote:
>
> public class Buiding
^^ Typo...

> public class test extends Applet
> {
^ Should be capitalised.
> private Building test; // or here if the above line is not there

Is test a class or a field? I'm confused.

But as Andrew says, it's a good idea to get into the habit of using
(non-default) packages.

Tom Hawtin
 
 
michael_jd





PostPosted: 2006-12-12 14:47:00 Top

java-programmer >> Applet, use seperate class
Thomas Hawtin wrote:
> michael_jd wrote:
> >
> > public class Buiding
> ^^ Typo...
>

Fixed. Thanks for the help!