Hey guys,

so this is what happened. I'm stuck on this project on the second part, which is the top right quadrant. Its not showing the graphics at its specified quadrant which is the top right. Its supposed to have randomized sized red blocks that fills the quadrant. Can you guys see what I did wrong?

/*
  RandomGraphics starting program
  
  Your heading here!	

*/
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.*; 	//for JFrame
import java.awt.*; 		//for Graphics and Container
// other import statements here


public class RandomGraphics 
{
	
	// constants are used to draw the grid, and for you to put shapes in the grid
	public static final int MIDX = 400;
	public static final int MIDY = 300;
	public static final int MAXX = 799;
	public static final int MAXY = 599;
	public static final int COLOR = 256;
    
    // make another constant for the color value that will
    // be used to generate a random color
    
    public static void main( String[] args )throws InterruptedException 
    {
    	//*** This next section sets up the graphics window.  
    	//*** You are not required to understand it
        Container contentPane;
        Graphics g;
        JFrame win = new JFrame("Random Graphics");
        win.setSize(825,650);
        win.setLocation(0,0);
        win.setVisible(true);
		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
        contentPane = win.getContentPane();
        contentPane.setBackground(Color.white);
        g = contentPane.getGraphics();
        Thread.sleep(50);
        //*** done setting up graphics window
         
        
	
		
		// Create Random object
		Random r = new Random();
		
		
		
		// Draws Grid - DO NOT CHANGE
		// After you use JOptionPane to get the number of lines, you can move this
		// section of code to just after that, so the lines will not disappear 
		g.drawRect(0,0,MAXX+1,MAXY+1);
		g.drawLine(0,MIDY,MAXX,MIDY); // horizontal line
		g.drawLine(MIDX,0,MIDX,MAXY); // vertical line
		
		
		
		// Top left quadrant:  
		// Use a JOptionPane to ask the user to enter the number of lines 1 to 100.  
		// Use a do..while loop to keep asking until they enter a correct value.
		// Then, in that quadrant, generate the lines with random colors, random
		// starting and ending points.  Use a for loop to draw the lines.
		String lq;
		int lqn;
		do
		{
			lq=JOptionPane.showInputDialog("Enter the number of lines [1 to 100]");
			lqn=Integer.parseInt(lq);
		}
		while(lqn<1&&lqn>100);
		int i;
		for(i=lqn; i>0; i--)
		{
			g.setColor(new Color(r.nextInt(COLOR), r.nextInt(COLOR), r.nextInt(COLOR)));
			g.drawLine(r.nextInt(MIDX), r.nextInt(MIDY), r.nextInt(MIDX), r.nextInt(MIDY));
		}
			
	
	
	
	
		// Top right quadrant: 
		// Draw 50 squares in top right quadrant with random widths between 2 and 50.
		// The color of each square should be a random red value. Green and blue values
		// should be 0.  Use a while loop to draw the squares.
		int b=0;
		while(b<51)
		{
			g.setColor(new Color(r.nextInt(COLOR), (0), (0)));
			g.fillRect(MIDX+50, MIDY+50, r.nextInt(48)+2, r.nextInt(48)+2);
			b++;
			
		}
		
		
		
			
		// Bottom left quadrant:
		// Draw 75 frame ovals in lower left quadrant with random widths between 2 and 75
		// and random heights between 5 and 25.  The color should be a random blue value.  
		// Red and green values should be 0.  Use a do..while loop to draw the ovals.
		
		
		
		
		// Bottom right quadrant:
		// Draw a patchwork of 50 x 50 squares of random colors to fill the lower right quadrant
		// Red, green and blue values should all be random.  
		// Use nested for loops to draw the squares.
		
		
		
    }
  
  
    
}

Recommended Answers

All 2 Replies

When I compile it the squares that you said that is suppose to be in the top right quadrant appear in the bottom right quadrant

while(b<51)
{
g.setColor(new Color(r.nextInt(COLOR), (0), (0)));
g.fillRect(MIDX+50, MIDY-50, r.nextInt(48)+2, r.nextInt(48)+2);
b++;

the y value coordinate in computers has the opposite value from the regular cartesian coordinate plane meaning the upper side is negative and the lower side is positive

Member Avatar for hfx642

ie. (0, 0) (x, y) is the LEFT, TOP corner of the screen.

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.