fixed sub-pixel text rendering in animated theme change; use weak hash map for static map to avoid memory leak for the case that something went wrong

This commit is contained in:
Karl Tauber
2020-08-31 16:20:20 +02:00
parent 6da220f36c
commit faffc9393d
2 changed files with 23 additions and 6 deletions

View File

@@ -24,6 +24,8 @@ FlatLaf Change Log
window is inactive.
- Custom window decorations: Fixed title pane background color in IntelliJ
themes if window is inactive.
- Fixed sub-pixel text rendering in animated theme change (see
[FlatLaf Extras](flatlaf-extras)).
#### Other Changes

View File

@@ -19,10 +19,10 @@ package com.formdev.flatlaf.extras;
import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Window;
import java.util.HashMap;
import java.awt.image.VolatileImage;
import java.util.Map;
import java.util.WeakHashMap;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.RootPaneContainer;
@@ -56,7 +56,7 @@ public class FlatAnimatedLafChange
public static int resolution = 40;
private static Animator animator;
private static final Map<JLayeredPane, JComponent> map = new HashMap<>();
private static final Map<JLayeredPane, JComponent> map = new WeakHashMap<>();
private static float alpha;
/**
@@ -79,10 +79,14 @@ public class FlatAnimatedLafChange
if( !(window instanceof RootPaneContainer) || !window.isShowing() )
continue;
JLayeredPane layeredPane = ((RootPaneContainer)window).getLayeredPane();
// create snapshot image
// (using volatile image to have correct sub-pixel text rendering on Java 9+)
VolatileImage snapshot = window.createVolatileImage( window.getWidth(), window.getHeight() );
if( snapshot == null )
continue;
// create snapshot image of layered pane
Image snapshot = window.createImage( window.getWidth(), window.getHeight() );
// paint window to snapshot image
JLayeredPane layeredPane = ((RootPaneContainer)window).getLayeredPane();
layeredPane.paint( snapshot.getGraphics() );
// create snapshot layer, which is added to layered pane and paints
@@ -90,9 +94,20 @@ public class FlatAnimatedLafChange
JComponent snapshotLayer = new JComponent() {
@Override
public void paint( Graphics g ) {
if( snapshot.contentsLost() )
return;
((Graphics2D)g).setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) );
g.drawImage( snapshot, 0, 0, null );
}
@Override
public void removeNotify() {
super.removeNotify();
// release system resources used by volatile image
snapshot.flush();
}
};
snapshotLayer.setSize( layeredPane.getSize() );