[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

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
0
a_anis3000
Asked:
a_anis3000
  • 2
2 Solutions
 
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
0
 
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 !
0
 
CEHJCommented:
There's a small demo at http://technojeeves.com/tech/stacker.jar (source inside) to run as



java -jar stacker.jar
0
 
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now