I am having difficulties drawing all images in a ArrayList to the canvas.
I was first able to correctly draw all the images using just one path name for all images in the code below:
private ArrayList<Module> list;
//... some code loading in modules
public void loadModuleImages() {
addModuleImage = new Image("images/Plain.jpg");
addModuleImage.setVisible(true);
addModuleImage.setVisible(true);
addModuleElement = ImageElement.as(addModuleImage.getElement());
addModuleImage.addLoadHandler( new LoadHandler(){
public void onLoad(final LoadEvent event){
for(int i = 1; i<=list.size()+1;i++){
context.drawImage(addModuleElement, list.get(i-1).getX(),list.get(i-1).getY());
}
}
});
canvas.setVisible(true);
addModuleImage.setVisible(false);
RootPanel.get().add(addModuleImage);
}
Now when I start to use an ArrayList to display all the images I end up drawing only the last image to the screen. Using the code below:
public void loadModuleImages() {
final ArrayList<Image> images = new ArrayList<Image>();
final ArrayList<ImageElement> imagesE = new ArrayList<ImageElement>();
for(int i = 1; i<=list.size()+1;i++){
images.add(new Image(list.get(i-1).getImageName()));
x = list.get(i-1).getX();
y = list.get(i-1).getY();
images.get(i-1).setVisible(true);
imagesE.add(ImageElement.as(images.get(i-1).getElement()));
addModuleElement = imagesE.get(i-1);
images.get(i-1).addLoadHandler( new LoadHandler(){
public void onLoad(final LoadEvent event){
context.drawImage(addModuleElement, x, y);
}
});
canvas.setVisible(true);
images.get(i-1).setVisible(false);
RootPanel.get().add(images.get(i-1));
}
}
The Module class contains accessor methods for the x position (getX()),y position getY(), and image name (getImageName()). For debugging reasons all the images are all "images/Plain.jpg". It appears something in my loop is making the last image get drawn over and over, not switching to the next image.
Please help me figure out how to correctly load all the images or any suggestions about a different way to approach my problem. Thank you