Hey all!

I use the folowing class in my program to draw lines. However I also need to be able to delete them. So I need to add a mouselistener to the lines but I don't know how and whereto do this.. Can anybody help me with this?

Thanks!!

import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

import javax.swing.border.*;
import java.io.*;

  public class Drawing extends JComponent implements MouseListener, MouseMotionListener
   {
 
    //  public JPanel [] lines = new JPanel [50];
      public Drawing() 
      {   
           
           addMouseListener(this);
           addMouseMotionListener(this);

      }
        
      public void paint(Graphics g) 
      {
          for (int i = 0; i < count; i++)
          g.drawLine(ptStarts[i].x, ptStarts[i].y, ptEnds[i].x, ptEnds[i].y);
        
          
      }

      
       
     // MouseListener
      public void mousePressed(MouseEvent e)
        {
          if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0)
           {
             if (count < 50) 
              {
               drawing = true;
                start.setLocation(e.getX(), e.getY());
                end.setLocation(start);
                Graphics g = getGraphics();
                g.setXORMode(getBackground());
                g.drawLine(start.x, start.y, end.x, end.y);
                 g.dispose();

              //  setName("" + 100 + i);
               }
              else 
                 {
                 JOptionPane.showMessageDialog(this, "Only 50 lines can be drawn!", "No more", JOptionPane.ERROR_MESSAGE); 
                 }
            }
           else if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0)
             {
             count = 0;
             repaint();
             }                                         
    }
      
      public void mouseReleased(MouseEvent e) 
       {
          if (drawing) 
            {
           drawing = false;
           if (!start.equals(end)) 
               {
               ptStarts[count] = new Point(start);
                ptEnds[count] = new Point(end);
                count++;
                repaint();
               }
            }
         }
       
      public void mouseClicked(MouseEvent e) 
      {
        
      }
       public void mouseEntered(MouseEvent e) {
 
       }
      public void mouseExited(MouseEvent e) {}
      
    // MouseMotionListener
       public void mouseDragged(MouseEvent e) 
       {
         if (drawing) 
            {
            Graphics g = getGraphics();
            g.setXORMode(getBackground());
            g.drawLine(start.x, start.y, end.x, end.y);
            end.setLocation(e.getX(), e.getY());
            g.drawLine(start.x, start.y, end.x, end.y);
            g.dispose();
            }
        }
      
      public void mouseMoved(MouseEvent e) {}
      
      // protected member variables
      protected int count = 0;
      protected Point[] ptStarts = new Point[50];
      protected Point[] ptEnds = new Point[50];
      protected Point start = new Point(), end = new Point();
      protected boolean drawing = false;
     
       }

Recommended Answers

All 7 Replies

Sorry I wanted to delete my post, but there's no delete :s.

yeah that's true but I don't know how to keep track of the lines. So for example when you click on it the program knows what to delete and then it repaints it... so i could use an array of jpanels for example but then I don't know how to implement that to the class so the lines become jpanels.. I think graphics drawlines are bjects but i cant do e.getObject and you can do e.getCmponent. But even in order to do that I have to add the mouselistener to the lines and I don't know how to do that.. any suggestions??

thnx for your effort to go through it and give a hint :)

annick

Instead of using 2 arrays of Point objects, I suggest ArrayList<Line2D> class, so deleting particular line won't leave a 'hole' in array.
Initialization of object Lin2D's class looks like that:

Line2D line = new Line2D.Double( arguments );

You paint it in 'paint' method ( or paintComponent? ) by:

Graphics2D g2d = (Graphics2D) g;   // for drawing 2D objects

for( Line2D line  :  myArrayList )     // for all lines in myArrayList
        g2d.draw ( line );

How to delete a line? You implement mouse listener to Your component that stores lines, and in mousePressed/Clicked method You write smth like:

public void mousePressed( MouseEvent e )
{
      for( Line2D line  :  myArrayList )
              if( line.contains( e.getPoint() ) )
              {
                      myArrayList.remove( line );
                      repaint();
              }
}

Instead of using 2 arrays of Point objects, I suggest ArrayList<Line2D> class, so deleting particular line won't leave a 'hole' in array.
Initialization of object Lin2D's class looks like that:

Line2D line = new Line2D.Double( arguments );

You paint it in 'paint' method ( or paintComponent? ) by:

Graphics2D g2d = (Graphics2D) g;   // for drawing 2D objects

for( Line2D line  :  myArrayList )     // for all lines in myArrayList
        g2d.draw ( line );

How to delete a line? You implement mouse listener to Your component that stores lines, and in mousePressed/Clicked method You write smth like:

public void mousePressed( MouseEvent e )
{
      for( Line2D line  :  myArrayList )
              if( line.contains( e.getPoint() ) )
              {
                      myArrayList.remove( line );
                      repaint();
              }
}

