1 Replies - 6075 Views - Last Post: 12 October 2009 - 02:21 PM Rate Topic: -----

#1 beachsoccerball3   User is offline

  • New D.I.C Head

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

java math.random with images

Post icon  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();
			r.addPoint(600,450);
			r.addPoint(700,450);
			r.addPoint(700,550);
			r.addPoint(600,550);	
			g.fillPolygon(r);
			g.setColor(Color.green);
			Polygon gr = new Polygon();
			gr.addPoint(600,450);
			gr.addPoint(600,550);
			gr.addPoint(550,500);
			gr.addPoint(550,400);
			g.fillPolygon(gr);
			g.setColor(Color.blue);
			Polygon bl = new Polygon();
			bl.addPoint(700,450);
			bl.addPoint(650,450);
			bl.addPoint(650,400);
			g.fillPolygon(bl);
			g.setColor(Color.yellow);
			Polygon yellow = new Polygon();
			yellow.addPoint(650,400);
			yellow.addPoint(650,450);
			yellow.addPoint(600,450);
			yellow.addPoint(550,400);
			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   User is offline

  • New D.I.C Head

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

Re: java math.random with images

Posted 12 October 2009 - 02:21 PM

Hey please elaborate your problem clearly
as far as the code is concerned it is working fine ...(i used the applet size 800 X 800)

View Postbeachsoccerball3, 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();
			r.addPoint(600,450);
			r.addPoint(700,450);
			r.addPoint(700,550);
			r.addPoint(600,550);	
			g.fillPolygon(r);
			g.setColor(Color.green);
			Polygon gr = new Polygon();
			gr.addPoint(600,450);
			gr.addPoint(600,550);
			gr.addPoint(550,500);
			gr.addPoint(550,400);
			g.fillPolygon(gr);
			g.setColor(Color.blue);
			Polygon bl = new Polygon();
			bl.addPoint(700,450);
			bl.addPoint(650,450);
			bl.addPoint(650,400);
			g.fillPolygon(bl);
			g.setColor(Color.yellow);
			Polygon yellow = new Polygon();
			yellow.addPoint(650,400);
			yellow.addPoint(650,450);
			yellow.addPoint(600,450);
			yellow.addPoint(550,400);
			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!!!!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1