am able to draw different programs on swings using mouse drag.but if i draw one diagram the previous diagram is getting vanished. how to avoid that ? i want to have all the drawn diagrams on the screen

import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;



class draw1 extends JPanel
{

	private ArrayList<draw1> list=new ArrayList<draw1>();
	int x1,y1,x2,y2,s;
	public draw1()
	{
	setBackground(Color.CYAN);
	addMouseListener(new MouseAdapter() {
	public void mousePressed(MouseEvent m)
	{
  		x1=m.getX(); y1=m.getY();
		repaint();
	}
	public void mouseReleased(MouseEvent m)
	{  	x2=m.getX(); y2=m.getY();
                list.add(d1);
		repaint();
	}
	});
	addMouseMotionListener(new MouseMotionAdapter() {	
	public void mouseDragged(MouseEvent m)
	{       x2=m.getX();  y2=m.getY();
		repaint();
	}
	});
     }
	public void paint(Graphics g)
		{
		  super.paint(g);
			int l,b,x,y;
			l=Math.abs(x1-x2);
			b=Math.abs(y1-y2);
			x=Math.min(x1,x2);
			y=Math.min(y1,y2);
                

		if(s==2)
		{
		g.drawRect(x,y,l,b);
		}
		else if(s==3)
		{	
		g.drawOval(x,y,l,b);
		}
		else if(s==4)
		{
		g.drawLine(x1,y1,x2,y2);
		}
		else if(s==5)
		{
                        int width = (x2 - x1) / 2;
			int height = (y2 - y1) /2;

			g.drawLine( x1, y1+height, x1+width, y1);
			g.drawLine( x1+width, y1, x2, y1+height);
			g.drawLine( x2, y1+height, x1+width, y2);
			g.drawLine( x1+width, y2, x1, y1+height);
	
		}
		else if(s==6)
			{
                          int w=(x2-x1);
  			  int h=(y2-y1);
                          g.drawLine(x1+w/2,y1,x2,y2);
			  g.drawLine(x2,y2,x2-w,y2);
			  g.drawLine(x2-w,y2,x1+w/2,y1);
			}
}
}


public class all extends JFrame
{
       draw1 d=new draw1();	
       public all()
       {
        JPanel p=new JPanel();
        JButton b1=new JButton("Rectangle");
	JButton b2=new JButton("ellipse");
        JButton b3=new JButton("line");
        JButton b4=new JButton("rhombus");
        JButton b5=new JButton("triangle");
        p.add(b1);
	p.add(b2);
	p.add(b3);
	p.add(b4);
	p.add(b5);
	b1.setMnemonic('r');
	b2.setMnemonic('e');
	b3.setMnemonic('l');
     	b4.setMnemonic('h');
	b5.setMnemonic('t');
	add(p,BorderLayout.NORTH);
	add(d,BorderLayout.CENTER);
		
	b1.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent a)
 	   {
             d.s=2;
	   }
	 });
	b2.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent a)
 	   {
 		d.s=3;
	   }
	 });
	b3.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent a)
 	   {
		d.s=4;
	   }
	 });
	b4.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent a)
 	   {
		d.s=5;
	   }
	 });
b5.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent a)
 	   {
		d.s=6;
	   }
	 });
        
}

	public static void main(String args[])
	  {
 		all a=new all();
		a.setTitle("code");
		                 a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		a.setSize(500,400);
		a.setVisible(true);
		a.pack();
	 }
}
super.paint(g);

That may be clearing the old drawings. Remove it to see what happens.
Removing is not the solution as you will see.
You need to have a way to "remember" what was drawn previously so you can draw it again.
You could put all the values in a class object and put the different versions of those objects on a list. Then have the paint method get its instructions on what to draw from the list. This would allow you to change the order of what was drawn and to remove selected drawings.
Another way if you don't want to edit the drawings is to use a BufferedImage to draw the shapes on and then have the paint method draw the current version of the BufferedImage.

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.