Home > Archive > Java Help > February 2006 > random stars draw but most out of my view
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
random stars draw but most out of my view
|
|
| andrewtitus@alltel.net 2006-02-22, 7:01 pm |
| I wrote a program that draws 10 stars at random locations and it works
except some of the stars are out of view. How can I get them to display in
400,500 size panel. I tried the width and height and when I put them in for
computing random values it compiles but gives error n must not be negative.
Exception in thread "main" java.lang.IllegalArgumentException: n must be
positive
ÏÏ§Ï at java.util.Random.nextInt(Unknown Source)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomStarPanel extends JPanel
{
final int MAXPOINTS = 10;
int width = getSize().width;
int height= getSize().height;
int[] Radius = new int[10];
int[][] Locations = new int[10][10];
int[] xPoints = new int[MAXPOINTS];
int[] yPoints = new int[MAXPOINTS];
//Star radius 25
Polygon star = new Polygon(
new int[]{0,6,24,9,15,0,-15,-9,-24,-6},
new int[]{-25,-8,-8,3,20,9,20,3,-8,-8},10);
public RandomStarPanel()
{
setBackground (Color.black);
setPreferredSize (new Dimension(400,500));
RandomNumberGenerator();
} //end RandomStarpanel()
public void RandomNumberGenerator()
{
Random generator = new Random();
for(int i = 0; i < MAXPOINTS; i++){
xPoints[i] = generator.nextInt(width); // problem
// xPoints[i] = Math.abs(xPoints[i]);
}
for(int j = 0; j < MAXPOINTS; j++){
yPoints[j] = generator.nextInt(height); // problem
// yPoints[j] = Math.abs(yPoints[j]);
}
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.white);
for (int xNumb = 0; xNumb < xPoints.length; xNumb++){
for (int yNumb = 0; yNumb < yPoints.length; yNumb++){
page. translate(xPoints[xNumb],yPoints[yNumb])
;
page.fillPolygon(star);
}
}
} //end paintComponent()
} // end RandomStarPanel
| |
| Oliver Wong 2006-02-22, 7:01 pm |
|
<andrewtitus@alltel.net> wrote in message
news:hI-dnXnEFK4A62HeRVn-rw@giganews.com...
>I wrote a program that draws 10 stars at random locations and it works
> except some of the stars are out of view. How can I get them to display in
> 400,500 size panel. I tried the width and height and when I put them in
> for
> computing random values it compiles but gives error n must not be
> negative.
> Exception in thread "main" java.lang.IllegalArgumentException: n must be
> positive
> ÏÏ§Ï at java.util.Random.nextInt(Unknown Source)
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import java.util.*;
>
> public class RandomStarPanel extends JPanel
> {
> final int MAXPOINTS = 10;
> int width = getSize().width;
> int height= getSize().height;
Note that the above two lines run as soon as the object is created. That
is, it runs before the panel even gets displayed, so it's possible that the
size is actually (-1,-1), indicating that there is no size, because the
panel is not visible yet.
> int[] Radius = new int[10];
> int[][] Locations = new int[10][10];
> int[] xPoints = new int[MAXPOINTS];
> int[] yPoints = new int[MAXPOINTS];
How come sometimes you use MAXPOINTS and other times you use 10? Does
that mean that the other 10s have a different conceptual meaning than
MAXPOINTS? I.e. that the number of Radiuses (Radii?) is not nescessarily
equal to the "maximum number of points"?
And this MAXPOINTS actualyl seems to refer to the number of stars you
want to draw, so why is it called MAXPOINT? I might otherwise think you mean
the maximum number of points a star can have (e.g. a 5-pointed star, or a 6
pointed star, etc.)
[snipped rest of code]
- Oliver
| |
| Bart Cremers 2006-02-23, 3:57 am |
|
andrewtitus@alltel.net wrote:
> I wrote a program that draws 10 stars at random locations and it works
> except some of the stars are out of view. How can I get them to display in
> 400,500 size panel.
The reason most of your stars are painted outside the view is the way
you use the translate. Translating moves the origin, so each time you
move the origin you need to recalculate the points.
Eg.
Origin at 0 - 0
x[0]=393 y[0]=261
Origin moves to 393 - 261 (relative to original origin. This point
becomes 0 - 0)
x[1]=36 y[1] =288
Origin moves to 429 - 549 (outside panel)
....
So you'll have to either recalculate your points each time you
translate the origin, or reset the origin after painting the star
(adviced).
> I tried the width and height and when I put them in for
> computing random values it compiles but gives error n must not be negative.
> Exception in thread "main" java.lang.IllegalArgumentException: n must be
> positive
At the time of construction of a JPanel, its width and height are 0.
So, you store zero in your width and height variables to use it for
random number generation. So, to solve this, call your
randomNumberGeneration() from the paint method, at which moment you are
certain the panel has a size (build in a check to make sure you don't
recalculate every time the panel is repainted).
<code snipped>
Regards,
Bart
|
|
|
|
|