1 | abstract static class Surface extends JPanel { |
2 | public BufferedImage bimg; |
3 | public int imageType; |
4 | public boolean clearSurface = true; |
5 | |
6 | private Toolkit toolkit; |
7 | private int biw, bih; |
8 | private boolean clearOnce; |
9 | |
10 | public Surface() { |
11 | setDoubleBuffered(false); |
12 | toolkit = getToolkit(); |
13 | setImageType(0); |
14 | } |
15 | |
16 | public int getImageType() { |
17 | return imageType; |
18 | } |
19 | |
20 | public void setImageType(int imgType) { |
21 | if (imgType == 0) { |
22 | imageType = 1; |
23 | } else { |
24 | imageType = imgType; |
25 | } |
26 | bimg = null; |
27 | } |
28 | |
29 | public BufferedImage createBufferedImage(Graphics2D g2, int w, int h, int imgType) { |
30 | BufferedImage bi = null; |
31 | if (imgType == 0) { |
32 | bi = (BufferedImage) g2.getDeviceConfiguration(). |
33 | createCompatibleImage(w, h); |
34 | } else if (imgType > 0 && imgType < 14) { |
35 | bi = new BufferedImage(w, h, imgType); |
36 | } else if (imgType == 14) { |
37 | bi = createBinaryImage(w, h, 2); |
38 | } else if (imgType == 15) { |
39 | bi = createBinaryImage(w, h, 4); |
40 | } else if (imgType == 16) { |
41 | bi = createSGISurface(w, h, 32); |
42 | } else if (imgType == 17) { |
43 | bi = createSGISurface(w, h, 16); |
44 | } |
45 | biw = w; |
46 | bih = h; |
47 | return bi; |
48 | } |
49 | |
50 | |
51 | // Lookup tables for BYTE_BINARY 1, 2 and 4 bits. |
52 | static byte[] lut1Arr = new byte[] {0, (byte)255 }; |
53 | static byte[] lut2Arr = new byte[] {0, (byte)85, (byte)170, (byte)255}; |
54 | static byte[] lut4Arr = new byte[] {0, (byte)17, (byte)34, (byte)51, |
55 | (byte)68, (byte)85,(byte) 102, (byte)119, |
56 | (byte)136, (byte)153, (byte)170, (byte)187, |
57 | (byte)204, (byte)221, (byte)238, (byte)255}; |
58 | |
59 | |
60 | private BufferedImage createBinaryImage(int w, int h, int pixelBits) { |
61 | int bytesPerRow = w * pixelBits / 8; |
62 | if (w * pixelBits % 8 != 0) { |
63 | bytesPerRow++; |
64 | } |
65 | byte[] imageData = new byte[h * bytesPerRow]; |
66 | IndexColorModel cm = null; |
67 | switch (pixelBits) { |
68 | case 1: |
69 | cm = new IndexColorModel(pixelBits, lut1Arr.length, |
70 | lut1Arr, lut1Arr, lut1Arr); |
71 | break; |
72 | case 2: |
73 | cm = new IndexColorModel(pixelBits, lut2Arr.length, |
74 | lut2Arr, lut2Arr, lut2Arr); |
75 | break; |
76 | case 4: |
77 | cm = new IndexColorModel(pixelBits, lut4Arr.length, |
78 | lut4Arr, lut4Arr, lut4Arr); |
79 | break; |
80 | default: |
81 | {new Exception("Invalid # of bit per pixel").printStackTrace();} |
82 | } |
83 | |
84 | DataBuffer db = new DataBufferByte(imageData, imageData.length); |
85 | WritableRaster r = Raster.createPackedRaster(db, w, h, pixelBits, null); |
86 | return new BufferedImage(cm, r, false, null); |
87 | } |
88 | |
89 | private BufferedImage createSGISurface(int w, int h, int pixelBits) { |
90 | int rMask32 = 0xFF000000; |
91 | int rMask16 = 0xF800; |
92 | int gMask32 = 0x00FF0000; |
93 | int gMask16 = 0x07C0; |
94 | int bMask32 = 0x0000FF00; |
95 | int bMask16 = 0x003E; |
96 | |
97 | DirectColorModel dcm = null; |
98 | DataBuffer db = null; |
99 | WritableRaster wr = null; |
100 | switch (pixelBits) { |
101 | case 16: |
102 | short[] imageDataUShort = new short[w * h]; |
103 | dcm = new DirectColorModel(16, rMask16, gMask16, bMask16); |
104 | db = new DataBufferUShort(imageDataUShort, imageDataUShort.length); |
105 | wr = Raster.createPackedRaster(db, w, h, w, |
106 | new int[] {rMask16, gMask16, bMask16}, |
107 | null); |
108 | break; |
109 | case 32: |
110 | int[] imageDataInt = new int[w * h]; |
111 | dcm = new DirectColorModel(32, rMask32, gMask32, bMask32); |
112 | db = new DataBufferInt(imageDataInt, imageDataInt.length); |
113 | wr = Raster.createPackedRaster(db, w, h, w, |
114 | new int[] {rMask32, gMask32, bMask32}, |
115 | null); |
116 | break; |
117 | default: |
118 | {new Exception("Invalid # of bit per pixel").printStackTrace();} |
119 | } |
120 | |
121 | return new BufferedImage(dcm, wr, false, null); |
122 | } |
123 | |
124 | public Graphics2D createGraphics2D(int width, int height, BufferedImage bi, Graphics g) { |
125 | |
126 | Graphics2D g2 = null; |
127 | |
128 | if (bi != null) { |
129 | g2 = bi.createGraphics(); |
130 | } else { |
131 | g2 = (Graphics2D) g; |
132 | } |
133 | |
134 | g2.setBackground(getBackground()); |
135 | |
136 | if (clearSurface || clearOnce) { |
137 | g2.clearRect(0, 0, width, height); |
138 | clearOnce = false; |
139 | } |
140 | |
141 | return g2; |
142 | } |
143 | |
144 | public abstract void render(int w, int h, Graphics2D g); |
145 | |
146 | public void paintImmediately(int x,int y,int w, int h) { |
147 | RepaintManager repaintManager = null; |
148 | boolean save = true; |
149 | if (!isDoubleBuffered()) { |
150 | repaintManager = RepaintManager.currentManager(this); |
151 | save = repaintManager.isDoubleBufferingEnabled(); |
152 | repaintManager.setDoubleBufferingEnabled(false); |
153 | } |
154 | super.paintImmediately(x, y, w, h); |
155 | |
156 | if (repaintManager != null) |
157 | repaintManager.setDoubleBufferingEnabled(save); |
158 | } |
159 | |
160 | public void paint(Graphics g) { |
161 | Dimension d = getSize(); |
162 | |
163 | if (imageType == 1) |
164 | bimg = null; |
165 | else if (bimg == null || biw != d.width || bih != d.height) { |
166 | bimg = createBufferedImage((Graphics2D)g, |
167 | d.width, d.height, imageType-2); |
168 | clearOnce = true; |
169 | } |
170 | |
171 | Graphics2D g2 = createGraphics2D(d.width, d.height, bimg, g); |
172 | render(d.width, d.height, g2); |
173 | g2.dispose(); |
174 | |
175 | if (bimg != null) { |
176 | g.drawImage(bimg, 0, 0, null); |
177 | toolkit.sync(); |
178 | } |
179 | } |
180 | } |
Began life as a copy of #1004554
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1007327 |
Snippet name: | Backup 2 of class Surface (with image types) |
Eternal ID of this version: | #1007327/1 |
Text MD5: | 824b1e997e099c30ec1228807c25bd31 |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-03-15 13:25:04 |
Source code size: | 5330 bytes / 180 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 527 / 495 |
Referenced in: | [show references] |