0

I need to add the Rectangle to the ArrayList called bricks as you can see below.

private void drawBrick(int startX, int startY){ new Rectangle(); currentColor = 0; startX = 54; startY = 16; bricks = new ArrayList<Rectangle>(); bricks.add("Rectangle"); } 

I keep getting compilation errors after adding that that last line of code and this weird warning pops up saying "Some messages have been simplified; recompile with -Xdiags:verbose to get full output."

Anyone know what I've done wrong?

5
  • 1
    When you did new Rectangle(), you made a Rectangle object. Right now you're not actually doing anything with it though, you would store that in a variable then add it to your list. Commented Nov 6, 2016 at 23:02
  • 2
    You need to learn about basic Java syntax. You are trying to add a string with the contents Rectangle into the list, not a Rectangle object. You should assign your new Rectangle() to a variable and use that. Commented Nov 6, 2016 at 23:02
  • 1
    You added string "Rectangle". It was not Rectangle instance. Commented Nov 6, 2016 at 23:04
  • 2
    Where to start! One that hasn't been mentioned is that you'll overwrite your ArrayList bricks each time you call the method, making it useless. Commented Nov 6, 2016 at 23:05
  • You keep getting compilation errors such as what? Without that information there is no question here to answer. NB There are hundreds of add() methods in Java. Commented Nov 6, 2016 at 23:15

1 Answer 1

1

You can't just refer to the Rectangle you created by using "Rectangle". Instead, you need to name the Rectangle when you create it and use that name later on, like this:

private void drawBrick(int startX, int startY) { Rectangle theRectangle = new Rectangle(); currentColor = 0; startX = 54; startY = 16; bricks = new ArrayList<Rectangle>(); bricks.add(theRectangle); } 

Notice how that creates a variable called theRectangle which is set equal to the Rectangle you create, then that specific Rectangle (theRectangle) is added to the ArrayList called bricks.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.