256 colorful nested squares only without the color...help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evilmonkey
    New Member
    • Jan 2007
    • 14

    256 colorful nested squares only without the color...help

    I am using swing to nest squares with in squares (256 times) and that part is working fine, but what i want to do is change the color of each line so that the red green and blue components each increment by 1 each time the next line is drawn. I really never used jpanel or jframe much less swing. any help I coulg get would be appreciated.

    Thanks in advance.

    here is the code i've got thus far

    Code:
    package nestedsquare;
    import javax.swing.JFrame;
    
    public class Main {
        
        /** Creates a new instance of Main */
        public Main() {
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
           JFrame application = new JFrame();
           DrawSquares   panel = new  DrawSquares();
           String myTitle = " NestedSquare ";
                application.setTitle(myTitle);
                application.add( panel ); // add the panel to the frame      
                application.setSize( 263, 290 ); // set the desired size
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                application.setVisible(true);
          
        }
        
    }
    Code:
    package nestedsquare;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class DrawSquares extends JPanel
    {
      
       
       public DrawSquares()
       {
         
       } 
       
       // draws a cascade of shapes starting from the top left corner
       public void paintComponent( Graphics g )
       {
          super.paintComponent( g );
          
          for ( int i = 0; i < 256; i++ )
          {
                   g.drawRect( 1 + i++, 1 + i++, 
                      256 - i*2 , 256 - i*2 ); 
             }
          } 
       } // end method
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I believe there is a fillRect function that takes a colour as its' last argument

    Comment

    • evilmonkey
      New Member
      • Jan 2007
      • 14

      #3
      I looked in the API but can't find anything on it where can I go to learn about the fillRect function?

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        if tyou have graphic called g
        g.setColor(new Color(x, y, z))
        /*notice Amaerican spelling for Color, and x y z are ints between 0 and 255
        (you might like to play around with these values to find what you want */
        and then
        g.fillRect(x, y, width, height)

        Comment

        • evilmonkey
          New Member
          • Jan 2007
          • 14

          #5
          Originally posted by DeMan
          if tyou have graphic called g
          g.setColor(new Color(x, y, z))
          /*notice Amaerican spelling for Color, and x y z are ints between 0 and 255
          (you might like to play around with these values to find what you want */
          and then
          g.fillRect(x, y, width, height)
          Thank you, I'll try it.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by evilmonkey
            I looked in the API but can't find anything on it where can I go to learn about the fillRect function?
            There you go .

            Comment

            • DeMan
              Top Contributor
              • Nov 2006
              • 1799

              #7
              Alternatively

              Originally posted by r035198x
              There you go .

              Comment

              • evilmonkey
                New Member
                • Jan 2007
                • 14

                #8
                Originally posted by DeMan
                Alternatively
                Can anyone help me figure out how to nest the for loops I need to increment the red green blue by 1 each time it draws a new rectangle ?
                for some reason netbeans can not find the symbols red, blue, green.



                package nestedsquare;
                import java.awt.Color;
                import java.awt.Graphi cs;
                import javax.swing.JPa nel;

                public class DrawSquares extends JPanel
                {
                public DrawSquares()
                {

                }

                public void paintComponent( Graphics g )
                {
                super.paintComp onent(g);
                //setBackground(C olor.BLACK);

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

                for (int red =0; red <256; red++);
                for (int green =0; green <256; green++);
                for (int blue =0; blue <256; blue++);

                g.setColor( new Color(red, green, blue));
                //g.setColor(Colo r.RED);
                g.drawRect( 1 + i++, 1 + i++,
                256 - i*2 , 256 - i*2 );
                }
                }

                } // end method

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by evilmonkey
                  Can anyone help me figure out how to nest the for loops I need to increment the red green blue by 1 each time it draws a new rectangle ?
                  for some reason netbeans can not find the symbols red, blue, green.



                  package nestedsquare;
                  import java.awt.Color;
                  import java.awt.Graphi cs;
                  import javax.swing.JPa nel;

                  public class DrawSquares extends JPanel
                  {
                  public DrawSquares()
                  {

                  }

                  public void paintComponent( Graphics g )
                  {
                  super.paintComp onent(g);
                  //setBackground(C olor.BLACK);

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

                  for (int red =0; red <256; red++);
                  for (int green =0; green <256; green++);
                  for (int blue =0; blue <256; blue++);

                  g.setColor( new Color(red, green, blue));
                  //g.setColor(Colo r.RED);
                  g.drawRect( 1 + i++, 1 + i++,
                  256 - i*2 , 256 - i*2 );
                  }
                  }

                  } // end method
                  All these statements

                  Code:
                   for (int red =0; red <256; red++); 
                  for (int green =0; green <256; green++);
                  for (int blue =0; blue <256; blue++);
                  are all do nothing statements.

                  Comment

                  Working...