• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing lines using mouse drag

 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a simple paint brush program that lets the user draw a line on jframe as mouse is dragged on the jframe area. I am making this program in more than one packages and classes. Line is drawn as mouse is dragged on the jframe area. Problem is that when i drag mouse on the jframe area for drawing a line, the line is not persistent, it is drawn as mouse is dragged and then at the same time it disappears. Actually it appears as a small line following the mouse pointer. I have tried different things but nothing seemed to work. Help needed to find the problem. Below is the code i have written so far.

Gui package, class gui


Logic Package, class Draw


Main class

 
Fred Kleinschmidt
Bartender
Posts: 453
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Draw class needs to keep track of every line segment drawn, so that the paintComponent() method can draw ALL of them.
As it stands now, your paintComponent() method draws only the most recent segment (from startPoint to endpoint).
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Custom Painting Approaches.

It shows the two common ways to do this:

1. Store objects to paint in an ArrayList and then iterate through the ArrayList each time the paintComponent() method is invoked
2. Paint directly onto a BufferedImage

The example code shows how to draw Rectangles, but the concept would be the same for lines.
 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java so i don't know how to do what you are saying. if you can show me how to do it by making changes in Draw class, that would really be helpful.
Actually if i remove super.paintComponent(g) from the paintComponent method, the line is drawn as i want it to be drawn but it creates other problems, for example it duplicates the JMenuBar below the original JMenuBar.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked at your code in any detail, but your Draw object is not storing much state: the coordinates of two points only. If you want the user to be able to draw multiple line segments, then your Draw object must store the coordinates of each segment (and also its color, if you plan to implement multiple colors). Then its paintComponent() method must draw all the line segments that you want the user to see, not just the most recent one.

Swing's painting system has no "memory" of what has already been drawn. If you want something to remain visible, then you have to draw it every time paintComponent() is called.


Also, while I'm here, let me mention two things:
1) Your class gui starts with a lower case letter, and your package Gui starts with an upper case letter. This is backwards from standard Java naming conventions.
2) The work your main() method is doing should happen inside a call to invokeLater(). If you're using java8 this could be as simple as If you're using java7 or earlier, then the syntax is more complex.

[edit: Wow, three replies on this thread came in at exactly the same time. And one reply beat the others by 7 minutes. I truly thought I was making the first reply. But, since the three of us are essentially giving you the same advice, that should tell you something.]
 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried the arraylist method but the problem is still the same.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yousaf khan wrote:I have tried the arraylist method but the problem is still the same.


Where? I don't see any code using an ArrayList?

Did you download the code for the link I gave you? Did you test the code? Did it work?

That code proves the concept does work. If your code doesn't work then you are doing something wrong. Compare your code with the working code to see what the difference is.

This is what problem solving is all about.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!