Skip to content

Commit

Permalink
8282526: Default icon is not painted properly
Browse files Browse the repository at this point in the history
Reviewed-by: aivanov, prr
  • Loading branch information
Alexander Zuev committed Jul 18, 2022
1 parent e72742e commit 6c8d0e6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,7 +25,9 @@

package sun.awt.shell;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.AbstractMultiResolutionImage;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
Expand Down Expand Up @@ -1421,7 +1423,7 @@ protected Image getBaseImage() {
public Image getResolutionVariant(double width, double height) {
int dist = 0;
Image retVal = null;
// We only care about width since we don't support non-rectangular icons
// We only care about width since we don't support non-square icons
int w = (int) width;
int retindex = 0;
for (Integer i : resolutionVariants.keySet()) {
Expand All @@ -1435,6 +1437,15 @@ public Image getResolutionVariant(double width, double height) {
}
}
}
if (retVal.getWidth(null) != w) {
BufferedImage newVariant = new BufferedImage(w, w, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = newVariant.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(retVal, 0,0, w, w, null);
g2d.dispose();
resolutionVariants.put(w, newVariant);
retVal = newVariant;
}
return retVal;
}

Expand Down
14 changes: 12 additions & 2 deletions src/java.desktop/windows/native/libawt/windows/ShellFolder2.cpp
Expand Up @@ -980,10 +980,20 @@ JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_extractIcon
UINT uFlags = getDefaultIcon ? GIL_DEFAULTICON : GIL_FORSHELL | GIL_ASYNC;
hres = pIcon->GetIconLocation(uFlags, szBuf, MAX_PATH, &index, &flags);
if (SUCCEEDED(hres)) {
UINT iconSize;
HICON hIconSmall;
if (size < 24) {
size = 16;
iconSize = (size << 16) + 32;
} else {
iconSize = (16 << 16) + size;
}
hres = pIcon->Extract(szBuf, index, &hIcon, &hIconSmall, iconSize);
if (size < 24) {
fn_DestroyIcon((HICON)hIcon);
hIcon = hIconSmall;
} else {
fn_DestroyIcon((HICON)hIconSmall);
}
hres = pIcon->Extract(szBuf, index, &hIcon, NULL, size);
} else if (hres == E_PENDING) {
pIcon->Release();
return E_PENDING;
Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;
import java.awt.Image;
import java.awt.image.MultiResolutionImage;
import java.io.File;
import java.io.IOException;

/*
* @test
* @bug 8282526
* @summary Default icon is not painted properly
* @requires (os.family == "windows")
* @run main WindowsDefaultIconSizeTest
*/

public class WindowsDefaultIconSizeTest {
public static void main(String[] args) {
WindowsDefaultIconSizeTest test = new WindowsDefaultIconSizeTest();
test.test();
}

public void test() {
String sep = System.getProperty("file.separator");
String dir = System.getProperty("test.src", ".");
String filename = "test.not";

File testFile = new File(dir + sep + filename);
try {
if (!testFile.exists()) {
testFile.createNewFile();
testFile.deleteOnExit();
}
FileSystemView fsv = FileSystemView.getFileSystemView();
Icon icon = fsv.getSystemIcon(new File(dir + sep + filename));
if (icon instanceof ImageIcon) {
Image image = ((ImageIcon) icon).getImage();
if (image instanceof MultiResolutionImage) {
Image variant = ((MultiResolutionImage) image).getResolutionVariant(16, 16);
if (variant.getWidth(null) != 16) {
throw new RuntimeException("Default file icon has size of " +
variant.getWidth(null) + " instead of 16");
}
}
}
} catch (IOException ioe) {
throw new RuntimeException("Unexpected error while creating the test file: " + ioe.getLocalizedMessage());
}
}
}

0 comments on commit 6c8d0e6

Please sign in to comment.