HELP w/Java!!!

X

X-War

Guest
Hi guys, I'm having difficulty creating this program, I'm a newbie at Java, so any help will be appreciated, I need to draw 10 randomly created circles of any diameter and fill the largest in red. Ok, I'm able to create the circles, but can't choose the largest one to fill it w/red. Here's the code if you want to take a look at it, thanks.


//******************************************************************************
// RedCirclePanel.java Author: X-War
//
// Creates 10 circles as graphical objects.
//******************************************************************************

import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;

public class RedCirclePanel extends JPanel
{

private final int MAX_CIRCLES = 10;
private final int MAX_RGB = 255;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 300;
private final int MAX_DIAMETER = 50;
private final int MAX_X = WINDOW_WIDTH - MAX_DIAMETER;
private final int MAX_Y = WINDOW_HEIGHT - MAX_DIAMETER;
private Random generator;

//-----------------------------------------------------------------------
// Sets up the drawing panel.
//-----------------------------------------------------------------------
public RedCirclePanel()
{
generator = new Random();

setBackground (Color.black);
setPreferredSize (new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
}

//-------------------------------------------------------------
// Draws circles of random diameter, position and color, paints red the largest one.
//-------------------------------------------------------------public void paintComponent(Graphics page)
{
super.paintComponent (page);

int x, y, radius, diameter;
int red, green, blue;


for (int count = 0; count < MAX_CIRCLES; count++)
{
red = generator.nextInt(MAX_RGB) + 1; //generates the red
green = generator.nextInt(MAX_RGB) + 1; //generates the green
blue = generator.nextInt(MAX_RGB) + 1; //generates the blue
x = generator.nextInt(MAX_X) + 1;
y = generator.nextInt(MAX_Y) + 1;
diameter = generator.nextInt(MAX_DIAMETER) + 1;
radius = (generator.nextInt(MAX_DIAMETER) + 1) /2;

if (radius == (MAX_DIAMETER/2)) //checks for the largest circle
{
page.setColor (Color.red);
}
else
{
page.setColor (new Color (red, green, blue));
page.fillOval (x, y, diameter, diameter);
}
}
}
}
 
Back
Top