a_anis3000
asked on
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(ActionEven t evt)
{
if(evt.getSource() == draw)
repaint();
y += 10;
}
such that
public void actionPerformed(ActionEven t 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
picture-2.jpg
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(ActionEven
{
if(evt.getSource() == draw)
repaint();
y += 10;
}
such that
public void actionPerformed(ActionEven
{
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);
}
}
picture-1.jpgpicture-2.jpg
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
ASKER
>>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 !
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 !
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.