I need to create a program where in you would draw a line, just like the paint application, but if you drag it, the lines you created would be deleted.It also displays the(x,y) where your mouse point at in the screen.

I'm starting on nothing.So anyone who can help me?
thanks!

Start with some kind of frame (eg JFrame), add a MouseListener, try something simple (eg draw a small rectangle wherever the mouse was clicked - you will need to override paintComponent(...) ) and work up from there. As you go you will (a) learn a lot and (b) discover exactly what additional knowledge/skill you need to complete the task. You can come back here for help as you progress.

Also while your are developing the code as James mentioned, add some print statements in the listeners to show the x,y values where the mouse was clicked and moved to. There are different points of reference: to the screen and to the component for example.

do you have he exact codes needed.
I don't now where to start it.

I have this code, but it draws the wrong lines.
It should be like this, when you click 2 points, a line would appear connecting those 2 points then clicking to another one would create a new line connecting those 3 and so on.


dragging it should allow you to delete those line at create new one.
The x,y coordinates of the location of the mouse is also needed.It should follow the mouse pointer.

i really need this one.
I have the codes here.
can you check on it?
Thanks!

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.ArrayList;

import javax.swing.JFrame;

import javax.swing.JPanel;



public class drawing extends JFrame implements MouseListener{

int positionX1 = 0;
int positionY1 = 0;



int positionX2 = 0;

int positionY2 = 0;

int maxLines = 1000;

int currentLines = 0;

int[][] drawLines = new int[maxLines][4];



ArrayList al = new ArrayList();

public drawing(){

setLayout(null);

JPanel p = new JPanel();

p.addMouseListener(this);

p.setBounds(0,0,800,600);

add(p);



setTitle("www.engineeringserver.com - Basic paint application");

setSize(800,600);

setResizable(false);

setLocationRelativeTo(null);

setVisible(true);

}



public static void main(String[] args){

drawing DL = new drawing();

}



public void paint(Graphics g){

super.paintComponents(g);

for(int i = 0; i < al.size(); i++){

g.drawLine(drawLines[i][0],drawLines[i][1],drawLines[i][2],drawLines[i][3]);

}

}



public void mouseClicked(MouseEvent e) {

}



public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {

drawLines[currentLines][0] = positionX1 = e.getX();

drawLines[currentLines][1] = positionY1 = e.getY();

}



public void mouseReleased(MouseEvent e) {
drawLines[currentLines][2] = positionX2 = e.getX();

drawLines[currentLines][3] = positionY2 = e.getY();

al.add(drawLines);

currentLines++;

repaint();

}

}

OK, well done, that looks like a decent start. But when you say "it draws the wrong lines" that doesn't help. You need to think very clearly about what you expect to happen, what actually happens, and why they are different.
Use this simple but powerful way to debug your code:
Put System.out.println(...) statements in many places in your code to see if the values you are getting are the ones you expect. For example, if the lines are in the wrong places, put something like this after line 100
System.out.println("X1= " + positionX1 + ", Y1= " + positionY1);
Does it print when you expect it to, and are they the values you expect? If so, move on and check X2,Y2 in the same way. If not, see what's wrong with them and why they are wrong.
Just try it. It becomes very easy very quickly.

>> OK, well done, that looks like a decent start.
It's a decent start copying this program: http://www.engineeringserver.com/forum/java+paint+application-t2342.0.html

Not much of a start showing any effort though. Perhaps the OP will post some more specific questions indicating that he understands the code he posted and what difficulties he is having in converting it to meed his needs.

Ah. I fell for that one, didn't I? Seems I need to turn up my scepticism filter a notch or two.
Good choice of name, dumbass. I guess you learned a lot from that.

He still has yet to get it working for his requirements, so perhaps there is some learning left to be had. Let's see if any effort is forthcoming.

sorry, used the wrong term.
What I mean was instead of the lines connecting each other, it goes the other way.it just creates the lines,but does not connect.

i also saw the program in this link:http://www.engineeringserver.com/for...n-t2342.0.html

but don't know where and how to insert it in my program since I'm a beginner in java.

it just creates the lines,but does not connect.

Does that mean that the ending x,y point for one line does NOT equal the beginning x,y point for another line?
Look in the code to see why the end of one line is not used for the beginning of the next line?

You should start by figuring out how it works. Then you can figure out how to adapt it.

So far you have only copied and pasted someone else's program here and demonstrated no effort whatsoever of your own.

really on rush.
But thanks to your help! :))

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.