Results 1 to 5 of 5
  1. #1
    chelseacortez is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default I want to recreate the method drawRect with a nested for-loop

    I have absolutely no idea how to start to create a method that will draw an unfilled rect. on the picture instantiated.

    The constructor of the class eats the following parameters:
    int width, int height

    I have the Color class imported and I use the usual parameters:
    int xStartingPt, int yStartingPt, int width, int height, Color color

    Please help me get started.
    Last edited by chelseacortez; 09-05-2008 at 07:00 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Can you post the code you are working on to show where your problem is?
    constructor of the class eats the following parameters:
    What does that mean?

    A rectangle has lines on 4 sides. It's upper left corner would be at a point say x,y. Given that position, you can compute the x,y points for the other 3 corners and draw connecting lines.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class DrawRect extends JPanel {
        int width;
        int height;
    
        public DrawRect(int width, int height) {
            this.width = width;
            this.height = height;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = 50;
            int y = 25;
            drawRect(g, x, y);
        }
    
        private void drawRect(Graphics g, int x0, int y0) {
            for(int i = 0; i < 4; i++) {
                int x = x0 + i*25;
                int y = y0 + i*25;
                g.drawRect(x, y, width, height);
            }
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new DrawRect(125, 75));
            f.setSize(300,300);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    You left off the color
    What are the hardcoded(not parameterized) values: 25 & 50 used for?
    Last edited by Norm; 09-05-2008 at 04:11 PM.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    You left off the color
    chelseacortez asked to help me get started.
    I just wanted to offer a minimal suggestion.

    What are the hardcoded(not parameterized) values: 25 & 50 used for?
    Some values to send off to the drawRect method which will use them to determine the origin of the first (and subsequent) rectangle(s) that it draws.

Similar Threads

  1. Nested(Inner) Classes
    By Z.S.Tehrani in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 10:54 AM
  2. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  3. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  4. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM
  5. Nested Tags JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2007, 05:42 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
  •