changeset 3:09dcd475d1bf

Works (prelim tests only).
author David Barts <n5jrn@me.com>
date Fri, 17 Jul 2020 11:48:07 -0700
parents a6f9b51d5e8d
children 5234e4500d45
files src/name/blackcap/imageprep/Menus.kt src/name/blackcap/imageprep/RotateDialog.kt
diffstat 2 files changed, 16 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/imageprep/Menus.kt	Fri Jul 17 10:34:48 2020 -0700
+++ b/src/name/blackcap/imageprep/Menus.kt	Fri Jul 17 11:48:07 2020 -0700
@@ -75,7 +75,7 @@
 
     private fun doOpen() {
         val chooser = JFileChooser().apply {
-            fileFilter = FileNameExtensionFilter("Image files", *ImageIO.getReaderFileSuffixes())
+            fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes())
         }
         if (chooser.showOpenDialog(Application.mainFrame) == JFileChooser.APPROVE_OPTION) {
             RotateDialog.makeDialog(chooser.selectedFile)
@@ -104,7 +104,7 @@
                 if (Settings.outputToInputDir) w.file.parent else Settings.outputTo,
                 outName)
         }
-        if (chooser.showOpenDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) {
+        if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) {
             return
         }
         val (name, ext) = splitext(chooser.selectedFile.name)
--- a/src/name/blackcap/imageprep/RotateDialog.kt	Fri Jul 17 10:34:48 2020 -0700
+++ b/src/name/blackcap/imageprep/RotateDialog.kt	Fri Jul 17 11:48:07 2020 -0700
@@ -11,6 +11,8 @@
 import java.awt.image.BufferedImage
 import java.io.File
 import java.io.IOException
+import java.util.logging.Level
+import java.util.logging.Logger
 import javax.imageio.ImageIO
 import javax.swing.*
 import kotlin.math.*
@@ -57,12 +59,15 @@
        manipulating data used by Swing itself. Since the size of the images
        being rotated are small, we do it in the foreground. */
     private fun doRotate(deg: Int) {
+        rootPane.defaultButton = null
         // https://stackoverflow.com/questions/15927014/rotating-an-image-90-degrees-in-java
+        if (deg % 90 != 0) {
+            val barf = "${deg} not a multiple of 90!"
+            LOGGER.log(Level.SEVERE, barf)
+            throw AssertionError(barf)
+        }
         val rad = java.lang.Math.toRadians(deg.toDouble())
-        val sinx = sin(rad)
-        val cosx = cos(rad)
-        val w = floor(image.width * cosx + image.height * sinx).toInt()
-        val h = floor(image.height * cosx + image.width * sinx).toInt()
+        val (w, h) = if (deg % 180 == 0) Pair(image.width, image.height) else Pair(image.height, image.width)
         val rotatedImage = BufferedImage(w, h, image.type)
         rotatedImage.createGraphics().run {
             translate((w - image.width) / 2, (h - image.height) / 2)
@@ -97,6 +102,7 @@
                 add(r90ccw)
             })
         }
+        rootPane.defaultButton = null
         pack()
     }
 
@@ -137,7 +143,10 @@
                     if (error != null)
                         ioExceptionDialog(Application.mainFrame, input, "read", error)
                     else if (image != null)
-                        RotateDialog(input, image).title = input.getName()
+                        RotateDialog(input, image).run {
+                            title = input.name
+                            setVisible(true)
+                        }
                     else
                         JOptionPane.showMessageDialog(Application.mainFrame,
                             "Image is too small to be scaled.",