New Posts  All Forums:Forum Nav:

Crayon java help

post #1 of 3
Thread Starter 
I created an applet that shows a box with crayons. I'm trying to set crayons in different colors. I manage to generate random colors for the crayons, but all the crayons ended up the same color. What I'm trying to do is to make each crayon generate in different colors. But, all my results ended in errors. Is there a way to make each crayons appear in different colors? Thanks to those who can help me. I'm also using java for this.

Here's what I put before I added things that ended up in errors.

import java.awt.*;
import java.util.Random;
public class BoxCrayons
{
private int baseX;//center of the crayon
private int baseY;//bottom of the crayon
private Color color;//color of the crayon
private int height;
int colors;



public BoxCrayons (int center, int bottom, Color shade, int size)
{
baseX=center;
baseY=bottom;
color=shade;
height=size;
}

public void draw (Graphics page)
{
Random generator = new Random();

colors=generator.nextInt(4)+1;
if(colors==1)
{
page.setColor(Color.green);
}else if (colors==2)
{
page.setColor(Color.blue);
}else if (colors==3)
{
page.setColor(Color.red);
}else
{
page.setColor(Color.orange);
}

int top = baseY-height;

page.fillRect(baseX-150,top-5, baseX-200, baseY);
page.fillRect(baseX-96,top-5, baseX-200, baseY);
page.fillRect(baseX-42,top-5, baseX-200,baseY);
page.fillRect(baseX+12,top-5, baseX-200,baseY);

page.setColor(Color.yellow);
page.fillRect(95, 105, 220, 170);
}
}
post #2 of 3
Hello, welcome to OCN. Please fill out your system's specs and add more info about you
You code doesn't look very good, sorry. Are you new to Java?
The problem is you generate only one random color which you then you use to draw all the crayons. The solution is to simply create a for and generate a new random color every time you draw a crayon.
post #3 of 3
Thread Starter 
Ah I see, yes I'm new to Java lol but thanks for telling me on what I should do.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming