comparison src/name/blackcap/imageprep/RotateDialog.kt @ 12:26a507e095ab

Use ImageObserver and wait if needed.
author David Barts <n5jrn@me.com>
date Sat, 18 Jul 2020 12:04:05 -0700
parents 1f824742e1fa
children 3feeb953d9ae
comparison
equal deleted inserted replaced
11:1f824742e1fa 12:26a507e095ab
4 package name.blackcap.imageprep 4 package name.blackcap.imageprep
5 5
6 import java.awt.Dimension 6 import java.awt.Dimension
7 import java.awt.Graphics 7 import java.awt.Graphics
8 import java.awt.Graphics2D 8 import java.awt.Graphics2D
9 import java.awt.Image
9 import java.awt.Toolkit 10 import java.awt.Toolkit
10 import java.awt.event.ActionListener 11 import java.awt.event.ActionListener
11 import java.awt.image.BufferedImage 12 import java.awt.image.BufferedImage
13 import java.awt.image.ImageObserver
12 import java.io.File 14 import java.io.File
13 import java.io.IOException 15 import java.io.IOException
16 import java.util.concurrent.Semaphore
14 import java.util.logging.Level 17 import java.util.logging.Level
15 import java.util.logging.Logger 18 import java.util.logging.Logger
16 import javax.imageio.ImageIO 19 import javax.imageio.ImageIO
17 import javax.swing.* 20 import javax.swing.*
18 import kotlin.math.* 21 import kotlin.math.*
38 override protected fun paintComponent(g: Graphics): Unit { 41 override protected fun paintComponent(g: Graphics): Unit {
39 g.drawImage(image, 0, 0, null) 42 g.drawImage(image, 0, 0, null)
40 } 43 }
41 } 44 }
42 private val drawingPane = DrawingPane(initialImage) 45 private val drawingPane = DrawingPane(initialImage)
46
47 private class ImageWaiter: ImageObserver {
48 private var sem = Semaphore(0)
49 @Volatile private var flags: Int? = null
50 private var MASK = ImageObserver.ALLBITS or ImageObserver.ERROR or ImageObserver.ABORT
51
52 override fun imageUpdate(img: Image, infoflags: Int, x: Int, y: Int, width: Int, height: Int): Boolean {
53 if (infoflags and MASK != 0) {
54 flags = infoflags
55 sem.release()
56 return false
57 }
58 return true
59 }
60
61 fun wait(): Int {
62 sem.acquire()
63 return flags!!
64 }
65 }
43 66
44 val image: BufferedImage 67 val image: BufferedImage
45 get() { return drawingPane.image } 68 get() { return drawingPane.image }
46 69
47 private val r90cw = JButton("90° CW").also { 70 private val r90cw = JButton("90° CW").also {
129 } else { 152 } else {
130 val nWidth = (imageIn.width * ratio).toInt() 153 val nWidth = (imageIn.width * ratio).toInt()
131 val nHeight = (imageIn.height * ratio).toInt() 154 val nHeight = (imageIn.height * ratio).toInt()
132 val imageOut = BufferedImage(nWidth, nHeight, imageIn.type) 155 val imageOut = BufferedImage(nWidth, nHeight, imageIn.type)
133 imageOut.createGraphics().run { 156 imageOut.createGraphics().run {
134 drawImage(imageIn.getScaledInstance(nWidth, nHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null) 157 val w = ImageWaiter()
158 if (!drawImage(imageIn.getScaledInstance(nWidth, nHeight, BufferedImage.SCALE_SMOOTH), 0, 0, w))
159 w.wait()
135 dispose() 160 dispose()
136 } 161 }
137 Pair(imageOut, null) 162 Pair(imageOut, null)
138 } 163 }
139 } catch (e: IOException) { 164 } catch (e: IOException) {