View Javadoc
1 /* 2 * RenderThread.java 3 * 4 * Created on 11. Februar 2002, 19:38 5 * 6 * Licensed under GNU General Public License (GPL). 7 * See http://www.gnu.org/copyleft/gpl.html 8 */ 9 10 package edu.psu.geovista.app.parvis.gui; 11 12 import java.awt.*; 13 import java.awt.image.*; 14 15 /*** 16 * 17 * @author flo 18 * @version 19 */ 20 class RenderThread extends Thread { 21 22 /*** flags to control rendering */ 23 boolean quality = false; 24 boolean progressive = false; 25 boolean brushMode = false; 26 27 /*** flags to indicate thread state */ 28 boolean isWorking = false; 29 boolean doWork = false; 30 boolean wasInterrupted = false; 31 boolean progressiveInterrupted = false; 32 boolean secondPass = false; 33 boolean qualitychanged = false; 34 35 int startAxis, stopAxis; 36 int progressiveStartAxis, progressiveStopAxis; 37 int lastStart = 0; 38 int lastStop = 0; 39 40 int ids[] = null; 41 BufferedImage renderedImage = null; 42 43 Stroke stroke = new BasicStroke(); 44 Color color = Color.black; 45 46 BasicParallelDisplayUI ui = null; 47 ParallelDisplay comp = null; 48 49 50 RenderThread(BasicParallelDisplayUI ui){ 51 this.ui = ui; 52 this.setPriority(Thread.MIN_PRIORITY); 53 } 54 55 void setCurrentComponent(ParallelDisplay comp){ 56 this.comp = comp; 57 } 58 59 void setBrushMode(boolean brushMode){ 60 this.brushMode = brushMode; 61 } 62 63 void setQuality(boolean quality, boolean progressive){ 64 this.progressive = progressive; 65 if (progressive) 66 this.quality = false; 67 else { 68 if (this.quality != quality){ 69 qualitychanged = true; 70 this.quality = quality; 71 } 72 } 73 } 74 75 synchronized void setRegion(int startAxis, int stopAxis){ 76 if (wasInterrupted || ((isWorking || doWork) && !secondPass) || (isWorking && secondPass && doWork)){ 77 // old render area still invalid -> expand 78 if (startAxis < this.startAxis) 79 this.startAxis = startAxis; 80 if (stopAxis > this.stopAxis) 81 this.stopAxis = stopAxis; 82 if (startAxis < this.progressiveStartAxis) 83 this.progressiveStartAxis = startAxis; 84 if (stopAxis > this.progressiveStopAxis) 85 this.progressiveStopAxis = stopAxis; 86 } 87 else if (progressiveInterrupted || (isWorking || doWork)){ 88 this.startAxis = startAxis; 89 this.stopAxis = stopAxis; 90 if (startAxis < this.progressiveStartAxis) 91 this.progressiveStartAxis = startAxis; 92 if (stopAxis > this.progressiveStopAxis) 93 this.progressiveStopAxis = stopAxis; 94 } 95 else { 96 this.startAxis = startAxis; 97 this.stopAxis = stopAxis; 98 this.progressiveStartAxis = startAxis; 99 this.progressiveStopAxis = stopAxis; 100 } 101 //this.ids = ids.clone(); 102 ////System.out.println("RenderThread: setting repaint axes: " + this.startAxis + ", " + this.stopAxis); 103 } 104 105 void setStyle(Stroke stroke, Color color){ 106 this.stroke = stroke; 107 this.color = color; 108 } 109 110 synchronized boolean isWorking(){ 111 return isWorking; 112 } 113 114 public BufferedImage getRenderedImage(){ 115 return renderedImage; 116 } 117 118 public int getRenderedRegionStart(){ 119 return lastStart; 120 } 121 122 public int getRenderedRegionStop(){ 123 return lastStop; 124 } 125 126 public void run(){ 127 while(true){ 128 synchronized(this){ 129 isWorking = false; 130 // wait for next rendering to be queued 131 do { 132 try { 133 if (! doWork){ 134 //System.out.println("RenderThread: waiting..."); 135 this.wait(); 136 } 137 ////System.out.println("RenderThread: exit waiting."); 138 } 139 catch (InterruptedException iex){ 140 //System.out.println("RenderThread: interruptedExcpetion."); 141 //rendering has been cancelled 142 } 143 } 144 while (this.interrupted()); 145 146 isWorking = true; 147 doWork = false; 148 renderedImage = null; 149 qualitychanged = false; 150 } 151 152 ////System.out.println("RenderThread: run loop start..."); 153 154 boolean progress = true; 155 156 while (comp != null && progress){ 157 String modestr; 158 159 if (progressive && quality){ 160 secondPass = true; 161 162 ////System.out.println("RenderThread: starting progressive paint..."); 163 modestr = "[quality]"; 164 165 //2nd pass: lower priority, keep response time low 166 this.yield(); 167 } 168 else { 169 secondPass = false; 170 171 ////System.out.println("RenderThread: starting paint..."); 172 modestr = "[preview]"; 173 } 174 175 // this is the main rendering routine 176 comp.fireProgressEvent(new ProgressEvent(comp, ProgressEvent.PROGRESS_START, 0.0f, "rendering " + modestr)); 177 178 BufferedImage img = new BufferedImage(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); 179 Graphics2D g2 = (Graphics2D)img.getGraphics(); 180 setupRendering(g2, quality, stroke, color); 181 182 // render all records 183 int i = 0; 184 float brushVal = 0.0f; 185 if (brushMode){ 186 color = comp.getBrushedColor();//changed fah july 30 02 187 } 188 189 for (; i<comp.getNumRecords(); i++){ 190 if (i % 300 == 0){ 191 comp.fireProgressEvent(new ProgressEvent(comp, ProgressEvent.PROGRESS_UPDATE, ((float)i)/comp.getNumRecords(), "rendering " + modestr)); 192 } 193 if (!brushMode || (brushVal = comp.getBrushValue(i)) > 0.0f){ 194 // select records in brushmode, render all in normal mode 195 //skip soft edges 196 if (!quality && brushMode && brushVal < 0.8) continue; 197 if (brushMode && quality){ 198 //Color col = new Color(color.getRed(), color.getBlue(), color.getGreen(), (int)(255 * brushVal)); 199 ////System.out.println("Brush value: " + brushVal + " alpha: " + col.getAlpha()); 200 //g2.setColor(col); 201 ui.drawRecord(g2, comp, i, progressiveStartAxis, progressiveStopAxis); 202 203 } 204 if (secondPass) { 205 ui.drawRecord(g2, comp, i, progressiveStartAxis, progressiveStopAxis); 206 207 } 208 else { 209 ui.drawRecord(g2, comp, i, startAxis, stopAxis); 210 211 } 212 213 if (qualitychanged || secondPass){ 214 //2nd pass: lower priority, keep response time low 215 this.yield(); 216 if (this.interrupted()){ 217 progressiveInterrupted = true; 218 ////System.out.println("### breaking!"); 219 break; 220 } 221 } 222 } 223 224 } 225 226 if (i==comp.getNumRecords()){ 227 //finished all records 228 wasInterrupted = false; 229 230 renderedImage = img; 231 if (secondPass){ 232 lastStart = progressiveStartAxis; 233 lastStop = progressiveStopAxis; 234 progressiveInterrupted = false; 235 } 236 else { 237 lastStart = startAxis; 238 lastStop = stopAxis; 239 } 240 241 comp.fireProgressEvent(new ProgressEvent(comp, ProgressEvent.PROGRESS_FINISH, 1.0f, "rendering " + modestr)); 242 comp.repaint(); 243 244 ////System.out.println("RenderThread: paint finished..."); 245 246 if (progressive){ 247 if (quality) { 248 quality = false; 249 progress = false; 250 } 251 else { 252 quality = true; 253 } 254 } 255 else { 256 progress = false; 257 } 258 } 259 else { 260 // we have been interrupted 261 ////System.out.println("RenderThread: paint interrupted..."); 262 progress = false; 263 if ( secondPass ){ 264 //2nd pass progressive -> throw away 265 wasInterrupted = false; 266 quality = false; 267 } 268 } 269 secondPass = false; 270 271 } 272 } 273 } 274 275 synchronized void render(){ 276 ////System.out.println(this.getName() + ".render() called"); 277 if (this.isWorking){ 278 this.interrupt(); 279 } 280 this.doWork = true; 281 this.notify(); 282 } 283 284 public void reset(){ 285 //throw away image 286 renderedImage = null; 287 } 288 289 public void setupRendering(Graphics2D g2, boolean quality, Stroke stroke, Color color){ 290 RenderingHints qualityHints = new RenderingHints(null); 291 292 if (quality) { 293 qualityHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 294 qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 295 296 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f); 297 g2.setComposite(ac); 298 299 g2.setStroke(stroke); 300 g2.setColor(color); 301 } 302 else { 303 qualityHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 304 qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); 305 306 g2.setStroke(new BasicStroke()); 307 //strip out alpha 308 g2.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue())); 309 } 310 311 g2.setRenderingHints(qualityHints); 312 313 314 } 315 316 }

This page was automatically generated by Maven