-
Allow ovals to move independently from each other
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.
Similar Threads
-
By pwnedjoo in forum Java
Replies: 42
Last Post: 05-05-2005, 05:52 PM
-
By ziphnor in forum VB Classic
Replies: 1
Last Post: 03-14-2005, 02:15 AM
-
Replies: 2
Last Post: 11-12-2001, 09:46 AM
-
By Nida in forum Enterprise
Replies: 0
Last Post: 04-10-2001, 05:25 AM
-
By joseph in forum VB Classic
Replies: 1
Last Post: 12-14-2000, 10:48 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks