last day (27 days later) » 

1:59 AM
hey man
 
hidi ho
 
hi
how are you?
long time no talk
 
k my question is how I can draw a line by just clicking
my code so far is
public class DrawLine extends JPanel {

int x;
int y;

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;
System.out.println("i am here");

g2d.drawLine(x, y, x + 100, y + 200);
// g2d.drawLine(200, 30, 30, 200);
// g2d.drawLine(30, 200, 200, 200);
// g2d.drawLine(200, 200, 30, 30);

}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}

public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
this code just draw line from 0 0 to 100 200
 
Well, obviously, you need two points, a start point and a end point. I'd personally use java.awt.Point, while either is null, you don't paint anything. You add a MouseListener to the DrawLine panel and on the mouseClicked event, you assign the resulting event Point to one of the two Points based on which one is currently null...
 
2:08 AM
so how the line will be drawn ?
my rapproche is not good ya?
my approach
 
Assuming that the points are not null g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
 
gotta
so I have to have to points
 
You could also use Line2D (docs.oracle.com/javase/7/docs/api/java/awt/geom/Line2D.html), something like d2d.draw(new Line2D.Double(p1, p2);
 
capture the x and y from those points
and draw the line
no need to paint
can I work on it and get back to you ?
 
You'd still need paintComponent, because thats how components get rendered
Possibly, got a meeting coming up :P
 
2:12 AM
so can you gimme a sample so i can follow it?
when should i use paintComponent ?
 
You should use paintComponent when ever you want to modify the way a component is painted...
 
as I can remember it is callback that willnot be called by me ya?
 
Yep, it will be called (indirectly) by the Event Dispatching Thread, when it pops a "repaint event" of the event queue, which is enqueue by the Repaint Manager...:P
 
is there any sample you can gimme so I can follow it through?
 
	import java.awt.Color;
	import java.awt.Dimension;
	import java.awt.EventQueue;
	import java.awt.Graphics;
	import java.awt.Graphics2D;
	import java.awt.Point;
	import java.awt.event.MouseAdapter;
	import java.awt.event.MouseEvent;
	import java.awt.geom.Line2D;
	import javax.swing.JFrame;
	import javax.swing.JPanel;
	import javax.swing.UIManager;
	import javax.swing.UnsupportedLookAndFeelException;

	public class Test {

		public static void main(String[] args) {
			new Test();
		}

		public Test() {
 
2:20 AM
k thank you
so I will keep this page open
if you get the chance plz come back
thanks
 
2:44 AM
how can I keep lines that have been drawn on my jframe?
 
Add each line to a List of some kind, iterate over this List when painting...
 
repainting you mean?
 
Inside paintComponent, iterate over the list and paint each instance of Line2D that it contains...
 
k lemme work on it
sorry it came little bit rude i am working on what you said and let you know what happened
 
2:51 AM
how about itreate through two pints ?
 
It' s a lot more complicated, as you might need to take into consideration odd number of points
 
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (line != null) {
g2d.draw(line);
} else if (startPoint != null) {
// g2d.drawOval(startPoint.x - 2, startPoint.y - 2, 4, 4);
if (mousePoint != null) {
List<Line> listOfLine = new ArrayList<>();
listOfLine.add(new Line(startPoint, mousePoint));
for(Line l: listOfLine)
g2d.drawLine(l.start.x, l.start.y, l.end.x, l.end.y);
System.out.println(mousePoint.x+ " " + mousePoint.y);
}

}
g2d.dispose();
what do you think about my paintComponent?
how can I make connection between 2dline and paintcomponent?
 
I think what you really want is something more like...
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Line2D;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
Remember, painting should paint the state, not make decisions about how to modify the state ;)
 
I think I get it plz let me to rectify my mistake and your last comment is very helpful
cool I change my code based on what you said and it works
right now I have to see how I can just have 3 lines
 
3:11 AM
Each time you want to add a new line, check it's size, if it's equal to 3, remove the element at position 0, this will allow you to add the last line to the List, or if you need to restrict it, just don't add anything...
 
please lemee to apply that
yaaaaaaaa can I hug you from here? lol
:p
can I ask couple of small question?
 
3:27 AM
sure
 
why here
} else {
mousePoint = null;
mousePoint assinged null?
 
I was using mousePoint as a "guide" to show where the new line might be created, once the new line was created, I didn't need it any more, so I made it null to stop the paintComponent method from trying to draw another "marker" line
 
so mousePoint is the end point?
when I get rid of it, my outcome is still the same
why?
 
Kind of, it's what would become the end point if the user clicked the mouse.
 
k next question is
 
3:31 AM
It's only really used as a guide, for me, the actual end point is the point at which the mouse is clicked a second time
 
i can draw just 3 lines but I still can draw a dot in my code
oh nice
I changed my code just for clicking
 
The dot is drawn when startPoint is not null, if the user can't add new lines, then don't allow startPoint to become not null
 
did you mean null?
 
No, I mean, don't allow to equal anything other than null (when you don't want the click point painted)
 
I cannot can you plase tell me where?
 
3:47 AM
This assumes you only ever want to have 3 lines and will stop allowing the user to add more...
    @Override
    public void mouseClicked(MouseEvent e) {
        Point p = e.getPoint();

        if (lines.size() < 3) {

            if (startPoint == null) {
                startPoint = p;
            } else {
                mousePoint = null;
                lines.add(new Line2D.Double(startPoint, p));
                startPoint = null;
            }

        }
        repaint();
    }
 
I swear I did the same
it keeps doing it
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
System.out.println(startPoint);
System.out.println(mousePoint);
if (startPoint == null) {
startPoint = p;
} else {
mousePoint = null;
if (lines.size() < 3) {
lines.add(new Line2D.Double(startPoint, p));

}
startPoint = p;
repaint();
}

if (startPoint != null) {
mousePoint = e.getPoint();
repaint();
}

}
 
But you're still allocating startPoint, because I checked for lines.size first and ignored everything when the size was 3 or greater
 
k got your point
the last question
can you exaplin why moustPOint and startPOint assign to null?
plz
 
 
1 hour later…
5:14 AM
mousePoint is set to null after a line is created because we no longer want to draw the "guide line" and startPoint is set to null so we know next time when the button is clicked that we are creating a new line...
 

  last day (27 days later) »