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!

Problems with classes and the getParameter() method... 1

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
I am writing a simple Applet that creates a starfield of 1000 stars (which I have working right), and then takes an unknown number of parameters and makes buildings that are of the height specified in the Applet tag of the HTML file. Then I need to compute how wide to make these buildings so that they will all fit on the canvas using the getWidth() and getHeight() methods and by cumputing the number of parameter tags in the Applet tag in the HTML file. Here is my code so far:

Code:
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;

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

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 drawBuildings(Graphics2D pen, int Width, int Height) {
	}
}

Thanks for helping! X-) :-Q &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
 
if you're getting a compile error, please include it. if you're getting a runtime error... please include that as well. If I'm sure what the problem is, it's easier to search for a solution. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
I've had no errors, I haven't tried to do it yet. I have no idea how to start off.

The biggest thing is finding a way to tell how many building heights are listed in the parameters of the Applet tag. I think that I can then do the rest by myself. &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
 
the applet tag must supply parameters that are already known by the Applet, so you can't dynamically discover the tags that are sent. I would suggest creating such parameters as &quot;num_buildings&quot; and &quot;building_height&quot;. As I don't use applets often enough, I'm not sure if you can dynamically discover the width of the applet. If not, I would have it passed in as a parameter (or perhaps use a standard fixed width).<br><br>To take a string and turn it into an integer, you want to use the Integer.parseInt() method.<br><br>I hope that's a solid beginning... if not, let me know and I'll see what I can add. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
You can have the program find the width and height of the Applet by using the getWidth() and getHeight() methods. Now I just need to find a way to get it to determine how many parameter tags are in the Applet tag. My teacher said something about getting a null pointer and using it to determine the last parameter. :-Q

This is a hard one, I wish wushutwist or pipk would help me. I'll bet they know how to do this. 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
 
I must be awfully offensive if no one will help me. I didn't think that I was that bad. &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
 
Like I said, I don't think there are any ways to determine this. The only thing I can think of is parsing the text from the URL, but that's kind of stupid. The more common way is to have the applet decide what parameters it will accept, rather than to have the applet figure out what parameters it has been given. I have no idea what your prof is referring to in this example. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Hi,

Well, if you don't know the number of the parameters beforehand, you could use naming like param1, param2, param3.... etc.

Then you could use loop like e.g. this:

int i=1;
String param = null;
while((param = ap.getParameter(&quot;param&quot;+i)) != null)
{
//...do something....
i++;
}

Example above will get parameters named param1 ... paramN

So, some solution like this might work?

-Vepo
 
I'll try it, and I fofgot to mention that every parameter will have a name &quot;building&quot; followed by a number. The numbers will go from 1 up. &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
 
OK, I tried it and it worked. Now I need help drawing the buildings. Here is my updated code:

Code:
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;

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

		int i = 1;
		String param = null;

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

		int i2 = 1;
		int runs = 1;
		int startX = 0;
		int startY = getHeight();
		int height = 0;
		int width = getWidth() / (i + 2);
		String input = &quot;&quot;;

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

			height = startY - height;

			Building one = new Building();
			one.drawBuilding(pen, height, width, startX, startY);

			startY = startY + width;
			i2++;
			runs++;
		}
	}
}

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) {
		Random generator = new Random();

		Rectangle building = new Rectangle(startX, startY, height, width);
		int red = generator.nextInt(100),
			green = generator.nextInt(100),
			blue = generator.nextInt(100);
		pen.setColor(new Color(red, green, blue));
		pen.fill(building);
	}
}

Thanks for the help. &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
 
but how will you know what parameters are what... now you have param1 = 50, param2 = 35, and you don't know which is which and what you need. You'd have to put the parameter names in the parameter values (like param1 = &quot;width:50&quot;, param2 = &quot;height:35&quot;), and then you'd have to parse these, and in any case you'd still need to know the names of all your parameters beforehand, so I don't know what that gains you. Unless you have a specified order, in which case you still know what you're getting... so why not ask for it. Oh well. I can't see much sense in it, but if that's what you need to do for school, then that's what you need to do. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top