By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,024 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,024 IT Pros & Developers. It's quick & easy.

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

evilmonkey
P: 14
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

Expand|Select|Wrap|Line Numbers
  1. package nestedsquare;
  2. import javax.swing.JFrame;
  3.  
  4. public class Main {
  5.  
  6.     /** Creates a new instance of Main */
  7.     public Main() {
  8.     }
  9.  
  10.     /**
  11.      * @param args the command line arguments
  12.      */
  13.     public static void main(String[] args) {
  14.         // TODO code application logic here
  15.        JFrame application = new JFrame();
  16.        DrawSquares   panel = new  DrawSquares();
  17.        String myTitle = " NestedSquare ";
  18.             application.setTitle(myTitle);
  19.             application.add( panel ); // add the panel to the frame      
  20.             application.setSize( 263, 290 ); // set the desired size
  21.             application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.             application.setVisible(true);
  23.  
  24.     }
  25.  
  26. }
Expand|Select|Wrap|Line Numbers
  1. package nestedsquare;
  2. import java.awt.Graphics;
  3. import javax.swing.JPanel;
  4.  
  5. public class DrawSquares extends JPanel
  6. {
  7.  
  8.  
  9.    public DrawSquares()
  10.    {
  11.  
  12.    } 
  13.  
  14.    // draws a cascade of shapes starting from the top left corner
  15.    public void paintComponent( Graphics g )
  16.    {
  17.       super.paintComponent( g );
  18.  
  19.       for ( int i = 0; i < 256; i++ )
  20.       {
  21.                g.drawRect( 1 + i++, 1 + i++, 
  22.                   256 - i*2 , 256 - i*2 ); 
  23.          }
  24.       } 
  25.    } // end method 
Mar 2 '07 #1
Share this Question
Share on Google+
8 Replies


DeMan
100+
P: 1,806
I believe there is a fillRect function that takes a colour as its' last argument
Mar 3 '07 #2

evilmonkey
P: 14
I looked in the API but can't find anything on it where can I go to learn about the fillRect function?
Mar 3 '07 #3

DeMan
100+
P: 1,806
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)
Mar 3 '07 #4

evilmonkey
P: 14
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.
Mar 3 '07 #5

10K+
P: 13,264
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 .
Mar 3 '07 #6

DeMan
100+
P: 1,806
Alternatively

There you go .
Mar 3 '07 #7

evilmonkey
P: 14
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.Graphics;
import javax.swing.JPanel;

public class DrawSquares extends JPanel
{
public DrawSquares()
{

}

public void paintComponent( Graphics g )
{
super.paintComponent(g);
//setBackground(Color.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(Color.RED);
g.drawRect( 1 + i++, 1 + i++,
256 - i*2 , 256 - i*2 );
}
}

} // end method
Mar 4 '07 #8

10K+
P: 13,264
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.Graphics;
import javax.swing.JPanel;

public class DrawSquares extends JPanel
{
public DrawSquares()
{

}

public void paintComponent( Graphics g )
{
super.paintComponent(g);
//setBackground(Color.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(Color.RED);
g.drawRect( 1 + i++, 1 + i++,
256 - i*2 , 256 - i*2 );
}
}

} // end method
All these statements

Expand|Select|Wrap|Line Numbers
  1.  for (int red =0; red <256; red++); 
  2. for (int green =0; green <256; green++);
  3. for (int blue =0; blue <256; blue++);
  4.  
are all do nothing statements.
Mar 5 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.