# java math.random with images

Page 1 of 1

## 1 Replies - 5988 Views - Last Post: 12 October 2009 - 02:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=131613&amp;s=ff9c000500738b325575352bc7ff5eee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 beachsoccerball3

Reputation: 0
• Posts: 1
• Joined: 12-October 09

# java math.random with images

Posted 12 October 2009 - 01:52 PM

I am working on a lab for Comp. Sci. and we have to basically create 4 different desired images within a grid our teacher gave us. The top left one is 100 random lines, the top right one is 100 random squares with diameters of 50 pixels, the bottom right one is using polygons to create a cube, and the bottom left one is to create 100 random circles with varying diameters of 0 to 200. None of the images can go outside of the boxes he designated for them. I have everything but the circles done, but I can't figure them out. So far this is my code
```
import java.awt.*;
import java.applet.*;
import java.util.*;

public class lab6 extends Applet
{

public void paint(Graphics g)
{
// Draw Grid
g.drawRect(10,10,780,580);
g.drawLine(400,10,400,590);
g.drawLine(10,300,790,300);

Random rnd = new Random(1234);

// Draw Random Lines
for(int a=0; a<100; a++)
{
int x1 = (int)(Math.random()*390) + 10;
int y1 = (int)(Math.random()*290) + 10;
int x2 = (int)(Math.random()*390) + 10;
int y2 = (int)(Math.random()*290) + 10;
int red = (int)(Math.random()*256);
int green = (int)(Math.random()*256);
int blue = (int)(Math.random()*256);
g.setColor(new Color(red,green,blue));
g.drawLine(x1,y1,x2,y2);

int x3 = (int)(Math.random()*340) + 400;
int y3 = (int)(Math.random()*240) + 10;
g.setColor(new Color(red,green,blue));
g.fillRect(x3,y3,50,50);

int x4 = (int)(Math.random()*200) + 10;
int y4 = (int)(Math.random()*100) + 300;
int d = (int)(Math.random()*200);
g.setColor(new Color(red,green,blue));
g.drawOval(x4,y4,d,d);

g.setColor(Color.red);
Polygon r = new Polygon();
g.fillPolygon(r);
g.setColor(Color.green);
Polygon gr = new Polygon();
g.fillPolygon(gr);
g.setColor(Color.blue);
Polygon bl = new Polygon();
g.fillPolygon(bl);
g.setColor(Color.yellow);
Polygon yellow = new Polygon();
g.fillPolygon(yellow);
}

}

}

```

I just put the rest of the rectangles in there even though they are finished so it doesn't seem confusing why I need the circles to go into the bottom left rectangle. The part with the x4 and y4 is the circle part. Please help!!!!

This post has been edited by beachsoccerball3: 12 October 2009 - 01:54 PM

Is This A Good Question/Topic? 0

## Replies To: java math.random with images

### #2 payamrastogi

Reputation: 3
• Posts: 31
• Joined: 30-July 08

## Re: java math.random with images

Posted 12 October 2009 - 02:21 PM

as far as the code is concerned it is working fine ...(i used the applet size 800 X 800)

beachsoccerball3, on 12 Oct, 2009 - 12:52 PM, said:

I am working on a lab for Comp. Sci. and we have to basically create 4 different desired images within a grid our teacher gave us. The top left one is 100 random lines, the top right one is 100 random squares with diameters of 50 pixels, the bottom right one is using polygons to create a cube, and the bottom left one is to create 100 random circles with varying diameters of 0 to 200. None of the images can go outside of the boxes he designated for them. I have everything but the circles done, but I can't figure them out. So far this is my code
```
import java.awt.*;
import java.applet.*;
import java.util.*;

public class lab6 extends Applet
{

public void paint(Graphics g)
{
// Draw Grid
g.drawRect(10,10,780,580);
g.drawLine(400,10,400,590);
g.drawLine(10,300,790,300);

Random rnd = new Random(1234);

// Draw Random Lines
for(int a=0; a<100; a++)
{
int x1 = (int)(Math.random()*390) + 10;
int y1 = (int)(Math.random()*290) + 10;
int x2 = (int)(Math.random()*390) + 10;
int y2 = (int)(Math.random()*290) + 10;
int red = (int)(Math.random()*256);
int green = (int)(Math.random()*256);
int blue = (int)(Math.random()*256);
g.setColor(new Color(red,green,blue));
g.drawLine(x1,y1,x2,y2);

int x3 = (int)(Math.random()*340) + 400;
int y3 = (int)(Math.random()*240) + 10;
g.setColor(new Color(red,green,blue));
g.fillRect(x3,y3,50,50);

int x4 = (int)(Math.random()*200) + 10;
int y4 = (int)(Math.random()*100) + 300;
int d = (int)(Math.random()*200);
g.setColor(new Color(red,green,blue));
g.drawOval(x4,y4,d,d);

g.setColor(Color.red);
Polygon r = new Polygon();
g.fillPolygon(r);
g.setColor(Color.green);
Polygon gr = new Polygon();
g.fillPolygon(gr);
g.setColor(Color.blue);
Polygon bl = new Polygon();
g.fillPolygon(bl);
g.setColor(Color.yellow);
Polygon yellow = new Polygon();
g.fillPolygon(yellow);
}

}

}

```

I just put the rest of the rectangles in there even though they are finished so it doesn't seem confusing why I need the circles to go into the bottom left rectangle. The part with the x4 and y4 is the circle part. Please help!!!!