0

The game is supposed to allow the user to click and place circles within the panel. If they overlap or are out of bounds, it's not supposed to draw a circle. The code I have so far allows the user to place circles, but when I attempt to overlap them it freezes. Can anyone please tell me what I'm doing wrong and point me in the right direction?

Here is the section of code so far that handles this:

protected void handleMouseClick(MouseEvent e) {
    clickX = e.getX();
    clickY = e.getY();

    disk = new Disk(clickX, clickY, radii[diskCount], DiskColor.values()[diskCount % 15]);
    disks[diskCount] = disk;
    for(int i = 0; i < disks.length; i++) {
        if(disks[i].overlaps(disk) == true) {
            diskCount++;
            repaint();
        }
        diskCount+=0;
        repaint();
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    I’d not add Disk to disks until you’ve ascertained that it won’t overlap - as it’s likely that the disk you’ve added will overlap itself - this could be creating an array out of bounds exception, but I don’t have enough information to be sure – MadProgrammer Oct 01 '17 at 21:38
  • I get a null pointer exception. It's supposed to generate circles of random color and size each time the user clicks within the panel. Prior to that, I don't believe a circle should be stored in the array. It should only store circles that are successfully placed. I don't know if this helps any. – J.Rhine Oct 01 '17 at 21:45
  • Using `disks.length` would allow you to address elements in the array which have not been assigned a value (ie are NULL) - generally speaking, a `ArrayList` would be a simpler choice – MadProgrammer Oct 01 '17 at 21:49
  • Thank you very much. I think that gives me an idea of what the problem may be. I will try to fiddle with it more and see if I can figure it out. – J.Rhine Oct 01 '17 at 21:55

0 Answers0