Okay, if I could, I would shoot my CS teacher, and then shoot myself.

Reality check. Done.

Anyways, check out this yucky program right here...I don't even know where to begin...we need to create a new drawing pad; and by using two methods, we would have to create this illusion image by using only parallel lines and shaded "rectangles". Here's the link to the assignment, since I can't copy or save his files:

Username: cs2
password: 89106

I know I'm asking you guys to go through alot, but I really need help on this. This is a challenging program, and most of the older kids in my class are having BIG trouble solving this assignment...

I'm going to work on it now and see what stuff I can come up with. But please, if you got spare time, take a quick look at the program and see if you can help. Thanks ALOT!!!

Dani AI

Generated

Short guide that fills gaps in the thread: the effect is simple in concept — a regular grid of same‑size cells, some cells shaded, and every row shifted by a small horizontal offset so the eye interprets bends where none exist. is right that nested loops do the heavy lifting; the refinements below make the result predictable and pixel‑perfect.

Practical steps

  1. Pick a cell size (e.g., cell = 40) and compute cols = width/cell + 2 (add 2 to cover extra offset) and rows = height/cell.
  2. Choose an offset pattern. A robust, readable choice is offset = (row * offsetStep) % cell, where offsetStep controls how much each row slides (try cell/2 for alternating halves or cell/3 for subtler shifts).
  3. Shade using parity so patterns stay consistent: if (row + col) % 2 == 0 fill the cell; otherwise leave it. This avoids complex flags.
  4. Draw thin parallel lines (vertical or horizontal) across the full canvas after painting rectangles — lines should be 1px and drawn after fills so they sit cleanly on top.

Java/Swing tips

  • Override paintComponent(Graphics g) and cast to Graphics2D. Turn antialiasing off for crisp edges (RenderingHints.VALUE_ANTIALIAS_OFF). Use integer math for coordinates to avoid half‑pixel jitter.
  • If the image is static, render once into a BufferedImage and draw that each repaint to save CPU.

Example skeleton (very short):

protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D)g;
  int cell = 40, offsetStep = cell/3;
  for (int r=0;r<rows;r++) {
    int off = (r*offsetStep)%cell;
    for (int c=0;c<cols;c++)
      if ((r+c)%2==0) g2.fillRect(c*cell+off, r*cell, cell, cell);
  }
  for (int x=0;x<=getWidth(); x+=4) g2.drawLine(x,0,x,getHeight());
}

Troubleshooting

  • If you see gaps on the right, increase cols.
  • If the illusion looks soft, disable antialiasing and use 1px strokes.
  • If offsets run off the canvas, clamp or wrap with % cell.

This keeps the nested‑loop idea but replaces flag toggling with deterministic math and fixes common off‑by‑one and drawing‑order pitfalls.

Recommended Answers

All 2 Replies

Given the date of the assignment, I think I can assume that it's already been due so I suppose I could try and answer it.
I kind of mixed basic and c++ code structures, but I think something like this would work. The only slightly tedious part is the row's offset. It's just nested loops, nothing really tricky here.

offset = 0
flag = 0
for y = 0 to 7
	for x = 0 to 13 step 2
		pen.setPosition(x*40 + offset,y*40)
		pen.fillRect(40,40)
		
		pen.setPosition(0 ,(y+1)*40)
		pen.drawLine(0,endOfScreen)  
	next x
	//determine whether to increase or decrease the offset of the row
	//offsets by 3 each time
	if flag = 0 and offset = 6
	{
		flag = 1
	}
	if flag = 1 and offset = 0
	{
		flag = 0
	}
	if flag = 0 then offset += 3
	if flag = 1 then offset -= 3
next y

As for the circles, I don't know what its suppose to look like.

Hmm...thanks for replying JUST in time. Luckily, this is due tomorrow, so at least I got some help for now.

I got the circles part myself...lemme see if your code works, Phaelax. and Thanks ALOT!! ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.