My panel's paintcomponent method draws a line based on the initial click and the current position as the mouse is dragged. Releasing the mouse leaves one line from the mouse press location to the mouse released location. The problem is. on the next click/drag, the old line disappears when the paint method is called again. Is there a way to save the old lines other than keeping them in a list and constantly redrawing them in the paintcomponent paint?

Recommended Answers

All 12 Replies

Member Avatar for hfx642

Are you calling repaint() after every action?
Not if you call repaint() all the time.

The problem is. on the next click/drag, the old line disappears when the paint method is called again

You need to save old lines in a list that the paint method can access and use to draw the old lines as well as the current line.

There is no magic. If you want old lines, you must save them somehow.

Well I have to change the color of the lines based on user clicks throughout the application, if I make new components (that are just thick line segments), can I make the application only repaint that component by calling it's paintComponent method as opposed to repainting the whole panel with a slight color change?

5 vectors could solve this problem (though there probably is a more efficient way).

Make one for the start x and one for the start y. Repeat for the end positions.
make one for the color

use an advanced for loop to set the color and then print the lines that the user drew.

That's what I was using, well point vectors, but I was thinking, what if I make a new component, that is basically a thick line that is can be resized in the initial drag (effectively serving the same function as drawing a line) and then call it's repaint method when I want to change it's color. Wouldnt that be more efficient than redrawing the whole panel each color change? And allow me to store the line as a whole? All these vectors for one line seems messy

Problem is, I have no idea how...

I think this will solve your problem for making a list.....
you dont have to make a list, use Graphics object for creating graphics

Below is the code shown

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class SampleLinePaint extends JPanel 
{   	JFrame frame; 
  	Graphics g2d;
	BufferedImage img;

		public SampleLinePaint()
		{     

    	 	img=new BufferedImage(500,500, BufferedImage.TYPE_INT_ARGB);
	 	g2d=img.createGraphics();
         	g2d.setColor(Color.BLACK);
     	 	MyMouseListener m=new MyMouseListener();
     	 	addMouseListener(m);
     	
    
		}

    
		void iniGUI()
		{ frame = new JFrame("Paint");
  		  frame.getContentPane().add(this);
                    frame.setSize(500,500);
        	           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                 }


 
		public void paint(Graphics g)   
		{
		g.drawImage(img,0,0,null);
		}
    
  
	class MyMouseListener extends MouseAdapter
	{
	int x1=-1,x2=-1,y1=-1,y2=-1;


			public  void mousePressed(MouseEvent me)
			{

				x1=me.getX();
				y1=me.getY();


			}

			public  void mouseReleased(MouseEvent me)
			{


				g2d.drawLine(x1, y1 ,me.getX(),me.getY());
				repaint();

			}

         }



public static void main(String []args)
{
SampleLinePaint p=new SampleLinePaint();
p.iniGUI();

}


}

Create a new class for a line. It would contain the x,y of the end points, the color and the width and any other property you want for the line. It would have a drawMe method that takes a Graphics object and does the drawing.
Objects of this class could be saved in a list that would be iterated over by the paintComponent method.

@efxee Override paintComponent for Swing components. See line 34

@sirlink99 Your design isn't OOP. 5 Vectors???

Create a new class for a line. It would contain the x,y of the end points, the color and the width and any other property you want for the line. It would have a drawMe method that takes a Graphics object and does the drawing.
Objects of this class could be saved in a list that would be iterated over by the paintComponent method.

@efxee Override paintComponent for Swing components. See line 34

@sirlink99 Your design isn't OOP. 5 Vectors???

Well that's what I was describing in my last post, but what do I extend to make this class? If i extend a component, would each object have its own paintComponent method so I could update individual objects and not the whole panel they are contained in each time I need to change the color of one of them?

what do I extend to make this class

You don't extend any class. The new class contains the line's properties and a drawMe method.
The paintComponent method for panel that the lines are being drawn on would call the drawMe(graphics) method for each of the saved lines. The drawMe method would draw the line on the panel referenced by the graphics object.

@NormR1 ok to use paintComponent

but i can't understand why to use list when my class is actually creating graphics(createGraphics() line 15) every time, why to save lines??... can you please explain??

waiting for your reply...

@efxee
I was answering the OPs question about this:

The problem is. on the next click/drag, the old line disappears when the paint method is called again.

I did not look at your code.

You don't extend any class. The new class contains the line's properties and a drawMe method.
The paintComponent method for panel that the lines are being drawn on would call the drawMe(graphics) method for each of the saved lines. The drawMe method would draw the line on the panel referenced by the graphics object.

Ahh ok, I misunderstood your earlier post, and I didn't have a good understanding if how graphics objects work. Thank you! I'll try this as soon as I can

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.