Better zoom for images

This commit is contained in:
Nico Mexis 2021-07-21 11:29:52 +02:00
parent 09b120ee89
commit 0653e8583e
No known key found for this signature in database
GPG Key ID: 27D6E17CE092AB78
2 changed files with 38 additions and 17 deletions

View File

@ -48,11 +48,14 @@ import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage;
public class FileViewer extends ResourceViewer
{
public static final float ZOOM_STEP_SIZE = 1.5f;
public final SearchableRSyntaxTextArea textArea = (SearchableRSyntaxTextArea)
Configuration.rstaTheme.apply(new SearchableRSyntaxTextArea());
public final JPanel mainPanel = new JPanel(new BorderLayout());
public BufferedImage originalImage;
public BufferedImage image;
public boolean canRefresh;
public int zoomSteps = 0;
public FileViewer(final ResourceContainer container, final String name)
{
@ -93,24 +96,42 @@ public class FileViewer extends ResourceViewer
!hexViewerOnly)
{
canRefresh = true;
image = MiscUtils.loadImage(image, contents); //gifs fail because of this
if (image == null) {
JHexEditor hex = new JHexEditor(contents);
mainPanel.add(hex);
return;
}
originalImage = image;
mainPanel.add(new ImageJLabel(image), BorderLayout.CENTER);
mainPanel.addMouseWheelListener(e ->
{
mainPanel.addMouseWheelListener(e -> {
int notches = e.getWheelRotation();
if (notches < 0) //zoom in
image = Scalr.resize(image, Scalr.Method.SPEED, image.getWidth() + 10,
image.getHeight() + 10);
else //zoom out
image = Scalr.resize(image, Scalr.Method.SPEED, image.getWidth() - 10,
image.getHeight() - 10);
mainPanel.removeAll();
mainPanel.add(new ImageJLabel(image), BorderLayout.CENTER);
mainPanel.updateUI();
int width = originalImage.getWidth();
int height = originalImage.getHeight();
int oldZoomSteps = zoomSteps;
if (notches < 0) {
//zoom in
zoomSteps++;
} else {
//zoom out
zoomSteps--;
}
try {
double factor = Math.pow(ZOOM_STEP_SIZE, zoomSteps);
int newWidth = (int) (width * factor);
int newHeight = (int) (height * factor);
image = Scalr.resize(originalImage, Scalr.Method.SPEED, newWidth, newHeight);
mainPanel.removeAll();
mainPanel.add(new ImageJLabel(image), BorderLayout.CENTER);
mainPanel.updateUI();
} catch (Throwable ignored) {
zoomSteps = oldZoomSteps;
}
});
return;
}

View File

@ -17,14 +17,14 @@ public enum ResourceType
JAVA_ARCHIVE(IconResources.jarIcon, "jar", "war", "ear"),
ZIP_ARCHIVE(IconResources.zipIcon, "zip"),
ANDROID_ARCHIVE(IconResources.androidIcon, "apk", "wapk", "dex"),
IMAGE_FILE(IconResources.imageIcon, "png", "jpg", "jpeg", "bmp", "wbmp", "gif", "tif"),
IMAGE_FILE(IconResources.imageIcon, "png", "jpg", "jpeg", "bmp", "wbmp", "gif", "tif", "webp"),
CONFIG_TEXT_FILE(IconResources.configIcon, "properties", "xml", "jsp", "mf", "config",
"csv", "yml", "yaml", "ini", "json", "sql", "gradle", "dockerfile", "htaccess",
"plugin", "attachprovider", "transportservice", "connector"),
JAVA_FILE(IconResources.javaIcon, "java"),
TEXT_FILE(IconResources.textIcon, "txt", "md", "log", "html", "css"),
CPP_FILE(IconResources.cplusplusIcon, "c", "cpp", "h"),
CSHAR_FILE(IconResources.csharpIcon, "cs"),
CSHARP_FILE(IconResources.csharpIcon, "cs"),
BAT_FILE(IconResources.batIcon, "bat", "batch"),
SH_FILE(IconResources.shIcon, "sh", "bash"),
;