IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Need help for drawing lines using mouselistener in java

4 posters

Go down

Need help for drawing lines using mouselistener in java Empty Need help for drawing lines using mouselistener in java

Post by BIT0201-farhan Sun Oct 17, 2010 5:43 pm

hay guys,
um stuck in a problem.i need to draw lines between two points using mouse listener.i've been lookin for this in web.but no convincing findings Crying or Very sad .if any body could hlp me providing some source code or link or atleast with some hints.... Sad
BIT0201-farhan
BIT0201-farhan
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 45
Points : 95

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0112-Rokon Sun Oct 17, 2010 11:25 pm

did you study on painting?
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0122-Amit Sun Oct 17, 2010 11:41 pm

Sheesh..
These guys were only taught JOptionPane and some other basics at class, or so I have heard.

I am not sure, but they are trying to learn mouse listener before even drawing anything.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0201-farhan Mon Oct 18, 2010 1:21 am

yup vaia...i've completed it....i think the 2nd code had some compiling problem...but i could fix it... Very Happy ....nw i need your help...@ rokon vai

we have idea bout many things..bt dn knw eny thing completely...jack of all trade,master of none... Crying or Very sad ..@ amit vai
BIT0201-farhan
BIT0201-farhan
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 45
Points : 95

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0102-Mohaimin Mon Oct 18, 2010 1:55 am

I did things like that before... Its slightly complicated at your level which is not a big deal. The big deal is... Its tricky...

As Rokon said, you need to learn painting... I have given a post about painting in the section SE206.

You said that you have already covered painting. So, I need to know something more specific so that I can help you...
Do you have basic idea about paint(), paintComponent() and repaint()?


Last edited by BIT0102-Mohaimin on Mon Oct 18, 2010 1:56 am; edited 1 time in total (Reason for editing : I did some UGLY formating... Posts should look good... :P)
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0201-farhan Mon Oct 18, 2010 3:17 am

yup.....i've read bout those met in web....though nt fully cleared...hope that it'll be ok by time goes on and aftr some implementation Smile
BIT0201-farhan
BIT0201-farhan
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 45
Points : 95

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0102-Mohaimin Mon Oct 18, 2010 4:31 am

Ok... Here is some tips... May be you already know...

1. Anything you want to do with painting should be done in paint() or paintComponent() methods. Which should be selected? That depends on what you want to do. honestly... Neither I am very clear about that selection... I'll try to study that one.

2. paint()/paintComponent() should not be called directly anywhere inside your code. Rather, call repaint(). It will call the painting functions in correct sequence.

These tips are not directly related to your query. But I need to be sure that you know these before approaching farther. I'll try to help you solve the problem you asked next morning.

BTW... You are doing 29, right??
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0201-farhan Mon Oct 18, 2010 4:39 am

i knw da matters.u ststed them as comments in your code...BTw,um nt doing 29...we found it nt feasible for this smstr...may be we gnna do it in nxt smstr and my partner atish got dengu...pray for em...and um waiting for your help
BIT0201-farhan
BIT0201-farhan
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 45
Points : 95

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0122-Amit Mon Oct 18, 2010 4:44 am

BIT0201-farhan wrote:i knw da matters.u ststed them as comments in your code...BTw,um nt doing 29...we found it nt feasible for this smstr...may be we gnna do it in nxt smstr and my partner atish got dengu...pray for em...and um waiting for your help

SMS language ain't appreciated in forum dude Smile and It is not exactly good for reading either.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0201-farhan Mon Oct 18, 2010 5:33 am

ok vaia...i'll try to avoid them from now on....
BIT0201-farhan
BIT0201-farhan
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 45
Points : 95

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0122-Amit Mon Oct 18, 2010 7:25 pm

Also the "..."
Keep up the activity here.
rock on!
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by BIT0102-Mohaimin Tue Oct 19, 2010 4:42 am

OK... Here is a code for your aid... I am sure this is not all you need. But I hope it will help.
I told that the code will be tricky, well, it appeared not to be that tricky. Its really simple.

Code:

package paintimg.mousedrag;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

/*
 * First of all, lets create a paper on which we will draw. Its just a JPanel
 */
public class Paper extends JPanel {

   // These two Points hold the coordinates of the line to
   // draw
   private Point   from, to;

   public Paper() {
      // These two initializations are not necessary, but
      // I don't like to risk a NullPointerException
      from = new Point(-1, -1);
      to = new Point(-1, -1);

      Pencil pencil = new Pencil();

      // Take a look at the inner class Pencil bellow,
      // after that come back here again
      addMouseListener(pencil);
      addMouseMotionListener(pencil);

      // See, we have added both MouseListener and
      // MouseMotionListener. I think its clear why...
   }

   // Overriding paintComponent().
   @Override
   protected void paintComponent(Graphics g) {
      // You should always call super class's painting
      // method whenever you override them. Those methods
      // checks possible exceptions and returns from the
      // correct place. If you do not call the super class
      // method, there is a big possibility to experience
      // unexpected occurrences.
      super.paintComponent(g);

      // Well, draw the line
      g.drawLine(from.x, from.y, to.x, to.y);
   }

   public void setFrom(Point from) {
      this.from = from;
   }

   public void setTo(Point to) {
      this.to = to;
   }

   /**
    * MouseAdapter is a Class that implements
    * (blank) all methods of MouseListener and
    * MouseMotionListener. I did not implement
    * those two interfaces directly because that would
    * require to implement all 7 methods of the two
    * interfaces where 5 would be blank. Like
    * link MouseAdapter, Adapter to other
    * EventListener classes are also available.
    * Using adapters makes your program clean
    */
   class Pencil extends MouseAdapter {
      // mousePressed() is declared in
      // MouseListener
      @Override
      public void mousePressed(MouseEvent event) {
         // A mouse press indicates starting point of the
         // line you want to draw. So, setFrom().
         setFrom(event.getPoint());
      }

      // mouseDragged() is declared in MouseMotionListener
      @Override
      public void mouseDragged(MouseEvent event) {
         // When you drag to some place, the line should
         // be redrawn, So, repaint is called, which
         // calls paintComponent().
         setTo(event.getPoint());
         repaint();
      }

      // Now, let's back to Paper()
   }
}

Add this paper to a JFrame.
Please tell me until all your questions about this code is clear.

And, you didn't tell me what you are doing actually.. Razz
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Need help for drawing lines using mouselistener in java Empty Re: Need help for drawing lines using mouselistener in java

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum