2010-03-28 73 views
0

我创建了一个程序,以网格布局格式放置随机图像。 网格布局的大小是6 x 6 = 36. 只有10个填充了图像(每个图像都不同),其余都是空的。如何保存文件并阅读

alt text http://freeimagehosting.net/uploads/bfb7e85f63.jpg

我怎样才能将其保存到一个文件,看了一遍,所以它会在网格上显示与相同的位置相同的图像?

这里是我用来保存图片代码:

//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); 

} 
+0

我们无法在您的问题中看到图像。尝试改变它链接到http://www.freeimagehosting.net/uploads/bfb7e85f63.jpg – 2010-03-28 12:58:54

+0

对不起。点击“代码”按钮旁边的图片插入按钮,然后将其粘贴到弹出的框中。 – 2010-03-28 13:03:39

回答

1

哎呦,误解你的问题。

我会将每个网格值链接到指定图片/文件的数组的索引。当保存这个设置时,用与网格关联的值保存一个新的数组(其中26个应该为空,对吧?)当文件打开时,最初有程序从数组中读取,如果数组完全为空随机化。

- 为网格创建一个foreach循环,如果它们为空,则将数组值设置为null,否则将值设置为与其关联的图像。使用pdraw.checkImage();

+0

没错,但它看起来应该让一些单元空白,只填充其中的10个。 – 2010-03-28 13:10:05

+0

感谢Mike,但我只想填充10张图片。如何将其保存到文件中,并在未来再次打开文件以在网格上查看相同的图像和位置? – Jessy 2010-03-28 13:10:14

+0

马特@是的,其中只有10个......剩下的就是空标签。 – Jessy 2010-03-28 13:11:56

2

在您的代码中,是类java.util.Randomrand?如果是这样,你可以自己“种下”,然后将种子值保存到文本文件中。对于任何给定的种子,(伪)随机数发生器将以相同的顺序产生相同的“随机”数字。所以:

Random rand = null; 

然后,创建一个新的 “种子”,并将其保存到一个文件:

long seed = System.currentTimeMillis(); 
rand = new Random(seed); 
try { 
    BufferedWriter writer = new BufferedWriter(new FileWriter("whatever.txt")); 
    writer.write(Long.toString(seed)); 
    writer.close(); 
} catch(IOException e) { 
    e.printStackTrace(); 
} 

或阅读以前保存的值回:

long seed = 0; 
try { 
    BufferedReader reader = new BufferedReader(new FileReader("whatever.txt")); 
    seed = Long.parseLong(reader.readLine()); 
    reader.close(); 
} catch(IOException e) { 
    e.printStackTrace(); 
} 
rand = new Random(seed); 

待办事项该代码不检查以确保文件退出,或者文件不为空,或者该文件不包含除有效数字之外的内容等...

1

那么你必须保存一个包含你所需要的所有信息的配置文件。你可以使用一个属性文件。所以你循环访问你的网格,每次你找到一个带有图像的单元格时,你都会保存索引和文件名。然后,当您重新加载程序时,您将遍历所有可能的属性值以查找存在的属性值,然后获取图像的文件名,读取图像并将其加载到单元格中。