Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Simple GUI applicataion problem

I am doing some problems for my java class, and I am stuck on this one. I cannot see what is wrong with my code. The assignment is to create an array which holds the properties of 20 different circles, of random x,y coordinates and random radius. Then, if one circle overlaps any part of another, that circle is cyan colored. Otherwise, the circle is black colored.

The problem is, regardless of whether or not a circle overlaps, it is always cyan colored.

Here is the code, which is seperated into the main code (CirclePanel), and a GUI loader (Circles)

Circles.java:

import javax.swing.JFrame;

public class Circles
{
//-------------------
// Creates and displays the GUI where the circles where be drawn
//-------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Circles");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

CirclePanel panel = new CirclePanel();

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

CirclePanel.java

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

public class CirclePanel extends JPanel
{
private final int NUMofCIRCLES = 20;
private final int MAXX = 800;
private final int MAXY = 640;
private final int MAXRADIUS = 65;

double lengthx, lengthy, sumOfRadii;

private Random generator;

private int[][] circleArray = new int[NUMofCIRCLES][4];

//-------------------
// Constructor: sets up the basic characteristics of this panel.
//-------------------
public CirclePanel()
{
setBackground (Color.white);
setPreferredSize (new Dimension(MAXX, MAXY));

generator = new Random();
}

public void paintComponent (Graphics page)
{
super.paintComponent(page);

// Fill 2D array CircleArray with properties of each circle
for (int count = 0; count < NUMofCIRCLES; count++)
{
circleArray[count][0] = generator.nextInt(MAXX - MAXRADIUS); // [0] is x coordinate
circleArray[count][1] = generator.nextInt(MAXY - MAXRADIUS); // [1] is y coordinate
circleArray[count][2] = generator.nextInt(MAXRADIUS); // [2] is length of radius
}

// Draw the circles one at a time
for (int count = 0; count < NUMofCIRCLES; count++)
{
boolean overlaps = false;

// Checks for overlap. If the distance between the center points, sqrt((y-y0)^2 + (x-x0)^2)
// is less than the sum of the radii, than the circle does overlap, and fillcolor is cyan.
for (int count2 = 0; count2 < NUMofCIRCLES; count2++)
{
lengthx = circleArray[count2][0] - circleArray[count][0];
lengthy = circleArray[count2][1] - circleArray[count][1];
sumOfRadii = circleArray[count2][2] + circleArray[count][2];

if (overlaps == false)
{
if (Math.sqrt((lengthx*lengthx) + (lengthy*lengthy)) < sumOfRadii)
{
overlaps = true;
}
}
}

int sidelength = circleArray[count][2]*2;

if (overlaps == true)
page.setColor(Color.cyan);
if (overlaps == false)
page.setColor(Color.black);

page.fillOval (circleArray[count][0], circleArray[count][1], sidelength, sidelength);
}
}
}
[3580 byte] By [gabrielr] at [2007-11-11 7:50:54]
# 1 Re: Simple GUI applicataion problem
Well, right off the bat I'd like to say that I wouldn't recommend using
a two dimensional array. Think OOP. I would create a Circle class, then
have an array of that:
class Circle() {
Point location;
int radius;

public Circle(Point location, int radius) {
this.location = location;
this.radius = radius;
}

public Circle(int x, int y, int radius) {
this(new Point(x, y), radius);
}

public int getX() {
return location.x;
}

public int getY() {
return location.y;
}

public Point getLocation() {
return location;
}

public int getRadius() {
return radius;
}
}

Also, don't stutter. You're doing this:
if (overlaps == true) {
}
if (overlaps == false) {
}
There's no need for that.
if (overlaps) {
}
if (!overlaps) {
}
or even better:
if (overlaps) {
} else {
}

I don't really have time to look at your calculations right now, but in case
you don't know, the x and y coordinates of the circle are the top left,
not the center.
destin at 2007-11-11 22:37:13 >