2010-03-28 74 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

那麼你必須保存一個包含你所需要的所有信息的配置文件。你可以使用一個屬性文件。所以你循環訪問你的網格,每次你找到一個帶有圖像的單元格時,你都會保存索引和文件名。然後,當您重新加載程序時,您將遍歷所有可能的屬性值以查找存在的屬性值,然後獲取圖像的文件名,讀取圖像並將其加載到單元格中。