From 177f9cef0ae55ab1f04881e50b60b04149b2993d Mon Sep 17 00:00:00 2001 From: Anton Tarasov Date: Tue, 6 Sep 2016 16:14:00 +0300 Subject: [PATCH] Fix HiDPIScaledImage used for drawing. - One of the ctors should correctly separate user & device spaces - getWidth/getHeight should always return values in a user space - cleanup HiDPIScaledGraphics --- .../intellij/util/HiDPIScaledGraphics.java | 4 +- .../com/intellij/util/JBHiDPIScaledImage.java | 42 ++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/platform/util/src/com/intellij/util/HiDPIScaledGraphics.java b/platform/util/src/com/intellij/util/HiDPIScaledGraphics.java index fbf545ecdb0b..6c38da2d094f 100644 --- a/platform/util/src/com/intellij/util/HiDPIScaledGraphics.java +++ b/platform/util/src/com/intellij/util/HiDPIScaledGraphics.java @@ -34,10 +34,8 @@ import java.util.Map; */ class HiDPIScaledGraphics extends Graphics2D { protected final Graphics2D myPeer; - private BufferedImage myImage; - public HiDPIScaledGraphics(Graphics g, BufferedImage image) { - myImage = image; + public HiDPIScaledGraphics(Graphics g) { myPeer = (Graphics2D)g; scale(2, 2); GraphicsUtil.setupAAPainting(myPeer); diff --git a/platform/util/src/com/intellij/util/JBHiDPIScaledImage.java b/platform/util/src/com/intellij/util/JBHiDPIScaledImage.java index bdc326ff8fd9..12e4837c4046 100644 --- a/platform/util/src/com/intellij/util/JBHiDPIScaledImage.java +++ b/platform/util/src/com/intellij/util/JBHiDPIScaledImage.java @@ -15,6 +15,8 @@ */ package com.intellij.util; +import org.jetbrains.annotations.NotNull; + import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; @@ -24,16 +26,33 @@ import java.awt.image.ImageObserver; */ public class JBHiDPIScaledImage extends BufferedImage { private final Image myImage; - private int myWidth; // == myImage.width / scale - private int myHeight; // == myImage.height / scale + private final int myWidth; + private final int myHeight; + /** + * Creates a HiDPI-aware BufferedImage to draw on. + * + * @param width width in a user space coordinates + * @param height height in a user space coordinates + * @param type type of the created image + */ public JBHiDPIScaledImage(int width, int height, int type) { - this(null, 2 * width, 2 * height, type); + super(2 * width, 2 * height, type); + myImage = null; + myWidth = width; + myHeight = height; } - public JBHiDPIScaledImage(Image image, int width, int height, int type) { - // In case there's a delegate image, create a dummy wrapper image of 1x1 size - super(image != null ? 1 : width, image != null ? 1 : height, type); + /** + * Creates a HiDPI-aware BufferedImage wrapping a delegate Image. + * + * @param image the delegate image of size in a device space coordinates to wrap + * @param width width in a user space coordinates + * @param height height in a user space coordinates + * @param type type of the created image + */ + public JBHiDPIScaledImage(@NotNull Image image, int width, int height, int type) { + super(1, 1, type); // give the wrapper a minimal 1x1 size to save space myImage = image; myWidth = width; myHeight = height; @@ -45,29 +64,30 @@ public class JBHiDPIScaledImage extends BufferedImage { @Override public int getWidth() { - return myImage != null ? myWidth : super.getWidth(); + return myWidth; } @Override public int getHeight() { - return myImage != null ? myHeight : super.getHeight(); + return myHeight; } @Override public int getWidth(ImageObserver observer) { - return myImage != null ? myWidth : super.getWidth(observer); + return myWidth; } @Override public int getHeight(ImageObserver observer) { - return myImage != null ? myHeight : super.getHeight(observer); + return myHeight; } @Override public Graphics2D createGraphics() { + assert myImage == null : "graphics should only be created for the image used for drawing"; Graphics2D g = super.createGraphics(); if (myImage == null) { - return new HiDPIScaledGraphics(g, this); + return new HiDPIScaledGraphics(g); } return g; }