drawing image in a cascading arrangment using for loop

in the below code the program will draw a 10$ bill image each time the draw button is clicked. Clicking the draw button multiple times result in the programming drawing a bunch of 10$ bills in cascading arragment. For instance of the draw button is clicked 6 times it will draw a stack of 6x 10$ bills as shown in (picture 1.jpg). The program source file (drawing.java) can be downloaded from below as well
        
http://www.megaupload.com/?d=K8M924OW

I want to change the program such that each time the button is clicked (one click) a stack is drawn. For instance clicking the draw button will produce a 6x bill stack. I tried to achieve this by adding a for loop to the following lines of code below

public void actionPerformed(ActionEvent evt)
      {
            if(evt.getSource() == draw)

            repaint();
            y += 10;


      }

such that

public void actionPerformed(ActionEvent evt)
      {
            if(evt.getSource() == draw)
            for (int i = 0; i < 6; i++)
            {
                  repaint();
                  y += 10;
            }


      }

in theory clicking the draw button should draw a 6x bill stack, oddly enough for some reason only the last bill in the stack is visible, while the others are not shown. In other words only a single displaced bill is drawn as illustrated by picture 2.jpg. The new modified program source file can be downloaded from below

http://www.megaupload.com/?d=ANWA41UQ

Any ideas why this is happening. The same problem occurred in similar programs

Thanks in advance
Ahmed Anis
      






      




import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class Drawing extends JFrame implements ActionListener
{
 
 
 
	Image img = Toolkit.getDefaultToolkit().getImage("10$.gif");
 
 
	int y = 100;
 
	JButton draw = new JButton("Draw");
 
	JPanel row1 = new JPanel();
	JPanel row2 = new JPanel();
 
	public Drawing()
	{
		Container container = getContentPane();
		container.setLayout(new GridLayout(2,1));
 
		row1.add(draw);
		container.add(row1);
		container.add(row2);
 
 
		draw.addActionListener(this);
	}
 
	public static void main(String[] arrg)
	{
		Drawing frame = new Drawing();
		frame.setSize(800,400);
		frame.setLocation(200,200);
		frame.setVisible(true);
	}
 
 
public void actionPerformed(ActionEvent evt)
	{
		if(evt.getSource() == draw)
 
		repaint();
		y += 10;
 
 
	}
 
 
		public void paint(Graphics g)
		{
			g.drawImage(img, 100, y, 150, 50, this);
		}
 
 
}

Open in new window

picture-1.jpg
picture-2.jpg
a_anis3000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
All you're doing in your code is to redraw the same image in a different position, each time erasing the previous one. You need to paint all the images at once in one invocation of paint
a_anis3000Author Commented:
>>each time erasing the previous one<<<

But before the change (before the addition of the for-loop) I can somehow draw as many stack of bills as desired without previous drawings being erased !
CEHJCommented:
There's a small demo at http://technojeeves.com/tech/stacker.jar (source inside) to run as



java -jar stacker.jar
dnunes_brCommented:
To correctly do what you want, you have to do this:



public void actionPerformed(ActionEvent evt)
        {
                if(evt.getSource() == draw){
                	for(int idx=0;idx < 6;idx++){
                		getGraphics().drawImage(img, 100, y, 150, 50, null);
		                y += 10;
                	}
                	
                }
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.