thnx so much for your help! I rewrote it and now all I have to do is add mouselistener to the lines.. however should i do this seperate from the mouselistener on the component itself??
here's my code what i did and thnx again!!!

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.*;




  public class Draw extends JComponent implements MouseListener, MouseMotionListener
  {
   ArrayList<Line2D.Double> lines = new ArrayList<Line2D.Double>();
   //public delLine lines = new delLine();
   protected int count = 0;
   protected Point start = new Point(), end = new Point();
   protected boolean drawing = false;
   //protected Line2D.Double l = new Line2D.Double();
   delLine l = new delLine();


    public Draw()
    {
     
     addMouseListener(this);
     addMouseMotionListener(this);
     
    }


      public void paintComponent(Graphics g) 
      { 
       super.paintComponent(g);
       Graphics2D g2d = (Graphics2D)g;
       g2d.setColor(Color.black);
       for(int i = 0; i < lines.size();i++)
       {
        g2d.draw(lines.get(i));
       }
      }

       public void mousePressed(MouseEvent e)
        {
          if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0)
           {

               drawing = true;
                start.setLocation(e.getX(), e.getY());
                end.setLocation(start);
                Graphics g = getGraphics();
		Graphics g2d = (Graphics2D)g;
                g2d.setXORMode(getBackground());
		l.setLine(start.x, start.y, end.x, end.y);
                g2d.dispose();

            }
           else if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0)
             {
              repaint();
             }                                         
         }

       public void mouseReleased(MouseEvent e) 
       {
          if (drawing) 
            {
           drawing = false;
           if (!start.equals(end)) 
               {
                //l = new Line2D.Double();
		l=new delLine();
		lines.add(l);
                repaint();
               }
            }
         }
     
 	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e){}

 	public void mouseDragged(MouseEvent e) 
        {
            if (drawing) 
            {
             Graphics g = getGraphics();
	     Graphics2D g2d = (Graphics2D) g;
             g2d.setXORMode(getBackground());
             
     	     l.setLine(start.x, start.y, end.x, end.y);
     	     g2d.draw(l);
             end.setLocation(e.getX(), e.getY());
     	     l.setLine(start.x, start.y, end.x, end.y);
     	     lines.add(l);
	     g2d.draw(l);
             g2d.dispose();
            }
         }


 
  

   public void mouseMoved(MouseEvent e) {}
  
 
  
  }

heyy
so here's the class that should add mouselistener to the lines but it doesn't really work.. when i let it create a JPanel i can add linelisteners to it but for a line2D i can't..
can you help me with this? pleeeeaaaassseeeeeee I'm so close now!!!

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.*;

 public class delLine extends Line2D.Double implements MouseListener
 {
  
  

  

  public void delLine()
  {
   Line2D.Double listLine = new Line2D.Double();
   listLine.addMouseListener(this);

   JPanel p = new JPanel();
   p.addMouseListener(this);
   
  }

 	public void mouseClicked(MouseEvent e) 
	{ 
	  
 	}
	public void mouseEntered(MouseEvent e) 
	{
	  System.out.println("entered");
	}
        public void mouseExited(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
        public void mousePressed(MouseEvent e){}
   
 }

"however should i do this seperate from the mouselistener on the component itself??"

I might be misinterpreting what you said, but if not, you can define an inner class that implements MouseListener, then add a mouse listener of that inner class type to the component. http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html?page=2

edit: as for your latest post, unless your 2D.Double class extends Component or JComponent (or any other class that has the addMouseListener method), you won't be able to use that method with it.

"however should i do this seperate from the mouselistener on the component itself??"

I might be misinterpreting what you said, but if not, you can define an inner class that implements MouseListener, then add a mouse listener of that inner class type to the component. http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html?page=2

edit: as for your latest post, unless your 2D.Double class extends Component or JComponent (or any other class that has the addMouseListener method), you won't be able to use that method with it.

Ok I get what you mean :)
it still doesn't work though , now i did this:

public void mouseClicked(MouseEvent e) 
	{
          Point p = e.getPoint();
          System.out.println(p);
	  for(int i = 0; i < lines.size();i++)
          {
  	    
	    //if(lines.get(i).contains(p))
            if(lines.get(i).ptSegDist(p) == 0.0)
            {
		System.out.println("clicked");
                 lines.remove(i);
                 //lines.remove(lines.get(i));
			       
	    }
	    
          }
        

     repaint();

	}

the thing is that it does print out the points that are clicked but never "clicked" so the lines.get(i).contains(p) doesn't work I guess.... how come? does it only work if you add a mouselistener to the lines, because as you said it then needs to be extended from JPanel or something alike and that would mean I'd have to change the whole thing (well more or less...) I'm so close now!!!!
hope you can help me out again :) its been VERY useful! thnx a million!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.