I am working on a lab and i have to create animation, and right now i am just creating the shapes I need and make sure they are in random locations and within the frame of 1000*700.
This lab also uses inheritance and so there are a few classes, but the class which creates the random one i have here :
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import java.util.ArrayList;
public class ShapeComponent extends JComponent
{
private int instances = 40;
final static double MaxX = 1000;
final static double MaxY = 700;
final static double MinR = 10;
final static double MaxR = 30;
ArrayList<Shape3> shape3 = new ArrayList<Shape3>();
public ShapeComponent(){
int i;
int value = 2;
for(i=0;i<instances;i++){
double radius = (double) (Math.random() * (MaxR - MinR) + MinR);
System.out.println(radius);
double x = (double)(Math.random() * (MaxX - 4*radius) + 2*radius);
System.out.println(x);
double y = (double)(Math.random() * (MaxY - 4*radius) + 2*radius);
System.out.println(y);
System.out.println();
shape3.add(new Shape3(x,y,radius));
}
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int i ;
for(i=0;i<instances;i++){
Shape3 shape = shape3.get(i);
shape.draw(g2);
}
}
}
So I used the print line to see the x, y coordinates and they all look within the range. since the width is 1000 , the math.random creates it between 60-940 and the height between 60-640. I have no idea why it is coming off as wrong, but it appears like a few of the objects are off the frame by the slightest of margins. Can anyone help?
Thank you Kindly
Aucun commentaire:
Enregistrer un commentaire