Results 1 to 4 of 4
  1. #1
    Jakey is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Using nested loops to repeat circles

    Hi there!

    I am having a bit of trouble finishing this bit of code. I am very new to writing Java so please bear with me.

    What I have completed so far: I have a JFrame with a black background, a random color generator and a single circle.

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

    Here is what I have so far:

    Java Code:
    /*
     */
    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 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 = 6;
            cols = 8;
            
            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;
            }
            canvas_width = getWidth();
            canvas_height = canvas_height - CIRCLE_HEIGHT;        
        }
    }
    What I am mostly confused on is how do I get the circle to move from that top right spot to the rest of the JFrame. I am at a total loss with what I've got to do.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Using nested loops to repeat circles

    Stop what you're doing and first learn the correct way to do custom painting, which is not is a paint(...) override in a top level window.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    As to your question, read the API for the classes and methods you are using. Specifically, Graphics#fillOval(...). Without the specs available in the API, you're coding blindly.

    Also, why didn't you tell us about the earlier discussion on the topic?
    Question Using If Statement To Repeat Circles - Java | Dream.In.Code

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Jakey is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: Using nested loops to repeat circles

    I didn't know I had to tell you about another discussion on another forum?

    I can't exactly stop what I am doing because I can't just do stuff that I haven't learned in my class.

    I'm not sure what you've given me is suppose to help at all. I don't know what read the API is, all I know is what I've been told in my class...

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Using nested loops to repeat circles

    Quote Originally Posted by Jakey View Post
    I didn't know I had to tell you about another discussion on another forum?
    It's the polite thing to do.

    Quote Originally Posted by Jakey View Post
    I can't exactly stop what I am doing because I can't just do stuff that I haven't learned in my class.
    If you that class teaches doing custom painting by overriding paint(...) in a top level window -- JFrame -- then I would demand my money back. It's not just poor form, it's wrong.

    Quote Originally Posted by Jakey View Post
    I'm not sure what you've given me is suppose to help at all. I don't know what read the API is, all I know is what I've been told in my class...
    Bookmark this: Java Platform SE 7

    Scroll down the list in the lower left 'All Classes' pane and click on Graphics. Scroll down in the right pane and find fillOval... under 'Method Summary' and click that. Read the specs for the method and its parameters.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  2. Nested loops
    By Aestuv in forum New To Java
    Replies: 3
    Last Post: 02-10-2011, 11:40 PM
  3. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  4. nested <ui:repeat> - problem
    By hackerofcrackers in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-20-2008, 10:06 AM
  5. Circles (Loops)
    By Zebra in forum New To Java
    Replies: 1
    Last Post: 05-29-2008, 06:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •