Java selection: store images in jar or Base64 order?
I wrote a poker simulator / odds calculator On the GUI, there are some JCombo@R_271_2419 @The user can select the card
Now, I have images of these cards, and I also have images in Base64 format I try to show them in three different ways I wrote a small program for everyone for demonstration purposes
Here are three methods I have tried:
1). Store the image in a jar file:
Very straightforward The images go into the jar, and the Java code reads them from there After that, it creates a file that contains all of these JCombo@R_271_2419 @And display it:
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JCombo@R_271_2419@; import javax.swing.ImageIcon; public class ImagesFromJAR { private JFrame mainframe; public ImagesFromJAR () { ImageIcon[] cardslist = new ImageIcon[52]; for (int i = 0; i < 52; ++i) { cardslist[i] = new ImageIcon (getClass (). getResource ((this.getImagePath (i)))); } JCombo@R_271_2419@ cards@R_271_2419@ = new JCombo@R_271_2419@ (cardslist); cards@R_271_2419@.setSelectedIndex (0); cards@R_271_2419@.setEditable (false); this.mainframe = new JFrame (); this.mainframe.setDefaultCloSEOperation (JFrame.DISPOSE_ON_CLOSE); this.mainframe.setSize (150,150); this.mainframe.setLayout (null); cards@R_271_2419@.setBounds (20,20,55,45); this.mainframe.add (cards@R_271_2419@); this.mainframe.setVisible (true); } private String getImagePath (int index) { switch (index) { case 0: return "images/2c.gif"; case 1: return "images/3c.gif"; case 2: return "images/4c.gif"; case 3: return "images/5c.gif"; case 4: return "images/6c.gif"; case 5: return "images/7c.gif"; case 6: return "images/8c.gif"; case 7: return "images/9c.gif"; case 8: return "images/Tc.gif"; case 9: return "images/Jc.gif"; case 10: return "images/Qc.gif"; case 11: return "images/Kc.gif"; case 12: return "images/Ac.gif"; case 13: return "images/2d.gif"; case 14: return "images/3d.gif"; case 15: return "images/4d.gif"; ...................................... default: return null; } } public static void main (String[] args) { javax.swing.SwingUtilities.invokelater (new Runnable () { public void run () { ImagesFromJAR t = new ImagesFromJAR (); } }); } }
2). Store images in a text file in Base64 format:
I convert each image in Base64 format and store it in a text file The Java code reads the file and recreates the image by decoding the lines in the file After that, create and display JCombo@R_271_2419 @:
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JCombo@R_271_2419@; import javax.swing.ImageIcon; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.FileInputStream; import java.io.IOException; public class CodeFromFile { private JFrame mainframe; public CodeFromFile () { ImageIcon[] cardslist = new ImageIcon[52]; InputStreamReader in = null; BufferedReader buff; int index = -1; try { in = new InputStreamReader (new FileInputStream ("card.encodings.txt")); buff = new BufferedReader (in); String line; byte[] ba; while ((line = buff.readLine ()) != null) { if (line.length () < 10) { continue; } index++; ba = Base64.decode (line); cardslist[index] = new ImageIcon (ba); } } catch (IOException e) { e.printStackTrace (); } finally { if (in != null) { try { in.close (); } catch (IOException e2) {} } } JCombo@R_271_2419@ cards@R_271_2419@ = new JCombo@R_271_2419@ (cardslist); cards@R_271_2419@.setSelectedIndex (0); cards@R_271_2419@.setEditable (false); this.mainframe = new JFrame (); this.mainframe.setDefaultCloSEOperation (JFrame.DISPOSE_ON_CLOSE); this.mainframe.setSize (150,45); this.mainframe.add (cards@R_271_2419@); this.mainframe.setVisible (true); } public static void main (String[] args) { javax.swing.SwingUtilities.invokelater (new Runnable () { public void run () { CodeFromFile t = new CodeFromFile (); } }); } }
card. encodings. Txt is as follows:
2c: R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXKIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMQN9DEhgN5MBBA5OtzIHtgEGAJ9qpwLEvjsDAImtsTcIkMo3Ag4DmUAMztSbaajQM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QgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKJgwVS52hIQQWWaaGj4krAwAOCD+vKFIUDpWQpyEHQz5QL12umywMpbyaCAjCLzfOizgBjrYnbQu7yp+dJdwjG2XiGRIRADs=
.. It continues for each image / card
3). The third method is to store the image directly in the source code as a Base64 string like this:
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JCombo@R_271_2419@; import javax.swing.ImageIcon; public class CodeFromSource { private JFrame mainframe; public CodeFromSource () { ImageIcon[] cardslist = new ImageIcon[52]; byte[] ba; for (int i = 0; i < 52; ++i) { ba = Base64.decode (this.getImageCode (i)); cardslist[i] = new ImageIcon (ba); } JCombo@R_271_2419@ cards@R_271_2419@ = new JCombo@R_271_2419@ (cardslist); cards@R_271_2419@.setSelectedIndex (0); cards@R_271_2419@.setEditable (false); this.mainframe = new JFrame (); this.mainframe.setDefaultCloSEOperation (JFrame.DISPOSE_ON_CLOSE); this.mainframe.setSize (150,45); this.mainframe.add (cards@R_271_2419@); this.mainframe.setVisible (true); } private String getImageCode (int index) { switch (index) { case 0: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXKIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMQN9DEhgN5MBBA5OtzIHtgEGAJ9qpwLEvjsDAImtsTcIkMo3Ag4DmUAMztSbaajQMAmfCdig4aInDt3pfuyvQTXwMyt19ikiIQA7"; case 1: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABEALAAAAAAeACYAAAW/YBQBZGme6CmOQeu+cAwDrGzfLonvss4HiJ9vtxAkeMMbhCAQNHZJm6EpIEBw0ZiC2gzesq8Gl7r4ApTMcfPZO9sOairBBm4t40662yZWK8w7W1wGWHs3U01WhTx3eYs8DAJejzwJV1CGP5iamnWcM5kxD4gGDps4BQwBDgQDl3o4C6YtkX+AmhACtrCaD468PwqElDtLD6c8BmzENwllSKExCbsJx8A2CWNzt9hqk22f0OFQNeMxNCIp6uskIiEAOw=="; ......... default: return null; } } public static void main (String[] args) { javax.swing.SwingUtilities.invokelater (new Runnable () { public void run () { CodeFromSource t = new CodeFromSource (); } }); } }
(I have to delete some switch cases because there is a 30 K character limit on stackoverflow)
Each of these methods produces the same results, namely:
PS: Base64 class is just some utility classes for encoding / decoding Base64 information (you can find it in the link below)
If you want all the resources (pictures, source code, etc.), you can get them here: resources
Now we've cleared... What I want is to know which of the three methods is better What are everyone's strengths and weaknesses? Which one should I use? Why? I'm also talking about the possibility of jar file corruption
The only thing I don't care about is performance
Solution
>Is preferred because no transformations are involved, and your code can read them from the classpath This makes your application self - contained Corruption is not a problem because it makes the entire application useless - not just the image file > will involve runtime penalties for conversion (startup time and memory), and Alsop will introduce the configuration file. > As bad as 2, but not very flexible (you can't change the image without recompiling)