1 | !636 |
2 | !629 // standard functions |
3 | !658 // image classes |
4 | |
5 | import java.awt.*; |
6 | import java.awt.image.*; |
7 | import java.util.List; |
8 | import javax.imageio.*; |
9 | |
10 | public class main { |
11 | public static void main(String[] args) { |
12 | JFrame frame = new JFrame("A JavaX Frame"); |
13 | |
14 | RGBImage image = loadImage("#1000326"); // Bryan Cranston! |
15 | JScrollPane panel = new JScrollPane(new ImageSurface(image)); |
16 | |
17 | frame.add(panel); |
18 | frame.setBounds(100, 100, 500, 400); |
19 | frame.setVisible(true); |
20 | exitOnFrameClose(frame); |
21 | } |
22 | |
23 | static boolean useImageCache = true; |
24 | static RGBImage loadImage(String snippetID) { |
25 | try { |
26 | File dir = new File(System.getProperty("user.home"), ".tinybrain/image-cache"); |
27 | if (useImageCache) { |
28 | dir.mkdirs(); |
29 | File file = new File(dir, snippetID + ".png"); |
30 | if (file.exists() && file.length() != 0) |
31 | try { |
32 | return new RGBImage(ImageIO.read(file)); |
33 | } catch (Throwable e) { |
34 | e.printStackTrace(); |
35 | // fall back to loading from sourceforge |
36 | } |
37 | } |
38 | |
39 | String imageURL = getImageURL(parseSnippetID(snippetID)); |
40 | System.err.println("Loading image: " + imageURL); |
41 | BufferedImage image = ImageIO.read(new URL(imageURL)); |
42 | |
43 | if (useImageCache) { |
44 | File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); |
45 | ImageIO.write(image, "png", tempFile); |
46 | tempFile.renameTo(new File(dir, snippetID + ".png")); |
47 | //Log.info("Cached image."); |
48 | } |
49 | |
50 | //Log.info("Loaded image."); |
51 | return new RGBImage(image); |
52 | } catch (IOException e) { |
53 | throw new RuntimeException(e); |
54 | } |
55 | } |
56 | |
57 | static String getImageURL(long snippetID) throws IOException { |
58 | String url; |
59 | if (snippetID == 1000010 || snippetID == 1000012) |
60 | url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + snippetID; |
61 | else |
62 | url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + snippetID |
63 | + "&contentType=image/png"; |
64 | return url; |
65 | } |
66 | |
67 | public static long parseSnippetID(String snippetID) { |
68 | return Long.parseLong(shortenSnippetID(snippetID)); |
69 | } |
70 | |
71 | private static String shortenSnippetID(String snippetID) { |
72 | if (snippetID.startsWith("#")) |
73 | snippetID = snippetID.substring(1); |
74 | String httpBlaBla = "http://tinybrain.de/"; |
75 | if (snippetID.startsWith(httpBlaBla)) |
76 | snippetID = snippetID.substring(httpBlaBla.length()); |
77 | return snippetID; |
78 | } |
79 | } |
Began life as a copy of #661
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #662 |
Snippet name: | Image displayer (from snippets database) |
Eternal ID of this version: | #662/1 |
Text MD5: | 710320763ada71dbf6d38464b661740f |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-07-13 05:05:50 |
Source code size: | 2511 bytes / 79 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 779 / 701 |
Referenced in: | [show references] |