How to save file and read

Posted by Jessy on Stack Overflow See other posts from Stack Overflow or by Jessy
Published on 2010-03-28T12:52:08Z Indexed on 2010/03/28 13:03 UTC
Read the original article Hit count: 310

Filed under:
|
|
|

Hello everyone,

I create a program that place random image in grid layout format. The size of the grid layout is 6 x 6 = 36. Only 10 were filled with images (each image was different) and the rest were empty.

freeimagehosting.net/uploads/bfb7e85f63.jpg

How can I save it to a file and read it again, so it will display the same images with same placement on the grid?

Here is the code that I used to save the images:

//image file
String []arrPic = {"pic1.jpg","pic2.jpg","pic3.jpg","pic4.jpg","pic5.jpg","pic6.jpg","pic7.jpg","pic8.jpg","pic9.jpg","pic10.jpg",,"pic11.jpg","pic12.jpg","pic13.jpg"};

ArrayList<String> pictures = new ArrayList<String>(Arrays.asList(arrPic));

ArrayList<String> file = new ArrayList<String>();    

JPanel pDraw = new JPanel(new GridLayout(6,6,2,2));
...

//fill all grids with empty label
for (int i =0; i<(6*6); i++){   
   JLabel lbl = new JLabel("");
   pDraw.add(lbl);  
}
...

//Choose random box to be filled with images
for(int i=0; i<10; i++){ 
   Boolean number = true;
   while(number){
   int n = rand.nextInt(35); 
   if(!(arraylist.contains(n)))
     number = false;
   arraylist.add(n);
}

//fill the grids with images
for(int i=0; i<arraylist.size(); i++){

   //select random image from arraylist
   int index = rand.nextInt (pictures.size());
   String fileName = (String) pictures.get(index );

   //find the image file
   icon = createImageIcon(fileName);   

   //save the file in a new file
   file.add(fileName);

   //rescaled the image      
   int x = rand.nextInt(50)+50;
   int y = rand.nextInt(50)+50;

   Image image = icon.getImage().getScaledInstance(x,y,Image.SCALE_SMOOTH);         
   icon.setImage(image);

   //remove empty label and replace it with an image
   int one = (Integer) arraylist.get(i);
   pDraw.remove(one);                           
   final JLabel label;
   pDraw.add(label,one); 

}

© Stack Overflow or respective owner

Related posts about java

Related posts about save