6 Replies - 5680 Views - Last Post: 03 January 2009 - 02:16 PM Rate Topic: -----

#1 srp10215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 09

A problem combining nested loops, graphics, and math.random()

Posted 02 January 2009 - 01:44 PM

Use nested loops, Graphics, and Math.random() to print out the square pattern show below.

Posted Image

This image changes every X milliseconds. A timer is redrawing all of the squares.

This is the code I have so far:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Canvas;
import javax.swing.JPanel;

public class RandomColoredBoxes extends JPanel
{
	private Timer timer;
	private final static int SLEEP = 110;
	
	public RandomColoredBoxes()
	{		
		setBackground(Color.BLACK);
		setVisible(true);

		ActionListener paintCaller = new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{
				repaint();  
			}
		};
		timer = new Timer(SLEEP, paintCaller);
		timer.start();
	}	

	public void paintComponent( Graphics window )
	{
		super.paintComponent(window);
		
		window.setColor(Color.RED);
		window.setFont(new Font("TAHOMA",Font.BOLD,12));
		  window.drawString("Graphics Lab Lab11k ", 20, 40);
		  window.drawString("Drawing boxes with nested loops ", 20, 80);

		  drawBoxes(window);
	}

	public void drawBoxes(Graphics window)
	{
		//for loop to to across the x - getWidth() might be useful
		
			//for loop to go down the y - getHeight() might be useful
			
				//draw random colored boxes
	}
}



This is the code for the Graphics Runner:
import javax.swing.JFrame;

public class GraphicsRunner extends JFrame
{
	private static final int WIDTH = 800;
	private static final int HEIGHT = 600;

	public GraphicsRunner()
	{
		super("Graphics Runner");
		setSize(WIDTH,HEIGHT);
						
		//getContentPane().add(new ColoredBoxes());					
						
		getContentPane().add(new RandomColoredBoxes());
				
		setVisible(true);
	}
	
	public static void main( String args[] )
	{
		GraphicsRunner run = new GraphicsRunner();
	}
}



Please help! I have no idea how to proceed.

Is This A Good Question/Topic? 0
  • +

Replies To: A problem combining nested loops, graphics, and math.random()

#2 srp10215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 09

Re: A problem combining nested loops, graphics, and math.random()

Posted 02 January 2009 - 03:04 PM

Someone please help!!
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: A problem combining nested loops, graphics, and math.random()

Posted 02 January 2009 - 05:56 PM

Here is a completly different and easier alternative

Bui;d a JFrame with a BorderLayout
In the BorderLayout.CENTER put this ColoredPanel
which contains in a GridLayout an Array of Opaque JPanel

public class ColoredPanel extends JPanel {

	JLabel[][] label;
	Random ran;
	
	ColoredPanel(int width, int height) {
		super(new GridLayout(width, height, 1, 1));
		setBackground(Color.BLACK);
		ran = new Random();
		label = new JLabel[width][height];
		for(int i = 0; i < width; i++) {
			for(int j = 0; j < height; j++) {
				label[i][j] = new JLabel("");
				label[i][j].setOpaque(true);
				add(label[i][j]);
			}
		}
	}
	
	public void paint(Graphics g) {
		for(int i = 0; i < label.length; i++) {
			for(int j = 0; j < label[i].length; j++) {
				label[i][j].setBackground(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));
			}
		}
		super.paint(g);
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 srp10215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 09

Re: A problem combining nested loops, graphics, and math.random()

Posted 02 January 2009 - 10:09 PM

Thank you so much, but I need to keep the code in the format that was given. Help again?? Or anyone else?
Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: A problem combining nested loops, graphics, and math.random()

Posted 03 January 2009 - 07:07 AM

Check out the parts of the code that are written as commented text. These are the parts that you are supposed to write. It's much simpler than you think. A quick look at the API and you'll find the methods you need. Search google for: java Graphics
Graphics is the class and the object that you work on is named window (see below). You're supposed to draw filled rectangles in random colors. Finally, you have to remove the comment characters somewhere in the code (It's rather obvious where). That's all the hints I'm gonna give you.

public void drawBoxes(Graphics window)
	{
		//for loop to to across the x - getWidth() might be useful
		
			//for loop to go down the y - getHeight() might be useful
			
				//draw random colored boxes
	}



Start by writing the apropriate code for this section.

This post has been edited by Gloin: 03 January 2009 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 srp10215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 09

Re: A problem combining nested loops, graphics, and math.random()

Posted 03 January 2009 - 10:56 AM

I got the loops to work, but how do I generate the random colors inside the loops?
Was This Post Helpful? 0
  • +
  • -

#7 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: A problem combining nested loops, graphics, and math.random()

Posted 03 January 2009 - 02:16 PM

in reference to the color question.
this is from http://java.sun.com/.../awt/Color.html for the Color class

Constructor Summary
Color(ColorSpace cspace, float[] components, float alpha)
Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.
Color(float r, float g, float B)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).
Color(float r, float g, float b, float a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).
Color(int rgb)
Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.
Color(int rgba, boolean hasalpha)
Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.
Color(int r, int g, int B)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).
Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).


as you can see you can create your own color by simply using 3 numbers Color(float,float,float).
from this you could easily generate 3 random numbers and pass them to the constructor :D
then simply create a method called setColor(Color c) or something similar..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1