-
Using mouseDragging events to move shapes painted from an array
I am attempting to add mouse dragging event to a canvas which allows users to drag a shape around the canvas.
The program reads in an SVG XML file parses it then adds shapes to an array, this is passed to the paint method of my 'MyCanvas' class which extends Canvas. From this I want to draw the shapes (rectangles, ellipses, lines) to the canvas allowing them to be moved when mouseDragged events are triggered (code below).
public class MyCanvas extends Canvas implements MouseMotionListener{
private ArrayList liList;
private ArrayList reList;
private int rectX;
private int rectY;
int re;
int li;
public MyCanvas()
{
Parser p = new Parser();
liList = p.getLineArray();
reList = p.getRectArray();
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
while(re<reList.size())
{
Shape r = (Shape)reList.get(re);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.blue);
g2.fill(r);
g2.draw(r);
re++;
}
while(li<liList.size())
{
Shape l = (Shape)liList.get(li);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.red);
g2.fill(l);
g2.draw(l);
li++;
}
}
public void mouseDragged(MouseEvent e)
{
int rx = e.getX();
int ry = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
}
Can anyone help me do this? or do I need to redesign my application? If I need to redesign any ideas on the direction I should take?
Thanks
-
The first thing I suggest is to put lots of println() statements in the code so you can see all the values as they are set and change!!!
Another useful thing is to put comments in the code to explain why you are doing something. That will help you to write code that does what you want and it will help me understand your thinking about some lines of code. I can see what the code does, but I don't always know what you were thinking and what you wanted to do when you wrote it.
For example, to move an object that you've drawn how do you determine the new x,y position for it based on the mouse's changing position?
Similar Threads
-
By kanakatam in forum Java
Replies: 2
Last Post: 04-15-2005, 09:06 PM
-
By Jaime Hyland in forum VB Classic
Replies: 2
Last Post: 02-22-2002, 01:05 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