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!

Let the User draw a line to connect two rectangles

Status
Not open for further replies.

Mohadib

Programmer
Dec 6, 2004
1
US
I have a program that creates rectangles and the user will be adding the lines to link them together.

I draw the rectangles from database information. How do I get the lines to snap to the rectangles when they cross over them?
 
If you define your rectangles as components, you can write code to snap the lines to the rectangles in the mouseEntered method of the MouseListener interface.
 
If you store x,y coordinates,rectangle width and height for each rectangle, you have to calculate four coordinates of each rectangle and store the coordinates in 2d array.
The following procedures are used to use line to connect two rectangles:
After a mouse click(e.g right click), use MouseEvent.getX(), MouseEvent.getY() to compare the four coordinates of all the rectangles.
int myX = e.getX();
int myY = e.getY();
if (Math.abs(myX-coordinateLeftTopCorner.x)<=5) && Math.abs(myY-coordinateLeftTopCorner.y)<=5))
System.out.println("this point is closest");
// assume valid mouse click area is 10 pixels square for each point
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);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top