How to create an applet, with many squares inside?

Hey,

I'm having problems creating an applet. Heres the image I need to recreate. The drawing should scale, so that it reaches the full width and

height of the applet.

http://img503.imageshack.us/my.php?image=graphicsafx5.jpg

I created a similar one (the code is below), but don't know how to modify it to create the image in the above link. Do I need a for loop? like for ( int i = 0; i < 10; ++i )

import java.applet.Applet;

import java.awt.*;

publicclass AprilTest1extends Applet

{

publicvoid paint(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

int width = getWidth();

int height = getHeight();

g2.setColor( Color.black );

g2.fillRect( 0, 0, width, height);

int pen_width = width/90 + 1;

g2.setStroke(new BasicStroke(pen_width));

g2.setColor( Color.white );

g2.drawRect( width/4, height/4, width/2, height/2);

}

}

[1431 byte] By [Donut20a] at [2007-11-27 11:18:35]
# 1

- if you ask things like "do I need a for loop", working with GUIs might not meet your sort of skillset yet

- if you ask things like "how do I modify this code", you might still be far away from actually knowing what you're doing, and should rather start writing that program from scratch

- think about how you'd create the same display with sheets of black and white paper (hint: rectangles of different size and color)

- you do need a for loop, terminating at the number of rectangles you want, and on each iteration changing color, size and position of the square to draw. So you will have to do some calculations based on the applet size as well.

CeciNEstPasUnProgrammeura at 2007-7-29 14:31:45 > top of Java-index,Java Essentials,Java Programming...
# 2

As CeciNEstPasUnProgrammeur said you do need a loop. You can probably make good use of the % operator inside your loop also.

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

if(i % 2 == 0) {

//draw black rectangle

} else {

//draw white rectangle

}

}

Just think about what you need to do:

-Start 1 rectangle at (0,0) and have its width and height as getWidth() and getHeight().

-Pick an increment [this will be the 'width' of each rectangle]

-Now for each rectangle you want to move your x and y points right and down by your increment. Moreover, you want to make your width and height smaller by your increment*2

Essentially what I did is initialize 3 variables to 0 [say x, y, and z]. In your for loop use the % operator in an if-else block. If the loop is on an even number make a white rectangle, if the loop is on an odd number make a black rectangle. Set the x and y position to x+z and y+z respectivley. Set the height and width to getHeight()-(z*2) and getWidth()-(z*2) and then increment z by...lets say 10.

This means on the first loop:

x = 0

y = 0

width = getWidth()

height = getHeight()

Second loop:

x = 10

y = 10

width = getWidth()-20 [10 to compensate for the shift, and 10 for the width]

height = getHeight()-20

Here is what I came up with - hope it helps.

import java.applet.Applet;

import java.awt.*;

public class AprilTest1 extends Applet {

private int x, y, z = 0;

public void paint(Graphics g) {

for(int i=0; i><10; i++){

if(i%2==1){

g.setColor( Color.white );

g.fillRect(x+z, y+z, getWidth()-(z*2), getHeight()-(z*2));

} else {

g.setColor( Color.black );

g.fillRect(x+z, y+z, getWidth()-(z*2), getHeight()-(z*2));

}

z += 10;

}

}

}

Any questions, feel free to ask :)

SidewinderXa at 2007-7-29 14:31:45 > top of Java-index,Java Essentials,Java Programming...
# 3

Thank you for the replies.

SideWinder, thanks for the code and hints. When I run your code, the black square gets smaller everytime i resize the applet window.

I'm trying to create an image that already has lots of alternating, white and black squares, like this: http://img503.imageshack.us/my.php?image=graphicsafx5.jpg

And whenever I resize the applet, the image needs to adjusts to the size of the window.

I'm lost and would appreciate any further help, my email is donut20@hotmail.co.uk, thanks.

Donut20a at 2007-7-29 14:31:45 > top of Java-index,Java Essentials,Java Programming...
# 4

To make the squares adjust to the window you need to do three things:

-Make the applet listen for a size change.

-Calculate the maximum amount of loops needed, which is dependent on the size of the applet. [At the default size, its 10, the larger the window, the more loops you will need and vice versa].

-Then repaint the window.

Struggle with it for a little and when you come up with something post back here so I know you at least tried, and ill help you from there.

Also in my code above I have:

for(int i=0; i><10; i++){

It should be:

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

Not sure how the extra `>` got in there...

SidewinderXa at 2007-7-29 14:31:45 > top of Java-index,Java Essentials,Java Programming...
# 5

Hey mate, thank you for the hints. I've been trying to wrap my head around this, but still stuck.

I tried changing my approach and having the background as black, and trying to paint the numerous white rectangles inside,

import java.applet.Applet;

import java.awt.*;

public class Box extends Applet

{

private int x, y, z = 0;

public void paint(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

int width = getWidth();

int height = getHeight();

g2.setColor( Color.black );

g2.fillRect( x, y, width, height);

for(int i=0; i<20; i++);

g.setColor( Color.white );

g.fillRect(x+z, y+z, width-(z*2), height-(z*2));

z += 10;

}

}

If I tried creating it your way, would I need a mouselistener? and I'm not sure where to put the repaint().

Please help,

Kind regards.

Donut20a at 2007-7-29 14:31:45 > top of Java-index,Java Essentials,Java Programming...