• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to draw a curve with the mouse pointer

 
pranjal alshi
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" i want to draw curve using mouseDragged, instead of line, i want a simple curve in java.
can anyone tell me how to write code for that?"
 
Piet Souris
Rancher
Posts: 1542
33
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pranjal,

are you familiar with JPanel, paintComponent, and MouseListeners?

For now, have a look at this java tutorial, where the author does a curve sketching program:

http://math.hws.edu/javanotes/source/chapter7/SimplePaint2.java
 
Campbell Ritchie
Sheriff
Posts: 51467
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
I split your post becuase it had no relationship with the thread you posted on.
 
pranjal alshi
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" thank you for your reply.. i know the link you have shared.
actually i am trying to draw a simple curve. means we draw something on panel.. it should draw curve. like we draw then something is drawn. like that simple curve should draw it on frame using java mouse Dragged
can you help me to write some hint for curve?"
 
Fred Kleinschmidt
Bartender
Posts: 417
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To draw a curve that follows the cursor you have to add a MouseMotionListener to your jpanel, then in the mouseDragged() method you need to keep track of all points that tyhe mouse moves over.

So perhaps your JPanel subclass would have an ArrayList<Point> that you add a new point every time the mouseDragged is called, then in your overridden paintComponent() method you need to draw line segments between each of the points in succession.
 
Piet Souris
Rancher
Posts: 1542
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@pranjal
Can you come up with a skeleton of your application? You need a JPanel, a MouseListener, a MouseMotionListener that gets activated whenever your JPanel is bemg clicked, and indeed an ArrayList<Point> that gets filled whenever there is a MousePressed event, or a MouseDragged event. Your JPanel should have a paintComponent that draws lines between adjacent points of the ArraYList.
Freds remakrs are very helpful in this respect.

So only a skeleton, to see how far you can get. We can then discuss and fill in the details, if you like.
 
pranjal alshi
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Thank you so much
actually i didn't use paintComponent method. i directly draw line work on lineto() method and used different line shapes. and i also didn't use arraylist.
for line draw, i used (last_x,last_y, x,y) which is in int. but not in arraylist. can i use same points to draw curve?
i am so sorry, i am new to java and i am asking basic doubt i suppose.
i am working on Paint application and have doubts.
 
Campbell Ritchie
Sheriff
Posts: 51467
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what you are trying to do. You started off by saying you want a curve, and now you want a line. The two are different.
 
pranjal alshi
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void lineto(int x, int y) {
     
    Graphics g = getGraphics();
   
   
    //Anti-aliasing drawing
    Graphics2D g2 = (Graphics2D)g;
 
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                           RenderingHints.VALUE_ANTIALIAS_ON);
      //g2.setStroke(new BasicStroke(10));
      
    g2.setColor(color); // Tell it what color to use
    //g2.fillOval( x, y, 1, 4 );
    if (pen_type==1){
    g2.fillOval( x, y, stroke_width, stroke_width );
    }  else if (pen_type==2) {
    g2.draw(new Line2D.Double(x, y, stroke_width, stroke_width));
    }
    else {
   
    g2.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    g2.drawLine(last_x, last_y,x,y);
   
      };
   
 
    moveto(x, y); // Save the current point
  }


see.. i have used this line shapes.. so in that i want one more shape that is curve.
 
Piet Souris
Rancher
Posts: 1542
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please explain what you are trying to do. You started off by saying you want a curve, and now you want a line. The two are different.

hi Campbell,

pranjal has two Points: the old one and the new one, coming from the MouseMotionListener, and he/she draws a line between those points. The curve originates since with the MML the two pioints are very close, so the effect is that of a real curve.
 
Piet Souris
Rancher
Posts: 1542
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pranjal alshi wrote:public void lineto(int x, int y) {(...)

see.. i have used this line shapes.. so in that i want one more shape that is curve.


Yes, I see. but doing this drawing directly, by getting a Graphics g from your JPanel, is actually not the way to go. Although it seems to work, it has one big drawback: draw a curve, and then resize your panel (i.e. drag one of the sides of your frame). Notice anything?

Then, your code is much more advanced than I thought (with BasicStrokes and using circles and antialiasing in some cases!) So it is not very useful to give a skeleton like I asked, since that would mean a big step back. Therefore: can you give us here the full code? We will then discuss what is needed to get a more permanent curve that doesn't vanish, and maybe we will add a 'save' option with a dedicated file format. Feel like it?

Greetings,
Piet
 
pranjal alshi
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 

Yes, I see. but doing this drawing directly, by getting a Graphics g from your JPanel, is actually not the way to go. Although it seems to work, it has one big drawback: draw a curve, and then resize your panel (i.e. drag one of the sides of your frame). Notice anything?

yes i know that drawback, i noticed.. when i resize my JFrame, all drawing gets disappear.
 
Piet Souris
Rancher
Posts: 1542
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I get back later this afternoon,  I'll come up with what I think is an appropriate 'painComponent', and some additions that I think will work. In essence, the line to code will become part of the paintComponent. Until then!
 
Campbell Ritchie
Sheriff
Posts: 51467
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:. . . . hi Campbell,

pranjal has two Points . . . .
Aaaaaaaaaaaaaaaaaaaaaaah! Thank you for the explanation.

I still think it ought to be done in the paintComponent method, or a method called from the paintComponent method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic