| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.dishevelled.iconbundle.impl; |
| 25 | |
|
| 26 | |
import java.awt.Color; |
| 27 | |
import java.awt.Image; |
| 28 | |
import java.awt.Paint; |
| 29 | |
import java.awt.Shape; |
| 30 | |
import java.awt.Stroke; |
| 31 | |
import java.awt.Component; |
| 32 | |
import java.awt.Graphics2D; |
| 33 | |
import java.awt.RenderingHints; |
| 34 | |
|
| 35 | |
import java.awt.geom.Rectangle2D; |
| 36 | |
|
| 37 | |
import java.awt.image.BufferedImage; |
| 38 | |
|
| 39 | |
import javax.swing.UIManager; |
| 40 | |
|
| 41 | |
import org.dishevelled.iconbundle.IconSize; |
| 42 | |
import org.dishevelled.iconbundle.IconState; |
| 43 | |
import org.dishevelled.iconbundle.IconBundle; |
| 44 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public final class ShapeIconBundle |
| 52 | |
implements IconBundle |
| 53 | |
{ |
| 54 | |
|
| 55 | |
private final Shape shape; |
| 56 | |
|
| 57 | |
|
| 58 | |
private final Stroke stroke; |
| 59 | |
|
| 60 | |
|
| 61 | |
private final Paint fillPaint; |
| 62 | |
|
| 63 | |
|
| 64 | |
private final Paint strokePaint; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public ShapeIconBundle(final Shape shape, |
| 77 | |
final Stroke stroke, |
| 78 | |
final Paint fillPaint, |
| 79 | |
final Paint strokePaint) |
| 80 | 33 | { |
| 81 | 33 | if (shape == null) |
| 82 | |
{ |
| 83 | 1 | throw new IllegalArgumentException("shape must not be null"); |
| 84 | |
} |
| 85 | 32 | if (stroke == null) |
| 86 | |
{ |
| 87 | 1 | throw new IllegalArgumentException("stroke must not be null"); |
| 88 | |
} |
| 89 | 31 | if (fillPaint == null) |
| 90 | |
{ |
| 91 | 1 | throw new IllegalArgumentException("fillPaint must not be null"); |
| 92 | |
} |
| 93 | 30 | if (strokePaint == null) |
| 94 | |
{ |
| 95 | 1 | throw new IllegalArgumentException("strokePaint must not be null"); |
| 96 | |
} |
| 97 | |
|
| 98 | 29 | this.shape = shape; |
| 99 | 29 | this.stroke = stroke; |
| 100 | 29 | this.fillPaint = fillPaint; |
| 101 | 29 | this.strokePaint = strokePaint; |
| 102 | 29 | } |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public Image getImage(final Component component, |
| 107 | |
final IconTextDirection direction, |
| 108 | |
final IconState state, |
| 109 | |
final IconSize size) |
| 110 | |
{ |
| 111 | 4217 | if (direction == null) |
| 112 | |
{ |
| 113 | 1 | throw new IllegalArgumentException("direction must not be null"); |
| 114 | |
} |
| 115 | 4216 | if (state == null) |
| 116 | |
{ |
| 117 | 1 | throw new IllegalArgumentException("state must not be null"); |
| 118 | |
} |
| 119 | 4215 | if (size == null) |
| 120 | |
{ |
| 121 | 1 | throw new IllegalArgumentException("size must not be null"); |
| 122 | |
} |
| 123 | |
|
| 124 | 4214 | int h = size.getHeight(); |
| 125 | 4214 | int w = size.getWidth(); |
| 126 | |
|
| 127 | 4214 | BufferedImage image = new BufferedImage(w + 1, h + 1, BufferedImage.TYPE_INT_ARGB); |
| 128 | 4214 | Graphics2D g = image.createGraphics(); |
| 129 | |
|
| 130 | 4214 | g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
| 131 | 4214 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 132 | |
|
| 133 | 4214 | Rectangle2D bounds = shape.getBounds2D(); |
| 134 | |
|
| 135 | 4214 | double d = 1.0d; |
| 136 | 4214 | if (bounds.getWidth() >= bounds.getHeight()) |
| 137 | |
{ |
| 138 | 392 | d = w / bounds.getWidth(); |
| 139 | |
} |
| 140 | |
else |
| 141 | |
{ |
| 142 | 3822 | d = h / bounds.getHeight(); |
| 143 | |
} |
| 144 | |
|
| 145 | |
|
| 146 | 4214 | g.translate(w / 2.0d, h / 2.0d); |
| 147 | |
|
| 148 | 4214 | g.scale(d, d); |
| 149 | |
|
| 150 | 4214 | g.translate(-1 * bounds.getCenterX(), -1 * bounds.getCenterY()); |
| 151 | |
|
| 152 | 4214 | g.setPaint(fillPaint); |
| 153 | 4214 | g.fill(shape); |
| 154 | |
|
| 155 | 4214 | g.setStroke(stroke); |
| 156 | 4214 | g.setPaint(strokePaint); |
| 157 | 4214 | g.draw(shape); |
| 158 | |
|
| 159 | 4214 | g.dispose(); |
| 160 | |
|
| 161 | 4214 | if (IconState.ACTIVE == state) |
| 162 | |
{ |
| 163 | 602 | return IconBundleUtils.makeActive(image); |
| 164 | |
} |
| 165 | 3612 | else if (IconState.MOUSEOVER == state) |
| 166 | |
{ |
| 167 | 602 | return IconBundleUtils.makeMouseover(image); |
| 168 | |
} |
| 169 | 3010 | else if (IconState.SELECTED == state) |
| 170 | |
{ |
| 171 | 602 | Color selectionColor = UIManager.getColor("List.selectionBackground"); |
| 172 | 602 | return IconBundleUtils.makeSelected(image, selectionColor); |
| 173 | |
} |
| 174 | 2408 | else if (IconState.SELECTED_MOUSEOVER == state) |
| 175 | |
{ |
| 176 | 602 | Color selectionColor = UIManager.getColor("List.selectionBackground"); |
| 177 | 602 | return IconBundleUtils.makeSelectedMouseover(image, selectionColor); |
| 178 | |
} |
| 179 | 1806 | else if (IconState.DRAGGING == state) |
| 180 | |
{ |
| 181 | 602 | return IconBundleUtils.makeDragging(image); |
| 182 | |
} |
| 183 | 1204 | else if (IconState.DISABLED == state) |
| 184 | |
{ |
| 185 | 602 | return IconBundleUtils.makeDisabled(image); |
| 186 | |
} |
| 187 | |
else |
| 188 | |
{ |
| 189 | 602 | return image; |
| 190 | |
} |
| 191 | |
} |
| 192 | |
} |