1

I ran the following code on eclipse.

import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class picture extends GraphicsProgram {
    public void run() {
        GRect sqr = new GRect(20, 20, 50, 50);
        sqr.setColor(Color.BLUE);
        add(sqr);
        sqr.setLocation(0, 0);
        sqr.setColor(Color.RED);
        sqr.setFilled(true);
    }
}

Since the add(sqr) went before I set the location, fill, and before I set the color to red, shouldn't the result be an unfilled blue square? Yet for some reason a red square shows up.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Anonymous
  • 295
  • 2
  • 13
  • 4
    The variable sqr is a reference to an Object. You can make changes to sqr even after you add the reference to GraphicsProgram, although it's not recommended to do so. As you've seen, it makes it harder for people (including you) to read and understand your code. – Gilbert Le Blanc Oct 07 '15 at 22:45
  • Do I have to create a new reference every time I want to add an object to GraphicsProgram? – Anonymous Oct 08 '15 at 00:07
  • Yes, each graphics object requires it's own reference to Object, or in other words, you have to create an instance of Object for each and every graphics object you want to draw. – Gilbert Le Blanc Oct 08 '15 at 14:20

0 Answers0