I am trying to create random rectangles and ovals. This is what i ahve so far:

import java.awt.Color;
import java.awt.Graphics;

public class MyRectangle 
{
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private Color myColor;
	
	//constructor with input values
	public MyRectangle( int x1, int y1, int x2, int y2, Color color)
	{
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		myColor = color;
	}
	
	//Draw the rectangle in the specified color
	public void draw( Graphics g)
	{
		g.setColor( myColor);
		g.drawRect(x1, y1, x2, y2);
	}
}
import java.awt.Color;
import java.awt.Graphics;

public class MyOval 
{
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private Color myColor;
	
	//constructor with input values
	public MyOval( int x1, int y1, int x2, int y2, Color color)
	{
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		myColor = color;
	}
	
	//Draw the rectangle in the specified color
	public void draw( Graphics g)
	{
		g.setColor( myColor);
		g.drawOval(x1, y1, x2, y2);
	}
}
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawPanel extends JPanel 
{
	private Random randomNumbers = new Random();
	private MyRectangle rectangle[];
	private MyOval oval[];
	
	//constructor, creates a panel with random shapes
	public DrawPanel()
	{
		setBackground( Color.WHITE);
		
		rectangle = new MyRectangle[randomNumbers.nextInt(5)];
		oval = new MyOval[randomNumbers.nextInt(5)];
		
		//create rectangles
		for (int count = 0; count < rectangle.length; count++)
		{
			//generate random coordinates
			int x1 = randomNumbers.nextInt(300);
			int y1 = randomNumbers.nextInt(300);
			int x2 = randomNumbers.nextInt(300);
			int y2 = randomNumbers.nextInt(300);
			
			//generate a random color
			Color color = new Color( randomNumbers.nextInt (256),
					randomNumbers.nextInt(256), randomNumbers.nextInt(256));
			
			//add to the list to be displayed
			rectangle[count] = new MyRectangle( x1, y1, x2, y2, color);
		}
		
		//create ovals
		for (int count = 0; count < oval.length; count++)
		{
			//generate random coordinates
			int x1 = randomNumbers.nextInt(300);
			int y1 = randomNumbers.nextInt(300);
			int x2 = randomNumbers.nextInt(300);
			int y2 = randomNumbers.nextInt(300);
			
			//generate a random color
			Color color = new Color( randomNumbers.nextInt (256),
					randomNumbers.nextInt(256), randomNumbers.nextInt(256));
			
			//add to the list to be displayed
			oval[count] = new MyOval( x1, y1, x2, y2, color);
		}
	}
	
	public void paintComonent( Graphics g)
	{
		super.paintComponent (g);
		
		for ( int count = 0; count < rectangle.length; count++)
		{
			//This is the area where i need help
		}
	}
}

in public void paintComonent( Graphics g), this is the area where i have my confussion. In the book i am reading it gives examples using lines, they use:

public void paintComonent( Graphics g)
	{
		super.paintComponent (g);
		
		//draw lines
                                for ( MyLine line: lines )
                                     line.draw( g )
	}

i tried changing for (MyLine line: lines) into for(MyReactangle rectangle : rectangles ) but that didnt work, im not too sure on what to change that section into.

public void paintComponent( Graphics g)
	{
		super.paintComponent (g);
		
		for ( int count = 0; count < rectangle.length; count++)
		{
			//This is the area where i need help
                        rectangle[count].draw (g);
		}
	}

See red above. Note the spelling mistake. Try the above and see if it works.

That was it thank you. i also another another line to print ovals as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.