Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trouble with draw and fillRect in pyramid logic using nested loop

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trouble with draw and fillRect in pyramid logic using nested loop

    Hello eveyone. I'm working on a project for a class to build a pyramid out of bricks using a user entered height in bricks between 1 and 100. I'm using a nested loop to draw a row of bricks then move up the height of a brick and draw the next row ect. The problem is when I enter the height of the pyramid and hit the button the first time nothing happens. When I hit the button a second time it only draws the first row of bricks. I spent a few hours writing the code and figuring out the logic and a couple of days trying to debug it. The code displays some diagnostic message dialogs that show the values of the variables before they enter the inner loop. The loops seem to be working and the variables are what I expected them to be. I even created a seperate method to draw the bricks that is called from the inner loop. Please take a look and let me know what I did wrong. I could really use a fresh pair of eyes. Also, is there a way to clear the panel before hitting the start button? Sorry about all the commented lines but I'm using them to turn off the statements when I rewrite the code. Thanks so much for you help.

    ]import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Pyramid extends JFrame implements ActionListener
        {
     
        private JPanel panel;
    //    private Random random;
        private JTextField pyramidHeightField;
        private JLabel pyramidHeightLabel;
        private JButton buildPyramidButton;
     
        public static void main (String[] args)
        {
            Pyramid frame = new Pyramid();
            frame.setSize(400, 500);
            frame.setTitle("Pyramid");
            frame.createGUI();
            frame.setVisible(true);
        }
     
        private void createGUI()
        {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container window = getContentPane();
            window.setLayout(new FlowLayout() );
     
            panel = new JPanel();
            panel.setPreferredSize(new Dimension(400, 400));
            panel.setBackground(Color.white);
            window.add(panel);
     
            pyramidHeightLabel = new JLabel("Enter Pyramid height from 1 to 100  ");
            window.add(pyramidHeightLabel);
            pyramidHeightField = new JTextField(10);
            window.add(pyramidHeightField);
     
            buildPyramidButton = new JButton("Build Pyramid");
            window.add(buildPyramidButton);
            buildPyramidButton.addActionListener(this);
        }
     
        public void actionPerformed(ActionEvent event)
        {
            int pyramidHeight, brickSize, random;
            int x = 0, y = 400, hLoop = 1, vLoop = 1, endWhile = 0;
            String diagText1, diagText2, diagText3, diagString;
     
            pyramidHeight = Integer.parseInt(pyramidHeightField.getText());
            if (pyramidHeight < 1 || pyramidHeight > 100)
            {
                pyramidHeight = 50;
            }
            brickSize = 400 / pyramidHeight;
     
     
            Graphics paper = panel.getGraphics();
     
            for (vLoop = 1; vLoop <= pyramidHeight; vLoop++)
            {
                y = y - brickSize;
     
                diagText1 = (Integer.toString(vLoop));
                diagText2 = (Integer.toString(x));
                diagText3 = (Integer.toString(y));
     
                endWhile = ((pyramidHeight + 1) - vLoop);
     
                while (hLoop <= endWhile)
                {
    //                paper.setColor(Color.yellow);
    //                paper.fillRect(x, y, brickSize, brickSize);
                    drawBrick(paper, x, y, brickSize);
     
    //                paper.setColor(Color.black);
    //                paper.drawRect(x, y, brickSize, brickSize);
     
     
                    x += brickSize;
     
                    hLoop++;
                }
    //            diagText1 = (Integer.toString(vLoop));
    //            diagText2 = (Integer.toString(x));
    //            diagText3 = (Integer.toString(y));
     
                x = (brickSize / 2) * vLoop;
     
                JOptionPane.showMessageDialog (null,
                diagString = "vLoop = " + diagText1 + " x = " + diagText2 + " y = " + diagText3);
     
     
            }
     
        }
        private void drawBrick(Graphics drawingArea, int xPos, int yPos, int size)
        {
            drawingArea.setColor(Color.yellow);
            drawingArea.fillRect(xPos, yPos, size, size);
            drawingArea.setColor(Color.black);
            drawingArea.drawRect(xPos, yPos, size, size);
        }   
     
     
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Trouble with draw/fillRect in nested loop

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Pyramid extends JFrame implements ActionListener
        {
     
        private JPanel panel;
    //    private Random random;
        private JTextField pyramidHeightField;
        private JLabel pyramidHeightLabel;
        private JButton buildPyramidButton;
     
        public static void main (String[] args)
        {
            Pyramid frame = new Pyramid();
            frame.setSize(400, 500);
            frame.setTitle("Pyramid");
            frame.createGUI();
            frame.setVisible(true);
        }
     
        private void createGUI()
        {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container window = getContentPane();
            window.setLayout(new FlowLayout() );
     
            panel = new JPanel();
            panel.setPreferredSize(new Dimension(400, 400));
            panel.setBackground(Color.white);
            window.add(panel);
     
            pyramidHeightLabel = new JLabel("Enter Pyramid height from 1 to 100  ");
            window.add(pyramidHeightLabel);
            pyramidHeightField = new JTextField(10);
            window.add(pyramidHeightField);
     
            buildPyramidButton = new JButton("Build Pyramid");
            window.add(buildPyramidButton);
            buildPyramidButton.addActionListener(this);
        }
     
        public void actionPerformed(ActionEvent event)
        {
            int pyramidHeight, brickSize, random;
            int x = 0, y = 400, hLoop = 1, vLoop = 1, endWhile = 0;
            String diagText1, diagText2, diagText3, diagString;
     
            pyramidHeight = Integer.parseInt(pyramidHeightField.getText());
            if (pyramidHeight < 1 || pyramidHeight > 100)
            {
                pyramidHeight = 50;
            }
            brickSize = 400 / pyramidHeight;
     
     
            Graphics paper = panel.getGraphics();
     
            for (vLoop = 1; vLoop <= pyramidHeight; vLoop++)
            {
                y = y - brickSize;
     
                diagText1 = (Integer.toString(vLoop));
                diagText2 = (Integer.toString(x));
                diagText3 = (Integer.toString(y));
     
                endWhile = ((pyramidHeight + 1) - vLoop);
     
                while (hLoop <= endWhile)
                {
    //                paper.setColor(Color.yellow);
    //                paper.fillRect(x, y, brickSize, brickSize);
                    drawBrick(paper, x, y, brickSize);
     
    //                paper.setColor(Color.black);
    //                paper.drawRect(x, y, brickSize, brickSize);
     
     
                    x += brickSize;
     
                    hLoop++;
                }
     
    //*************************
                hLoop = 1;
    //*************************
     
     
    //            diagText1 = (Integer.toString(vLoop));
    //            diagText2 = (Integer.toString(x));
    //            diagText3 = (Integer.toString(y));
     
                x = (brickSize / 2) * vLoop;
     
                JOptionPane.showMessageDialog (null,
                diagString = "vLoop = " + diagText1 + " x = " + diagText2 + " y = " + diagText3);
     
     
            }
     
        }
        private void drawBrick(Graphics drawingArea, int xPos, int yPos, int size)
        {
            drawingArea.setColor(Color.yellow);
            drawingArea.fillRect(xPos, yPos, size, size);
            drawingArea.setColor(Color.black);
            drawingArea.drawRect(xPos, yPos, size, size);
        }   
     
     
    }
    I change, I reset hLoop back to 1 .

    You can use Graphics.clearRect( ) with it's params to clear the area

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    LiquidMetal (April 24th, 2009)

  4. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with draw/fillRect in nested loop

    Thanks so much for your help Chris. I forgot to reinitalize hLoop...silly me. Do you know why the program won't draw the first time you hit the button? I've seen it happen on other programs I've written. Is this a common problem? Also is there way to clear the whole panel rather and clearing each Rect?

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Trouble with draw/fillRect in nested loop

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Pyramid extends JFrame implements ActionListener
        {
     
        private JPanel panel;
    //    private Random random;
        private JTextField pyramidHeightField;
        private JLabel pyramidHeightLabel;
        private JButton buildPyramidButton;
     
        public static void main (String[] args)
        {
            Pyramid frame = new Pyramid();
            frame.setSize(400, 500);
            frame.setTitle("Pyramid");
            frame.createGUI();
            frame.setVisible(true);
        }
     
        private void createGUI()
        {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container window = getContentPane();
            window.setLayout(new FlowLayout() );
     
            panel = new JPanel();
            panel.setPreferredSize(new Dimension(400, 400));
            panel.setBackground(Color.white);
            window.add(panel);
     
            pyramidHeightLabel = new JLabel("Enter Pyramid height from 1 to 100  ");
            window.add(pyramidHeightLabel);
            pyramidHeightField = new JTextField(10);
            window.add(pyramidHeightField);
     
            buildPyramidButton = new JButton("Build Pyramid");
            window.add(buildPyramidButton);
            buildPyramidButton.addActionListener(this);
        }
     
        public void actionPerformed(ActionEvent event)
        {
            int pyramidHeight, brickSize, random;
            int x = 0, y = 400, hLoop = 1, vLoop = 1, endWhile = 0;
            String diagText1, diagText2, diagText3, diagString;
     
            pyramidHeight = Integer.parseInt(pyramidHeightField.getText());
            if (pyramidHeight < 1 || pyramidHeight > 100)
            {
                pyramidHeight = 50;
            }
            brickSize = 400 / pyramidHeight;
     
     
            Graphics paper = panel.getGraphics();
     
    //*********************************
            paper.clearRect(0,0,400,400);        
    //*********************************
     
            for (vLoop = 1; vLoop <= pyramidHeight; vLoop++)
            {
                y = y - brickSize;
     
                diagText1 = (Integer.toString(vLoop));
                diagText2 = (Integer.toString(x));
                diagText3 = (Integer.toString(y));
     
                endWhile = ((pyramidHeight + 1) - vLoop);
     
                while (hLoop <= endWhile)
                {
    //                paper.setColor(Color.yellow);
    //                paper.fillRect(x, y, brickSize, brickSize);
                    drawBrick(paper, x, y, brickSize);
     
    //                paper.setColor(Color.black);
    //                paper.drawRect(x, y, brickSize, brickSize);
     
     
                    x += brickSize;
     
                    hLoop++;
                }
     
     
                hLoop = 1;
     
     
     
    //            diagText1 = (Integer.toString(vLoop));
    //            diagText2 = (Integer.toString(x));
    //            diagText3 = (Integer.toString(y));
     
                x = (brickSize / 2) * vLoop;
     
                JOptionPane.showMessageDialog (null,
                diagString = "vLoop = " + diagText1 + " x = " + diagText2 + " y = " + diagText3);
     
     
            }
     
        }
        private void drawBrick(Graphics drawingArea, int xPos, int yPos, int size)
        {
            drawingArea.setColor(Color.yellow);
            drawingArea.fillRect(xPos, yPos, size, size);
            drawingArea.setColor(Color.black);
            drawingArea.drawRect(xPos, yPos, size, size);
        }   
     
     
    }

    that clears it all at once. it works on first press for me :/

    Chris

  6. The Following User Says Thank You to Freaky Chris For This Useful Post:

    LiquidMetal (April 27th, 2009)

  7. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Trouble with draw/fillRect in nested loop

    Hey LiquidMetal,

    If your question has been answered, please mark this thread as solved
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    LiquidMetal (April 27th, 2009)

Similar Threads

  1. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  2. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  3. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 02:31 PM
  4. Trouble in downloading java
    By captjade in forum Java Theory & Questions
    Replies: 6
    Last Post: March 3rd, 2009, 04:16 PM

Tags for this Thread