I need to create a class that represents one crayon of a particular color and height(length). Then, I need to design an applet that lets me draw a box of different colored crayons.
This is what I have so far:
CLASS:

import java.awt.*;

public class Crayon
{
    private final int BOTTOM = 150;
    private final int WIDTH = 25;

    private Color color;
    private int length, location;

    //----------------------------------------------------------------------------------------------
    //  Crayon's characteristics.
    //----------------------------------------------------------------------------------------------
    public Crayon (Color color, int length, int position)
    {
        color = color;
        length = length;
        location = position;
    }

    //----------------------------------------------------------------------------------------------
    //  Draws crayon.
    //----------------------------------------------------------------------------------------------
    public void draw (Graphics page)
    {
        page.setColor (color);
        page.fillRect (location, BOTTOM - length, WIDTH, length);
    }
}

AND OBJECT/APPLET:

import java.awt.*;
import java.applet.*;

public class BoxOfCrayons extends Applet
{
    private final int APPLET_WIDTH = 350;
    private final int APPLET_HEIGHT = 300;


    Crayon c1 = new Crayon (Color.cyan, 100, 50);
    Crayon c2 = new Crayon (Color.blue, 75, 80);
    Crayon c3 = new Crayon (Color.gray, 100, 110);
    Crayon c4 = new Crayon (Color.green, 90, 140);
    Crayon c5 = new Crayon (Color.magenta, 82, 170);
    Crayon c6 = new Crayon (Color.orange, 100, 200);
    Crayon c7 = new Crayon (Color.pink, 70, 230);
    Crayon c8 = new Crayon (Color.red, 100, 260);



    //----------------------------------------------------------------------------------------------
    //  Draws crayon box.
    //----------------------------------------------------------------------------------------------
    public void paint (Graphics page)
    {
        setBackground (Color.white);
        setSize (APPLET_WIDTH, APPLET_HEIGHT);

        c1.draw (page);
        c2.draw (page);
        c3.draw (page);
        c4.draw (page);
        c5.draw (page);
        c6.draw (page);
        c7.draw (page);
        c8.draw (page);

        page.setColor (Color.yellow);
        page.fillRect (45, 150, 245, 90);

        page.setColor (Color.black);
        page.drawString ("Crayola", 150, 200);
    }
}

However, the yellow box is being drawn but not the crayons. PLEASE HELP!!

There should NOT be code in the paint method to set background and size in the paint() method.

Try debugging the code by printing out the values of all the variables used in the draw() method to see where the fillRect() method is doing its thing.

Would I just put that outside? It doesn't compile.
Do I need another loop?

Add just one println statement inside the draw() method (after line 25) that prints the values of all the variables used in the draw() method.

Alright I got it! Thanks!

Heya, so I think I found what's wrong and fixed it accordingly. So, in your Crayons class you have statements like color=color and length=length, but these statements are pretty reduntant. What you want to do is to do this.length and this.color in order to specify that the length you are referring to is the one in the parameter and the color you're specifying is the color in set in the parameter of the class.

commented: You are right - but after 6 years it's probably too late! +15
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.