Home > Archive > Java Help > February 2006 > using height and width to make random images display in viewable area and not working
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 |
using height and width to make random images display in viewable area and not working
|
|
| andrewtitus@alltel.net 2006-02-22, 7:01 pm |
| I am trying to use width and heigh to make my stars display in viewable area
based on the size of my display area. The program draws the stars in random
locations but not within view. Is there a way to fix this?
Thank You,
Andy
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomStarPanel extends JPanel
{
final int STARS = 10;
int[] xPoints = new int[STARS];
int[] yPoints = new int[STARS];
//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,400));
// RandomNumberGenerator();
} //end RandomStarpanel()
public void RandomNumberGenerator()
{
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
int width = getSize().width;
int height= getSize().height;
Random generator = new Random();
for(int i = 0; i < STARS; i++){
xPoints[i] = generator.nextInt(width); // should the xPoints and
yPoints be within the
} // dimension of display area if
so why is it
//displaying outside area.
for(int j = 0; j < STARS; j++){
yPoints[j] = generator.nextInt(height);
}
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
| |
| opalpa@gmail.com opalinski from opalpaweb 2006-02-22, 9:57 pm |
| Put up code with a main and I'll give this problem a go. I'm thinking
two things:
1) fillPolygon has starts at some point and that might mean your stars
get croped. That is instead of picking between [0,width] and [0,
heigiht] you might need to pick between [starwidth,width-startwidth]
and [startheight,height-starheight]
2) Instead of calling translate you can translate star polygon and draw
that.
Good luck, have fun,
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
| |
| andrewtitus@alltel.net 2006-02-22, 9:57 pm |
| Here are the two files Iam using.
import javax.swing.JFrame;
public class RandomStars
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("Random Star");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new RandomStarPanel());
frame.pack();
frame.setVisible(true);
}
}
****************************************
**
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomStarPanel extends JPanel
{
final int STARS = 10;
int[] xPoints = new int[STARS];
int[] yPoints = new int[STARS];
//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,400));
// RandomNumberGenerator();
} //end RandomStarpanel()
public void RandomNumberGenerator()
{
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
int width = getSize().width;
int height= getSize().height;
Random generator = new Random();
for(int i = 0; i < STARS; i++){
xPoints[i] = (generator.nextInt(width)/2)+10; // should the xPoints and
yPoints be within the dimension of display area
} // if so why is it displaying
outside area.
for(int j = 0; j < STARS; j++){
yPoints[j] = (generator.nextInt(height)/ 2)-40;
}
page.setColor(Color.white);
for (int xNumb = 0; xNumb < xPoints.length; xNumb++){
for (int yNumb = 0; yNumb < yPoints.length; yNumb++){
page.translate(xPoints[xNumb/2],yPoints[yNumb/2]);
page.fillPolygon(star);
}
}
} //end paintComponent()
} // end RandomStarPanel
| |
| opalpa@gmail.com opalinski from opalpaweb 2006-02-23, 3:57 am |
| OK.
I see things.
Here are things I see:
1) instead of printing STARS number of stars you print
(xPoints.length*yPoints.length) number of stars. You loop across all
xs and all ys instead of one for loop going through numbers upto number
defined as STARS.
2) translate might have a cumulative effect. That is if you translate
once and translate again, you translated twice and then you translate
more and more towards postive x and postive y.
3) After componsating for above two all the stars end up in top left
because you've changed random generator to to divide by half and
subtract. So remove the division.
4) Finally, to eliminate croping, I would limit the range of xPoints
and yPoints from 0 to width and 0 to height to something that is inside
that range by the width and height of a star.
All together I get fairly satisfying results with:
package experiment;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomStars {
public static void main(String[] args) {
JFrame frame = new JFrame ("Random Star");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new RandomStarPanel());
frame.pack();
frame.setVisible(true);
}
}
class RandomStarPanel extends JPanel {
final int STARS = 10;
int[] xPoints = new int[STARS];
int[] yPoints = new int[STARS];
//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,400));
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
int width = getSize().width;
int height= getSize().height;
System.out.println(""+width+" "+height);
Random generator = new Random();
for(int i = 0; i < STARS; i++){
xPoints[i] = (generator.nextInt(width - 50) ) + 25;
}
for(int j = 0; j < STARS; j++){
yPoints[j] = (generator.nextInt(height - 50)) + 25;
}
page.setColor(Color.white);
for (int i=0; i<STARS; i++) {
page.translate(xPoints[i],yPoints[i]);
page.fillPolygon(star);
page.translate(-xPoints[i],-yPoints[i]);
}
}
}
The random distribution varies, all stars appear.
All the best,
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
| |
| Roedy Green 2006-02-23, 3:57 am |
| On Wed, 22 Feb 2006 22:55:18 GMT, andrewtitus@alltel.net wrote, quoted
or indirectly quoted someone who said :
> setPreferredSize (new Dimension(400,400));
Check in your paint component that the LayoutManager respected your
wishes by displaying width and height.
I don't know if you intended this , but your stars will move every
time paintComponent gets called. PaintComponent gets called often just
a slice at a time.
If you wanted them to sit still like real stars, you would need to
generate their positions once in your constructor then reference that
array pair in paintcomponent.
If you want to get very fancy, you can sort your list and use a binary
search so you don't bother painting stars outside the clip region.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
|
|
|