| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import java.awt.*; |
| 20 |
import java.util.Map; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class GraphicsUtil { |
| 26 |
public static void setupAntialiasing(Graphics g2) { |
| 27 |
setupAntialiasing(g2, true, false); |
| 28 |
} |
| 29 |
|
| 30 |
public static void setupAntialiasing(Graphics g2, boolean enableAA, boolean ignoreSystemSettings) { |
| 31 |
if (g2 instanceof Graphics2D) { |
| 32 |
Graphics2D g = (Graphics2D) g2; |
| 33 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
| 34 |
|
| 35 |
Map map = (Map) tk.getDesktopProperty("awt.font.desktophints"); |
| 36 |
|
| 37 |
if (map != null && !ignoreSystemSettings) { |
| 38 |
g.addRenderingHints(map); |
| 39 |
} else { |
| 40 |
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 41 |
enableAA ? RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public static GraphicsConfig setupAAPainting(Graphics g) { |
| 47 |
final GraphicsConfig config = new GraphicsConfig(g); |
| 48 |
final Graphics2D g2 = (Graphics2D) g; |
| 49 |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 50 |
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); |
| 51 |
return config; |
| 52 |
} |
| 53 |
|
| 54 |
public static GraphicsConfig paintWithAlpha(Graphics g, float alpha) { |
| 55 |
assert 0.0f <= alpha && alpha <= 1.0f : "alpha should be in range 0.0f .. 1.0f"; |
| 56 |
final GraphicsConfig config = new GraphicsConfig(g); |
| 57 |
final Graphics2D g2 = (Graphics2D) g; |
| 58 |
|
| 59 |
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); |
| 60 |
return config; |
| 61 |
} |
| 62 |
} |