Question using if statement to repeat circles

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2948 Views - Last Post: 30 September 2012 - 10:53 AM Rate Topic: -----

#1 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Question using if statement to repeat circles

Posted 29 September 2012 - 09:01 PM

Hi folks! First time posting here and hoping I can get some help :)

I have a homework assignment that is asking me to create a JFrame that is filled with randomly colored circles
I have a bit of code already completed, but cannot wrap my head around the "if" statement and how to use it properly to execute this bit of code.

Below is my code and I am at a loss as to what to do next.

/*
 */
package p03looping;

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


public class ColorDots extends javax.swing.JFrame {
    public static final int CIRCLE_WIDTH = 100;
    public static final int CIRCLE_HEIGHT = 100;
    public static final int CIRCLE_COUNT = 35;
    public static final int CIRCLE_RIGHT_OFFSET = 120;
    public static final int CIRCLE_TOP_OFFSET = 40;
    public static final int CIRCLE_SPACE = 10;
    
    public ColorDots(String title) {
        setTitle("Colored Dots");}
    
    @Override
    public void paint(Graphics canvas) {
        super.paint(canvas);
    
        int canvas_width = getWidth();
        int canvas_height = getHeight();
        
        // Background color = black
        canvas.setColor(Color.BLACK);
        canvas.fillRect(0, 0, canvas_width, canvas_height);
        
        // Drawing circles with random colors
        int circleX;
        int circleY;        
        int red = (int) (Math.random( )*256);
        int green = (int)(Math.random( )*256);
        int blue = (int)(Math.random( )*256);
        Color randomColor = new Color(red, green, blue);
        canvas.setColor(randomColor);
        
        for (int circleCount=0; circleCount <= CIRCLE_COUNT; circleCount++)
        {
            
            int space = canvas_width-CIRCLE_WIDTH;
            canvas.fillOval(canvas_width-CIRCLE_RIGHT_OFFSET, CIRCLE_TOP_OFFSET, 
                        CIRCLE_WIDTH, CIRCLE_HEIGHT);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Question using if statement to repeat circles

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: Question using if statement to repeat circles

Posted 29 September 2012 - 09:40 PM

Quote

I have a bit of code already completed, but cannot wrap my head around the "if" statement and how to use it properly to execute this bit of code.

An if statement to do what? It's not clear how it comes into play in accomplishing your goal, nor is it clear what you have already accomplished.
Was This Post Helpful? 0
  • +
  • -

#3 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Question using if statement to repeat circles

Posted 29 September 2012 - 09:45 PM

I have currently accomplished the following: Black background on the JFrame and a random color generator to fill the oval.

What needs to be accomplished: I need the JFrame filled with 5 rows and 7 columns of these randomly colored circles.

The code above will draw a single circle in the top right corner of the JFrame, but I don't know where to go from there. My completed project needs to look like this:

Posted Image

But I don't know how to use the "if" statement to continue to draw circles. I can only get the top right one to be drawn.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: Question using if statement to repeat circles

Posted 29 September 2012 - 09:49 PM

You don't really need an if statement. You need nested loops and to offset the x and y coordinates based on the loop iterations.

for(int i = 0; i < rows; i++){

    for(int j = 0; j < cols; j++){
        //generate a color
        //draw the circle
        //increment the x-coordinate by the x-offset
    }

    //reset the x coordinate to move to the next row
    //increment the y coordinate by the y-offset
}


Was This Post Helpful? 1
  • +
  • -

#5 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Question using if statement to repeat circles

Posted 29 September 2012 - 09:53 PM

Yes, I am sorry! I meant a "for" statement. If you don't mind, do you mind telling me what the int j and i are for? and what "cols" is? I am new at this and trying to get as much grasp on it as possible.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: Question using if statement to repeat circles

Posted 29 September 2012 - 09:58 PM

The cols variable represents the number of columns in the row. The i and j variables are just counters, which should be pretty self-explanatory.
Was This Post Helpful? 0
  • +
  • -

#7 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Question using if statement to repeat circles

Posted 29 September 2012 - 09:59 PM

Gotcha! I'll give a shot at finishing my code and let you know the results tomorrow. Thanks for your time! ^.^
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: Question using if statement to repeat circles

Posted 29 September 2012 - 10:00 PM

Glad I could help! Good luck and happy coding! :)
Was This Post Helpful? 0
  • +
  • -

#9 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Question using if statement to repeat circles

Posted 30 September 2012 - 07:32 AM

This is what I've got right now.

// Drawing circles with random colors        
        int rows, cols;
        int red = (int) (Math.random( )*256);
        int green = (int)(Math.random( )*256);
        int blue = (int)(Math.random( )*256);
        Color randomColor = new Color(red, green, blue);
        canvas.setColor(randomColor);
        
        rows = 8;
        cols = 6;
        
        for (int i = 0; i < rows; i++);{
            for(int j = 0; j < cols; j++);
            canvas.setColor(randomColor);
            canvas.fillOval(canvas_width-CIRCLE_RIGHT_OFFSET, CIRCLE_TOP_OFFSET, 
                        CIRCLE_WIDTH, CIRCLE_HEIGHT);
            canvas_width = canvas_width - CIRCLE_RIGHT_OFFSET - CIRCLE_WIDTH;


I'm still not sure how I would go about moving the circle from that single top left spot to a spot left of it.

I get tat there is an x value that needs to be adjusted, but I don't know what I have to do to adjust that x value. The same pretty much goes for the y value as well. :(

Am I onto something with this line?

            canvas_width = canvas_width - CIRCLE_RIGHT_OFFSET - CIRCLE_WIDTH;



I'm crunching my brain over this seemingly simple project, but getting nowhere...
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Question using if statement to repeat circles

Posted 30 September 2012 - 08:59 AM

You're not moving the circle you've already drawn, you're drawing circles at different locations than the one you've drawn. As already discussed, you need a loop (or a nested loop) that changes the x/y starting point of each subsequent circle.

You can do some simple math to figure out the delta x (starting point from the left): if the first circle starts at (5, 5) and is 50 pixels wide (diameter) and you'd like a 5 pixel border between each circle, then:

deltaX = 50 + 5 = 55

for the next x. So you'll draw the first row of circles at:

(5, 5)
(60, 5)
(115, 5)
etc . . .

You'll do a similarly simple calculation for deltaY to compute the starting point for the next row.
Was This Post Helpful? 1
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: Question using if statement to repeat circles

Posted 30 September 2012 - 09:21 AM

And you don't want semi-colons after your loops.
Was This Post Helpful? 1
  • +
  • -

#12 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Question using if statement to repeat circles

Posted 30 September 2012 - 09:46 AM

Getting closer now!

This is my current state: I have one complete row of 7 circles.

What I am doing wrong: Every circle is the same color, and I can't get the circles to repeat on a different row.

        // Drawing circles with random colors        
        int circles;
        int red = (int) (Math.random( )*256);
        int green = (int)(Math.random( )*256);
        int blue = (int)(Math.random( )*256);
        Color randomColor = new Color(red, green, blue);
        canvas.setColor(randomColor);
        
        int rows = 5;
        int cols = 7;
        
        for (int circlesInColumn = 0; circlesInColumn <= rows; circlesInColumn++){
            for (int circlesInRow = 1;  circlesInRow <= cols; circlesInRow++){
            
                canvas.setColor(randomColor);
                canvas.fillOval(canvas_width - CIRCLE_RIGHT_OFFSET, CIRCLE_TOP_OFFSET, 
                        CIRCLE_WIDTH, CIRCLE_HEIGHT);
                canvas_width = getWidth();
                int deltaX;
                deltaX = 8 + CIRCLE_WIDTH;
                canvas_width = canvas_width - deltaX*circlesInRow;     
            }
            canvas_width = getWidth();
            int deltaY;
            deltaY = CIRCLE_HEIGHT*circlesInColumn;
            canvas_height = canvas_height - deltaY;
            
        }

Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Question using if statement to repeat circles

Posted 30 September 2012 - 09:53 AM

The random color is set outside the for loops so will be the same for each circle drawn. Vary the color inside the for loop(s) before each circle is drawn.

It looks like you're not adding the deltaY to calculate the new row. I think you're printing 35 circles on the same (first) row. Calculating the new row starting point should look something like:

CIRCLE_TOP_OFFSET += deltaY;

And you don't need to calculate deltaY after each row. Unless your changing the circle's diameter, deltaY will be the same.
Was This Post Helpful? 1
  • +
  • -

#14 Jakey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-September 12

Re: Question using if statement to repeat circles

Posted 30 September 2012 - 10:06 AM

Yes, that makes so much more sense now! I think that I have one more question though.

I am pulling an error from trying to offset the rows. Netbeans says that it cannot assign a value to a final variable CIRCLE_TOP_OFFSET.

Any simple solution to this type of error?
Was This Post Helpful? 0
  • +
  • -

#15 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Question using if statement to repeat circles

Posted 30 September 2012 - 10:11 AM

Ahh, I should have realized that it was final with the name in all caps. Instead of recalculating the offset for each row, you'll add deltaY to it for each new row as in:

canvas.fillOval(canvas_width - CIRCLE_RIGHT_OFFSET, CIRCLE_TOP_OFFSET + deltaY,
CIRCLE_WIDTH, CIRCLE_HEIGHT);
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2