Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getParameter() method... 1

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
Can I use the getParameter() method in a class? "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Uhm... odd question. Why wouldn't you be able to? If you create your own class and give it a getParameter() method call there won't be a problem. Not sure that's the actual question, though. Is it? Here's an idea, instead of waiting around for someone to answer the question why don't you fire up your IDE (or whatever) and create a test application with a class that contains the method getParameters()? That would pretty much provide the answer you're looking for, now won't it?
 
I already tried it, and it didn't work. I need to know if there's something special I need to do to get it to work. "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
From the Applet tag of the HTML file. "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
You can't implement getParameter in a class if it isn't an applet (i.e. extends Applet) and expect to get a parameter from an HTML page unless you provide some hooks somewhere. You would normally have this class use a callback reference to the applet that ultimately spawned it and use the applet to call its getParameter method. For example

public class MyApplet extends Applet
{

public void foo()
{
// Create an instance of the class
MyClass mc = new MyClass(this);

// Do something
mc.bar()
}
...
}

public class MyClass
{
private Applet callback;
public MyClass(Applet a)
{
callback = a;
}

public void bar()
{
String str = callback.getParameter("foobar");
// Do something with string
}
}

Hope this helps,

Charles
 
Here is my code, maybe you can find what I am doing wrong...

import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class Skyline extends Applet {

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

public void paint(Graphics g) {
Graphics2D pen = (Graphics2D) g;
Random generator = new Random();

Stars starField = new Stars();
starField.drawStars(pen, getWidth(), getHeight());

int i = 1;
String param = null;

while((param = getParameter("param" + i)) != null) {
i++;
}

int startX = 0;
int startY = getHeight();
int height = 0;
int width = getWidth() / (i + 2);

Building structure = new Building();
structure.drawBuilding(pen, height, width, startX, startY, i);
}
}

class Stars {

public void drawStars(Graphics2D pen, int Width, int Height) {
Random generator = new Random();

int runs = 1;

while (runs <= 1000) {
int Xcord = generator.nextInt(Width - 3);
int Ycord = generator.nextInt(Height - 3);

Ellipse2D.Double star
= new Ellipse2D.Double(Xcord, Ycord, 3, 3);

pen.setColor(Color.white);
pen.draw(star);

runs++;
}
}
}

class Building {

public void drawBuilding(Graphics2D pen, int height, int width, int startX, int startY, int i) {
Random generator = new Random();

int i2 = 1;
int runs = 1;
String input = &quot;&quot;;

int red = generator.nextInt(100),
green = generator.nextInt(100),
blue = generator.nextInt(100);

pen.setColor(new Color(red, green, blue));

while (runs <= i) {
input = getParameter(&quot;building&quot; + i2);
height = Integer.parseInt(input);

height = startY - height;

Rectangle building = new Rectangle(startX, startY, height, width);
pen.fill(building);

startY = startY + width;
i2++;
runs++;
}
}
} &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
As I said above, Building does not have a getParameter method (you don't define one and it doesn't inherit one) so this won't work. One alternative to the strategy I outlined above is to make Building an inner class of your applet (i.e. enclose the Building class within the curly braces of the Applet class. This will give the building class access to the methods of it's enclosing class.
 
Thanks! It compiles! X-) &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top