diff src/name/blackcap/imageprep/RotateDialog.kt @ 1:0bded24f746e

Compiles, still untested.
author David Barts <n5jrn@me.com>
date Thu, 16 Jul 2020 21:51:08 -0700
parents e0efe7848130
children 09dcd475d1bf
line wrap: on
line diff
--- a/src/name/blackcap/imageprep/RotateDialog.kt	Thu Jul 16 19:57:23 2020 -0700
+++ b/src/name/blackcap/imageprep/RotateDialog.kt	Thu Jul 16 21:51:08 2020 -0700
@@ -3,8 +3,11 @@
  */
 package name.blackcap.imageprep
 
+import java.awt.Dimension
+import java.awt.Graphics
 import java.awt.Graphics2D
 import java.awt.RenderingHints
+import java.awt.event.ActionListener
 import java.awt.image.BufferedImage
 import java.io.File
 import java.io.IOException
@@ -18,7 +21,7 @@
     private val MINWIDTH = 512
     private val MINHEIGHT = 384
 
-    private class DrawingPane() : JPanel() {
+    private class DrawingPane(initialImage: BufferedImage) : JPanel() {
         var image: BufferedImage = initialImage
         set(value) {
             field = value
@@ -26,27 +29,26 @@
             repaint()
         }
 
-        override val preferredSize
-        get { return image.preferredSize }
+        override fun getPreferredSize() = Dimension(image.width, image.height)
 
-        protected paintComponent(g: Graphics): Unit {
+        override protected fun paintComponent(g: Graphics): Unit {
             g.drawImage(image, 0, 0, null)
         }
     }
-    private val drawingPane = DrawingPane()
+    private val drawingPane = DrawingPane(initialImage)
 
     val image: BufferedImage
     get() { return drawingPane.image }
 
-    private val r90cw = JButton("90° CW").apply {
+    private val r90cw = JButton("90° CW").also {
         it.addActionListener(ActionListener { doRotate(90) })
     }
 
-    private val r180 = JButton("180°").apply {
+    private val r180 = JButton("180°").also {
         it.addActionListener(ActionListener { doRotate(180) })
     }
 
-    private val r90ccw = JButton("90° CCW").apply {
+    private val r90ccw = JButton("90° CCW").also {
         it.addActionListener(ActionListener { doRotate(270) })
     }
 
@@ -64,7 +66,7 @@
         val rotatedImage = BufferedImage(w, h, image.type)
         rotatedImage.createGraphics().run {
             translate((w - image.width) / 2, (h - image.height) / 2)
-            rotate(rad, image.width / 2, image.height / 2)
+            rotate(rad, image.width.toDouble()/2.0, image.height.toDouble()/2.0)
             drawRenderedImage(image, null)
             dispose()
         }
@@ -76,7 +78,7 @@
         title = "Untitled"
         contentPane.apply {
             layout = BoxLayout(this, BoxLayout.Y_AXIS)
-            add(JScrollpane(drawingPane).apply {
+            add(JScrollPane(drawingPane).apply {
                 alignmentX = JScrollPane.CENTER_ALIGNMENT
                 addBorder(BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2))
                 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
@@ -106,13 +108,13 @@
          */
         fun makeDialog(input: File): Unit {
             Application.mainFrame.useWaitCursor()
-            swingWorker<Pair<BufferedImage?, IOException?> {
+            swingWorker<Pair<BufferedImage?, IOException?>> {
                 inBackground {
                     try {
                         val imageIn = ImageIO.read(input) /* IOException */
                         val ratio = Settings.maxDimension.toDouble() / max(imageIn.width, imageIn.height).toDouble()
                         if (ratio >= 1.0) {
-                            null
+                            Pair(null, null)
                         } else {
                             val nWidth = (imageIn.width * ratio).toInt()
                             val nHeight = (imageIn.height * ratio).toInt()
@@ -134,8 +136,12 @@
                     val (image, error) = get()
                     if (error != null)
                         ioExceptionDialog(Application.mainFrame, input, "read", error)
-                    if (image != null)
+                    else if (image != null)
                         RotateDialog(input, image).title = input.getName()
+                    else
+                        JOptionPane.showMessageDialog(Application.mainFrame,
+                            "Image is too small to be scaled.",
+                            "Warning", JOptionPane.WARNING_MESSAGE)
                 }
             }
         }