Hi,

I need to able to draw several ovals (up to 30) and allow each oval to move independently
from the others when the user clicks/moves the mouse inside it.

I can draw and move one oval but when I add more, the ovals either move off the screen or do nothing.

I have tried to use arrays to store x and y co - ordinates as well as creating a class to handle the mouse
movement methods separately from the paint methods but so far nothing has worked.

Here is my code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MoveMultiGUI extends JFrame
{
public MoveMultiGUI()
{
super("GUI");
setSize(1500, 1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

Container contentArea = getContentPane();

MoveMultiDemo mmd = new MoveMultiDemo();
contentArea.add(mmd);
setVisible(true);
setContentPane(contentArea);
}

class MoveMultiDemo extends JComponent implements MouseListener, MouseMotionListener
{
public MoveMultiDemo()
{
addMouseListener(this);
addMouseMotionListener(this);
}

int x = 5;
int y = 5;

int mouseX, mouseY;

boolean mouseInNode = false;

public void mousePressed(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();

if( x < mouseX && mouseX < x+50 && y < mouseY && mouseY < y+50 )
{
mouseInNode = true;
}

e.consume();
}

public void mouseReleased(MouseEvent e)
{
mouseInNode = false;
e.consume();
}

public void mouseDragged(MouseEvent e)
{
int updateX = e.getX();
int updateY = e.getY();

if(mouseInNode)
{
x += updateX - mouseX;
y += updateY - mouseY;
}

mouseX = updateX;
mouseY = updateY;

repaint();
e.consume();
}

public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void updateLocation(MouseEvent e){}
public void mouseMoved(MouseEvent e){}

public void paint(Graphics g)
{
update(g);
}

public void update(Graphics g)
{
g.drawOval(x,y, 50, 50);
//works fine until x and y are updated and next node drawn
x += 70;
y += 70;

g.drawOval(x,y, 50, 50);

x += 70;
y += 70;

g.drawOval(x,y, 50, 50);

}

}

public static void main(String[] args)
{
MoveMultiGUI mmg = new MoveMultiGUI();
}
}

I have been stuck on this for a while so any help is greatly appreciated.