Need help for drawing lines using mouselistener in java
4 posters
Page 1 of 1
Need help for drawing lines using mouselistener in java
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 .if any body could hlp me providing some source code or link or atleast with some hints....
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 .if any body could hlp me providing some source code or link or atleast with some hints....
BIT0201-farhan- Alpha Release
- Course(s) :
- BIT
Blood Group : AB+
Posts : 45
Points : 95
Re: Need help for drawing lines using mouselistener in java
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.
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.
Re: Need help for drawing lines using mouselistener in java
yup vaia...i've completed it....i think the 2nd code had some compiling problem...but i could fix it... ....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... ..@ amit vai
we have idea bout many things..bt dn knw eny thing completely...jack of all trade,master of none... ..@ amit vai
BIT0201-farhan- Alpha Release
- Course(s) :
- BIT
Blood Group : AB+
Posts : 45
Points : 95
Re: Need help for drawing lines using mouselistener in java
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()?
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- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Need help for drawing lines using mouselistener in java
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
BIT0201-farhan- Alpha Release
- Course(s) :
- BIT
Blood Group : AB+
Posts : 45
Points : 95
Re: Need help for drawing lines using mouselistener in java
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??
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- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Need help for drawing lines using mouselistener in java
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- Alpha Release
- Course(s) :
- BIT
Blood Group : AB+
Posts : 45
Points : 95
Re: Need help for drawing lines using mouselistener in java
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 and It is not exactly good for reading either.
Re: Need help for drawing lines using mouselistener in java
ok vaia...i'll try to avoid them from now on....
BIT0201-farhan- Alpha Release
- Course(s) :
- BIT
Blood Group : AB+
Posts : 45
Points : 95
Re: Need help for drawing lines using mouselistener in java
Also the "..."
Keep up the activity here.
Keep up the activity here.
Re: Need help for drawing lines using mouselistener in java
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.
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..
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..
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford