Skip to content

Commit

Permalink
fix java.lang.NoSuchMethodError: 'java.awt.Image com.intellij.util.Ic…
Browse files Browse the repository at this point in the history
…onUtil.toImage$default(javax.swing.Icon, com.intellij.ui.scale.ScaleContext, int, java.lang.Object)'
  • Loading branch information
Grigory Rylov committed Aug 17, 2023
1 parent 44b3e9f commit 2aef667
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ studioCompilePath=/Applications/Android Studio Preview.app/Contents

pluginGroup = com.github.grishberg.android
pluginName = android-layout-inspector-plugin
pluginVersion = 23.08.16.0
pluginVersion = 23.08.16.1

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.grishberg.android.layoutinspector.ui.layout

import java.awt.Graphics2D
import java.awt.GraphicsEnvironment
import java.awt.Image
import java.awt.Transparency
import java.awt.image.BufferedImage

Expand All @@ -29,7 +28,7 @@ class ImageHelper {
}

fun copyForClipboard(source: BufferedImage): BufferedImage {
val original: Image = source
val original: BufferedImage = source
val newImage = BufferedImage(
original.getWidth(null), original.getHeight(null), BufferedImage.TYPE_INT_RGB
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.github.grishberg.android.layoutinspector.ui.tree

import com.intellij.openapi.util.IconLoader
import com.intellij.util.IconUtil
import com.intellij.util.ui.ImageUtil
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import org.imgscalr.Scalr
import java.awt.Graphics2D
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import javax.swing.Icon
import javax.swing.ImageIcon


Expand All @@ -16,13 +14,31 @@ class IconsStore(private val iconSize: Int = ICON_SIZE) {
fun createImageIcon(path: String, altText: String = ""): ImageIcon {
val icon = IconLoader.getIcon(path, this.javaClass)

var image: BufferedImage? = ImageUtil.toBufferedImage(IconUtil.toImage(icon))
image =
Scalr.resize(image, Scalr.Method.ULTRA_QUALITY, if (UIUtil.isRetina()) iconSize else JBUI.scale(iconSize))
return if (image != null) {
ImageIcon(image, altText)
} else {
throw IllegalStateException("Image $path not found")
}
return resizeIcon(icon, iconSize, iconSize)
}

private fun resizeIcon(icon: Icon, width: Int, height: Int): ImageIcon {
val bufferedImage = BufferedImage(
icon.iconWidth, icon.iconHeight,
BufferedImage.TYPE_4BYTE_ABGR
)
val bufImageG: Graphics2D = bufferedImage.createGraphics()
icon.paintIcon(null, bufImageG, 0, 0)
bufImageG.dispose()

val resizedImg = BufferedImage(
width, height,
BufferedImage.TYPE_4BYTE_ABGR
)

val g2: Graphics2D = resizedImg.createGraphics()
g2.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR
)
g2.drawImage(bufferedImage, 0, 0, width, height, null)
g2.dispose()

return ImageIcon(resizedImg)
}
}

0 comments on commit 2aef667

Please sign in to comment.