473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

evilmonkey
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
8 2187
DeMan
1,806 1GB
I believe there is a fillRect function that takes a colour as its' last argument
Mar 3 '07 #2
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
1,806 1GB
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
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
r035198x
13,262 8TB
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
1,806 1GB
Alternatively

There you go .
Mar 3 '07 #7
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
r035198x
13,262 8TB
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

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

Similar topics

2
by: Simon | last post by:
Hi, I am having a little problem with my PHP - MySQl code, I have two tables (shown below) and I am trying populate a template page with data from both. <disclaimer>Now I would like to say my...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
5
by: Candace | last post by:
I have to write a program to find all Pythagorean triples for a right triangle. I know that the squares of two sides of the triangle must equal the square of the third (longest) side. However, I...
10
by: wazzup | last post by:
C++ is new to me and I'm trying solve this problem but get stuck. Please help me out. Below is the task and after that is what i got so far. Create a program to print nested squares Input:...
1
by: jyohere | last post by:
I have a problem to solve.I want to draw squares and lines connecting them.The number of squares is only known at runtime.How can it be done.if someone has some code like this,pls help.I am able to...
7
by: mcollins1989 | last post by:
Draw a checkerboard (8 by 8 squares) out of vertical lines and underscores. You must use two nested for loops, and the for loops should each execute 8 times, e.g. for (int x = 0; x < 8; x++) {...
4
by: Evelien | last post by:
Dear python-users, I am trying to do a non-linear least squares fitting. Maybe trying is not the best word, as I already succeeded in that. At the moment I am using leastSquaresFit from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.