Greyscale bars/Display: Difference between revisions
Content added Content deleted
(→{{header|C sharp|C#}}: removing some repetition from c# version) |
(→{{header|Java}}: Adjusted so color component always goes from 0 to 255 or vice versa in each quarter) |
||
Line 627: | Line 627: | ||
public void paint ( Graphics g ) { |
public void paint ( Graphics g ) { |
||
int run = 0 ; |
int run = 0 ; |
||
double colorcomp = 0.0 ; //component of the color |
|||
for ( int columncount = 8 ; columncount < 128 ; columncount *= 2 ) { |
for ( int columncount = 8 ; columncount < 128 ; columncount *= 2 ) { |
||
double colorgap = 255.0 / (columncount - 1) ; //by this gap we change the background color |
|||
int columnwidth = width / columncount ; |
int columnwidth = width / columncount ; |
||
int columnheight = height / 4 ; |
int columnheight = height / 4 ; |
||
if ( run % 2 == 0 ) //switches color directions with every for loop |
if ( run % 2 == 0 ) //switches color directions with every for loop |
||
colorcomp = 0 ; |
colorcomp = 0.0 ; |
||
else { |
else { |
||
colorcomp = 255 ; |
colorcomp = 255.0 ; |
||
colorgap *= -1 ; |
colorgap *= -1.0 ; |
||
} |
} |
||
int ystart = 0 + columnheight * run ; |
int ystart = 0 + columnheight * run ; |
||
int xstart = 0 ; |
int xstart = 0 ; |
||
for ( int i = 0 ; i < columncount ; i++ ) { |
for ( int i = 0 ; i < columncount ; i++ ) { |
||
int icolor = (int)Math.round(colorcomp) ; //round to nearer integer |
|||
Color nextColor = new Color( |
Color nextColor = new Color( icolor , icolor, icolor ) ; |
||
g.setColor( nextColor ) ; |
g.setColor( nextColor ) ; |
||
g.fillRect( xstart , ystart , columnwidth , columnheight ) ; |
g.fillRect( xstart , ystart , columnwidth , columnheight ) ; |
||
Line 655: | Line 656: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Live Demo: http://jsfiddle.net/gcN9g/embedded/result/ |
Live Demo: http://jsfiddle.net/gcN9g/embedded/result/ |