Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I thought about decreasing my code, by making a function that does the same as 4 lines of code. But in order to do that, I will have to make an string array function that calls the image files. I want to make a function that does this;

Java
Image StartScreen1 = Toolkit.getDefaultToolkit().getImage("1.png");
g.drawImage(StartScreen1, 0, 0, FrameHeight + 25, FrameLength - 10, this);


The function would then look similar to this
Java
public void Image("" file){
String[] ImageNr = new String[30]
ImageNr[1] ="1.png"
ImageNr[2] ="2.png"
ImageNr[3] ="3.png"
// and so on....
}


The problem is I dont know how to properly make the function that will call in the images. Can someone please explain how to make a array function that calls files?

What I have tried:

I have tried to read about arrays and string arrays, etc but could not understand how to properly do it.
Posted
Updated 23-Mar-18 1:55am

1 solution

This looks like a continuation of Switch between pictures, without a white flash occurring in java.[^] where I already gave you a suggestion of what to do. All you need is a field that will contain the name of the currently selected image. Then in your selector code you choose the file name, store it in that field and call the refresh method to redraw the window. You can put all your file names in a list of some sort, as described in Trail: Collections (The Java™ Tutorials)[^]
 
Share this answer
 
Comments
Nillerh 23-Mar-18 8:25am    
Okay thanks, yes it is a contination of Switch between.... I had some problems with decreasing the code, and my teacher is on holiday...
Thanks for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900