Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mouse Events and Arrays

Status
Not open for further replies.

mfc2005

Programmer
Jul 25, 2005
8
GB
Hi,

I currently paint shapes to a canvas from an array and want to be able to move these shapes around the screen. Unfortunately I am unsure how to set up the MouseDragged event so that it redraws only the shape I want (Currently there is no link in the paint method to the mouseDragged variables), any ideas? the code is below.

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

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)
{

}

}


Cheers



 
// you have to use a class array to store the x,y coordinates of each point which is created by each mouse draw.
Use drawing with a pencil as an example. I use a pencil to start at point 0,0 (x=0,y=0, this is the first mouse click).
Then I draw to a point 100,200(x=100,y=200, this is the second mouse click)

for programming, i have two store the location of first click and second click.
Each time redraw, i have to clear the screen, draw a line from first click to second click.
 
The is my favourite Java Swing Application example
Code:
// this is a program of a user of this forum
// right click mean drag the point location
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Task1e extends JPanel 
    implements MouseMotionListener , MouseListener 
{
        final static int max=1000;
        Point[] arrayPoints= new Point[max];
        boolean pointSelected[] = new boolean[max];
        int ncount=0, startDragX=0, startDragY=0, endDragX=0, endDragY=0;
        boolean dragJustStart = false;
        
              
        public Task1e(){
         addMouseListener(this); addMouseMotionListener(this);
          }
         
        public void mouseClicked(MouseEvent event){}
        public void mouseEntered(MouseEvent event){}
        public void mouseExited(MouseEvent event){}
        public void mousePressed(MouseEvent e){ 
            
            if(e.isMetaDown()){}//right
            //if(e.isAltDown () )
            else{ if(ncount<arrayPoints.length)
                    {
                    if (!pointOccupy(e.getX(),e.getY(),false))
                       {
                        arrayPoints[ncount++]=new Point(e.getX(),e.getY());
                        //System.out.println(" "+arrayPoints[ncount-1]);
                       }
                    }
                }
        }
        public void mouseReleased(MouseEvent event)
               {
                if (dragJustStart)
                   {
                    dragJustStart = false;
                    if (!pointOccupy(endDragX, endDragY, false)) // drag a point to position without point
                       {
                        for (int i=0; i<ncount; i++)
                        {
                         if (pointSelected[i])
                            {
                             arrayPoints[i] = new Point(endDragX, endDragY);
                            }
                       
                        }
                       }
                    for (int i=0; i<ncount; i++)
                        {
                        pointSelected[i] = false;
                        }
                   }
                repaint();
               }
        public void mouseMoved(MouseEvent e){}
        public void mouseDragged(MouseEvent e){

                 if (!dragJustStart && e.getModifiers()==MouseEvent.META_MASK && pointOccupy(e.getX(),e.getY(),true))
                    {
                     dragJustStart = true;
                     startDragX = e.getX();
                     startDragY = e.getY();
                    }
               if (dragJustStart && e.getModifiers()==MouseEvent.META_MASK)
                  {
                   endDragX = e.getX();
                   endDragY = e.getY();
                  }
         

        }        
        
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            
            g.drawString("Index Count = "+ncount,10,20);
           
            for(int i=0; i<ncount;i++){
                g.drawRect(arrayPoints[i].x-3 ,arrayPoints[i].y-3,6,6); 
                g.drawString("Index = " +i+  " ["+arrayPoints[i].x+" , "+arrayPoints[i].y+"]"  , arrayPoints[i].x, arrayPoints[i].y-5);    
            }
            
               for(int i=0; i<ncount;i++){
                for(int j=i+1;j<ncount;j++){ // j=i+1
                                 
            //g.setColor(new Color((int)(Math.floor(Math.random()*0xffffff) )) );
            //System.out.print(".");
            //g.drawLine(0,0, 100,100);
        
            g.drawLine(arrayPoints[i].x,arrayPoints[i].y, arrayPoints[j].x,arrayPoints[j].y);
                
                }
            }
       }
        public boolean pointOccupy(int tempX, int tempY, boolean toSelectPoint)
               {
                for (int i=0; i<ncount; i++)
                    {
                     if (Math.abs(tempX-arrayPoints[i].x)<=5 && Math.abs(tempY-arrayPoints[i].y)<=5)
                        {
                         if (toSelectPoint)
                             pointSelected[i] = true;
                         return true;                         
                        }
                    }
                return false;
               }

  
      public static void main(String[] args) {
    JFrame f = new JFrame("Task1e");
    
    // Make the application exit when the window is closed.
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    f.setSize(300, 300);
    f.getContentPane(  ).add(new Task1e());
    f.setVisible(true);
    }
}
 
Thanks prosper,

The real problem I have is how to determine when I click on a shape that is the one I want to move. For example I have 4 rectangles on the screen I click on one and drag only that one to a new location. I suppose I need to define the rectangle shape as the mouse listening area to listen in, is this possible. If not how would one go about doing this?

Chris
 
you should try the program i post(The program is written by another user).
Use right mouse click to drag the point to a new location.

For your program in a simple approach and resource saving approach. After the user drag one a location on your screen, the location is compared to the location of all rectangle, if one of the rectanges is within effective drag range, pick that rectangle and assign it to a new location.

The program i post has many feature you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top