Using mouseDragging events to move shapes painted from an array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Using mouseDragging events to move shapes painted from an array

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Posts
    1

    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

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    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

  1. Replies: 2
    Last Post: 04-15-2005, 10:06 PM
  2. Creating an array of shapes at run time
    By Jaime Hyland in forum VB Classic
    Replies: 2
    Last Post: 02-22-2002, 02: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center