Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

java code help please?

i got the code i put up before down to one error it sais

NestedSquaresComponent.java:40: error: 'else' without 'if'

else if (holdColor = Color.BLUE);

^

1 error

my code is

import java.awt.*;

import javax.swing.*;

/**

A component that draws two rectangles.

*/

public class NestedSquaresComponent extends JComponent

{

public void paintComponent(Graphics g)

{

//create rectangle

Rectangle box = new Rectangle();

int x = 5;

int y = 5;

final int OFFSET = 25;

Color holdColor = Color.BLUE;

// Recover Graphics2D

Graphics2D g2 = (Graphics2D) g;

// Determine the size of the first square

// Set it to the smallest of the width and height,

// minus 10 for spacing between the square an the border

int size = Math.min(this.getWidth(), this.getHeight()) - 10;

box = new Rectangle(x, y, size, size);

g2.setColor(holdColor);

g2.fill(box);

// second square

while (size >= 10);

{

size = size - (OFFSET * 2);

x = x + OFFSET;

y = y + OFFSET;

box = new Rectangle(x, y, size, size);

if (holdColor = Color.ORANGE);

holdColor = (Color.BLUE);

else if (holdColor = Color.BLUE);

holdColor = (Color.ORANGE);

g2.fill(box);

size++;

}

Update:

i still get two errors here is the updated code it compiles but it is giving me a endless loop and not showing the picture

import java.awt.*;

import javax.swing.*;

/**

A component that draws two rectangles.

*/

public class NestedSquaresComponent extends JComponent

{

public void paintComponent(Graphics g)

{

//create rectangle

Rectangle box = new Rectangle();

int x = 5;

int y = 5;

final int OFFSET = 25;

// Recover Graphics2D

Graphics2D g2 = (Graphics2D) g;

// Determine the size of the first square

// Set it to the smallest of the width and height,

// minus 10 for spacing between the square an the border

int size = Math.min(this.getWidth(), this.getHeight()) - 10;

box = new Rectangle(x, y, size, size);

g2.setColor(Color.ORANGE);

g2.fill(box);

// second square

while (size = 10);

{

size = size - (OFFSET * 2);

x = x + OFFSET;

y = y + OFFSET;

box = new Rectangle(x, y, size, size);

Color holdColor = (Color.BLUE);

if (holdColor == (Color.ORANGE))

holdColor = (Color.BLUE);

if (holdColor =

1 Answer

Relevance
  • 10 years ago
    Favorite Answer

    You have a semicolon (;) after the if (condition). This is incorrect.

    You're basically saying "if the colour is orange, do nothing".

    Remove the semicolon after Color.ORANGE) and after Color.BLUE) and it should compile.

Still have questions? Get your answers by asking now.