Hi, I'm new to java, but I am working on the startings of a speed reading application... The application currently inputs a text file, displays it in a jtextpane, highlights the first sentence letter by letter in a for loop until a period is found, then truncates that sentence from the jtextpane, finally starting the highlighting process over with the next sentence as the new first sentence of the jtextpane. Every time the application restarts highlighting a sentence it paints the background of the jtextpane with an image (successively painting a new image from a list of images stored in a folder on my hard drive).
So you kind of get an automated reading experience, that keeps highlighting and chopping off a sentence, to avoid the need for scrolling through the jtextpane, all the while repainting images behind the text in the jtextpane.
Apparently I don't have a clue what I'm doing when it comes to allowing java to release either the graphics cache, or the image cache, or perhaps the jtextpane paint cache, because every time the application repaints the jtextpane with a new image, the memory usage goes up and the last image cache is never released from memory.
The only image related sections of the code are: the public static declarations of (graphics g1, image img);
27. public static Image img; 28. 29. public static Graphics g1;
The overridden: protected void paintComponent(Graphics g);
176. private static class MyTextPane extends JTextPane 177. { 178. public MyTextPane() 179. { 180. super(); 181. setText("Hello World"); 182. setOpaque(false); 183. setBackground(new Color(0,0,0,0)); 184. } 185. 186. @Override 187. protected void paintComponent(Graphics g) 188. { 189. if(g1 == null) 190. { 191. g1 = g; 192. } 193. img = null; 194. img = Toolkit.getDefaultToolkit().getImage("C:\\images\\" + i1 + ".jpg"); 195. g.drawImage(img, 0, 0, this); 196. super.paintComponent(g); 197. } 198. }
and a section in the main that makes intermittent calls to the overridden paintComponent method and increments the image file name place holder
125. jtp.paintComponent(g1); 126. if(i1 < 600) 127. i1++; 128. else 129. i1 = 100;
The entire 200 lines of code
1. import java.net.MalformedURLException; 2. import java.net.URL; 3. import java.awt.Color; 4. import java.awt.Dimension; 5. import java.awt.Graphics; 6. import java.awt.Image; 7. import java.awt.Toolkit; 8. import java.awt.image.BufferedImage; 9. import java.io.File; 10. import java.io.IOException; 11. import javax.imageio.ImageIO; 12. import javax.swing.ImageIcon; 13. import javax.swing.JFileChooser; 14. import javax.swing.JFrame; 15. import javax.swing.JLabel; 16. import javax.swing.JPanel; 17. import javax.swing.JScrollPane; 18. import javax.swing.JTextPane; 19. import javax.swing.text.BadLocationException; 20. import javax.swing.text.DefaultHighlighter; 21. 22. import org.apache.commons.io.FileUtils; 23. 24. 25. public class storybook 26. { 27. public static Image img; 28. 29. public static Graphics g1; 30. public static int i1 = 100; 31. 32. public static void main( String args[] ) 33. { 34. 35. JFileChooser fileChooser = new JFileChooser(); 36. 37. // show open file dialog 38. int result = fileChooser.showOpenDialog( null ); 39. 40. if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file 41. { 42. URL mediaURL = null; 43. 44. try 45. { 46. // get the file as URL 47. mediaURL = fileChooser.getSelectedFile().toURI().toURL(); 48. } // end try 49. catch ( MalformedURLException malformedURLException ) 50. { 51. System.err.println( "Could not create URL for the file" ); 52. } // end catch 53. 54. if ( mediaURL != null ) // only display if there is a valid URL 55. { 56. JLabel jl1 = new JLabel("Label"); 57. FileUtils fu = new FileUtils(); 58. File f = new File (mediaURL.getFile()); 59. String t = new String(); 60. try 61. { 62. t = FileUtils.readFileToString(f); 63. } 64. catch (IOException e) 65. { 66. // TODO Auto-generated catch block 67. e.printStackTrace(); 68. } 69. JFrame jf = new JFrame("Text"); 70. JPanel jp = new JPanel(); 71. MyTextPane jtp = new MyTextPane(); 72. MyTextPane jtp2 = new MyTextPane(); 73. jtp2.setSize(1024,768); 74. jtp.setSize(1024,768); 75. 76. jtp.setText(t); 77. jtp2.setText(t); 78. 79. JScrollPane slider = new JScrollPane(jtp); 80. slider.setVisible(true); 81. slider.setPreferredSize(new Dimension(1024, 768)); 82. 83. jp.add(slider); 84. JPanel jp1 = new JPanel(); 85. BufferedImage myPicture = null; 86. 87. try 88. { 89. myPicture = ImageIO.read(new File("C:\\Lighthouse.jpg")); 90. } 91. catch (IOException e1) 92. { 93. // TODO Auto-generated catch block 94. e1.printStackTrace(); 95. } 96. 97. jf.add(jp); 98. jf.setSize(1024,768); 99. jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 100. jf.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Lighthouse.jpg")); 101. 102. jf.setVisible(true); 103. 104. char space; 105. String periodstring = "."; 106. char period = periodstring.charAt(0); 107. String spacestring = " "; 108. space = spacestring.charAt(0); 109. int nz = 0; 110. DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); 111. t.replaceAll("\r", ""); 112. t.replaceAll("\n", ""); 113. 114. for(int n = 0; n <= jtp.getText().length(); n++) 115. { 116. int length = jtp.getDocument().getLength(); 117. 118. try { 119. 120. if(jtp.getDocument().getText(0, length).toString().charAt(n) == period) 121. { 122. 123. 124. 125. jtp.paintComponent(g1); 126. if(i1 < 600) 127. i1++; 128. else 129. i1 = 100; 130. 131. 132. 133. try 134. { 135. jtp.getHighlighter().addHighlight(nz, n, highlightPainter); 136. } 137. catch (BadLocationException e) 138. { 139. // TODO Auto-generated catch block 140. e.printStackTrace(); 141. } 142. nz = n + 1; 143. n++; 144. jtp.getHighlighter().removeAllHighlights(); 145. length = jtp.getDocument().getLength(); 146. String tempText = jtp.getDocument().getText(0, length).toString().substring(n,length); 147. jtp.setText(tempText); 148. n = 0; 149. } 150. else 151. { 152. 153. 154. try 155. { 156. jtp.getHighlighter().addHighlight(0, n, highlightPainter); 157. } 158. catch (BadLocationException e) 159. { 160. // TODO Auto-generated catch block 161. e.printStackTrace(); 162. } 163. 164. } 165. } 166. catch (BadLocationException e) { 167. // TODO Auto-generated catch block 168. e.printStackTrace(); 169. } 170. } 171. } 172. } 173. 174. 175. } 176. private static class MyTextPane extends JTextPane 177. { 178. public MyTextPane() 179. { 180. super(); 181. setText("Hello World"); 182. setOpaque(false); 183. setBackground(new Color(0,0,0,0)); 184. } 185. 186. @Override 187. protected void paintComponent(Graphics g) 188. { 189. if(g1 == null) 190. { 191. g1 = g; 192. } 193. img = null; 194. img = Toolkit.getDefaultToolkit().getImage("C:\\images\\" + i1 + ".jpg"); 195. g.drawImage(img, 0, 0, this); 196. super.paintComponent(g); 197. } 198. } 199. }
How should I go about altering this code to allow java to release image/graphic/paintComponent cache from memory once a new image is painted?