| 1 |
package org.jfree.svg.demo; |
| 2 |
|
| 3 |
import java.awt.Color; |
| 4 |
import java.awt.GradientPaint; |
| 5 |
import java.awt.Graphics2D; |
| 6 |
import java.awt.LinearGradientPaint; |
| 7 |
import java.awt.MultipleGradientPaint; |
| 8 |
import java.awt.MultipleGradientPaint.CycleMethod; |
| 9 |
import java.awt.RadialGradientPaint; |
| 10 |
import java.awt.Rectangle; |
| 11 |
import java.awt.geom.AffineTransform; |
| 12 |
import java.awt.geom.Ellipse2D; |
| 13 |
import java.awt.geom.Path2D; |
| 14 |
import java.awt.geom.Rectangle2D; |
| 15 |
import java.awt.image.BufferedImage; |
| 16 |
import java.io.File; |
| 17 |
import java.io.IOException; |
| 18 |
import javax.imageio.ImageIO; |
| 19 |
import org.jfree.svg.SVGGraphics2D; |
| 20 |
import org.jfree.svg.SVGUtils; |
| 21 |
|
| 22 |
/** |
| 23 |
* |
| 24 |
* @author dgilbert |
| 25 |
*/ |
| 26 |
public class ImageTest { |
| 27 |
|
| 28 |
private static void drawClipTest(Graphics2D g2) { |
| 29 |
g2.translate(10, 20); |
| 30 |
g2.setColor(Color.RED); |
| 31 |
g2.fillRect(10, 10, 100, 100); |
| 32 |
g2.clip(new Rectangle(0, 0, 60, 60)); |
| 33 |
g2.setPaint(Color.BLUE); |
| 34 |
g2.fillRect(10, 10, 100, 100); |
| 35 |
g2.setClip(null); |
| 36 |
g2.setPaint(Color.GREEN); |
| 37 |
g2.fillRect(60, 60, 50, 50); |
| 38 |
} |
| 39 |
|
| 40 |
private static void drawGradientPaintTest(Graphics2D g2) { |
| 41 |
g2.setPaint(new GradientPaint(10f, 10f, Color.RED, 10f, 60f, Color.YELLOW)); |
| 42 |
g2.fillRect(10, 10, 50, 50); |
| 43 |
g2.setPaint(Color.BLUE); |
| 44 |
g2.fillRect(60, 10, 50, 50); |
| 45 |
} |
| 46 |
|
| 47 |
private static void drawLinearGradientPaintTest(Graphics2D g2) { |
| 48 |
// top left |
| 49 |
LinearGradientPaint lgp = new LinearGradientPaint(10, 30, 50, 30, new float[] {0.0f, 1.0f}, new Color[] {Color.RED, Color.BLUE}); |
| 50 |
g2.setPaint(lgp); |
| 51 |
g2.fill(new Rectangle2D.Double(10, 10, 40, 40)); |
| 52 |
|
| 53 |
// top right |
| 54 |
lgp = new LinearGradientPaint(80, 10, 80, 50, new float[] {0.0f, 1.0f}, new Color[] {Color.RED, Color.BLUE}); |
| 55 |
g2.setPaint(lgp); |
| 56 |
g2.fill(new Rectangle2D.Double(60, 10, 40, 40)); |
| 57 |
|
| 58 |
// bottom left |
| 59 |
lgp = new LinearGradientPaint(10, 100, 50, 60, new float[] {0.0f, 1.0f}, new Color[] {Color.RED, Color.BLUE}); |
| 60 |
g2.setPaint(lgp); |
| 61 |
g2.fill(new Rectangle2D.Double(10, 60, 40, 40)); |
| 62 |
|
| 63 |
// bottom right |
| 64 |
lgp = new LinearGradientPaint(70, 70, 90, 90, new float[] {0.0f, 0.5f, 1.0f}, new Color[] {Color.RED, Color.YELLOW, Color.BLUE}, CycleMethod.REPEAT); |
| 65 |
g2.setPaint(lgp); |
| 66 |
g2.fill(new Rectangle2D.Double(60, 60, 40, 40)); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
private static void drawOldLinearGradientPaintTest(Graphics2D g2) { |
| 71 |
// top left |
| 72 |
GradientPaint lgp = new GradientPaint(10, 30, Color.RED, 50, 30, Color.BLUE); |
| 73 |
g2.setPaint(lgp); |
| 74 |
g2.fill(new Rectangle2D.Double(10, 10, 40, 40)); |
| 75 |
|
| 76 |
// top right |
| 77 |
lgp = new GradientPaint(80, 10, Color.RED, 80, 50, Color.BLUE); |
| 78 |
g2.setPaint(lgp); |
| 79 |
g2.fill(new Rectangle2D.Double(60, 10, 40, 40)); |
| 80 |
|
| 81 |
// bottom left |
| 82 |
lgp = new GradientPaint(10, 100, Color.RED, 50, 60, Color.BLUE); |
| 83 |
g2.setPaint(lgp); |
| 84 |
g2.fill(new Rectangle2D.Double(10, 60, 40, 40)); |
| 85 |
|
| 86 |
// bottom right |
| 87 |
lgp = new GradientPaint(70, 70, Color.RED, 90, 90, Color.BLUE); |
| 88 |
g2.setPaint(lgp); |
| 89 |
g2.fill(new Rectangle2D.Double(60, 60, 40, 40)); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
private static void drawRadialGradientPaintTest(Graphics2D g2) { |
| 94 |
RadialGradientPaint rgp = new RadialGradientPaint(50, 50, 40, 30, 30, |
| 95 |
new float[] {0f, 0.75f, 1f}, new Color[] {Color.RED, |
| 96 |
Color.GREEN, Color.BLUE}, |
| 97 |
MultipleGradientPaint.CycleMethod.NO_CYCLE); |
| 98 |
|
| 99 |
g2.setPaint(rgp); |
| 100 |
Ellipse2D circle = new Ellipse2D.Double(10, 10, 80, 80); |
| 101 |
g2.fill(circle); |
| 102 |
} |
| 103 |
|
| 104 |
private static void drawArcTest(Graphics2D g2) { |
| 105 |
g2.setPaint(Color.GREEN); |
| 106 |
g2.drawRect(0, 20, 70, 50); |
| 107 |
g2.setPaint(Color.RED); |
| 108 |
Path2D path1 = new Path2D.Double(); |
| 109 |
double[] pts = calculateReferenceArc(90); |
| 110 |
path1.moveTo(pts[0], pts[1]); |
| 111 |
path1.curveTo(pts[2], pts[3], pts[4], pts[5], pts[6], pts[7]); |
| 112 |
AffineTransform t = new AffineTransform(); |
| 113 |
t.translate(35, 45); |
| 114 |
t.scale(35, 25); |
| 115 |
t.rotate(Math.PI / 4); |
| 116 |
path1.transform(t); |
| 117 |
g2.draw(path1); |
| 118 |
|
| 119 |
Path2D path2 = new Path2D.Double(); |
| 120 |
path2.moveTo(pts[0], pts[1]); |
| 121 |
path2.curveTo(pts[2], pts[3], pts[4], pts[5], pts[6], pts[7]); |
| 122 |
AffineTransform t2 = new AffineTransform(); |
| 123 |
t2.rotate(3 * Math.PI / 4); |
| 124 |
t2.scale(35, 25); |
| 125 |
t2.translate(35, 35); |
| 126 |
path2.transform(t2); |
| 127 |
//g2.draw(path2); |
| 128 |
Path2D arc = new Path2D.Double(); |
| 129 |
arc.append(path1, false); |
| 130 |
arc.append(path2, false); |
| 131 |
//g2.draw(arc); |
| 132 |
//g2.draw(path1); |
| 133 |
//g2.transform(t); |
| 134 |
g2.setPaint(Color.BLUE); |
| 135 |
g2.drawArc(0, 20, 70, 50, 0, -270); |
| 136 |
//Arc2D arc2d = new Arc2D.Double(0d, 20d, 70d, 50d, 0d, 90, Arc2D.OPEN); |
| 137 |
//g2.draw(arc2d); |
| 138 |
} |
| 139 |
|
| 140 |
private static double[] calculateReferenceArc(double angle) { |
| 141 |
double a = (angle / 180 * Math.PI) / 2.0; |
| 142 |
double x0 = Math.cos(a); |
| 143 |
double y0 = Math.sin(a); |
| 144 |
double x1 = (4 - x0) / 3; |
| 145 |
double y1 = (1 - x0) * (3 - x0) / (3 * y0); |
| 146 |
double x2 = x1; |
| 147 |
double y2 = - y1; |
| 148 |
double x3 = x0; |
| 149 |
double y3 = -y0; |
| 150 |
return new double[] { x0, y0, x1, y1, x2, y2, x3, y3 }; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Starting point for the demo. |
| 155 |
* |
| 156 |
* @param args ignored. |
| 157 |
* |
| 158 |
* @throws IOException |
| 159 |
*/ |
| 160 |
public static void main(String[] args) throws IOException { |
| 161 |
// BufferedImage image = new BufferedImage(200, 200, |
| 162 |
// BufferedImage.TYPE_INT_ARGB); |
| 163 |
// Graphics2D g2 = image.createGraphics(); |
| 164 |
|
| 165 |
SVGGraphics2D g2 = new SVGGraphics2D(110, 110); |
| 166 |
//drawClipTest(g2); |
| 167 |
//drawGradientPaintTest(g2); |
| 168 |
drawLinearGradientPaintTest(g2); |
| 169 |
//drawOldLinearGradientPaintTest(g2); |
| 170 |
//drawRadialGradientPaintTest(g2); |
| 171 |
//drawArcTest(g2); |
| 172 |
// ImageIO.write(image, "png", new File("oldlgp-test.png")); |
| 173 |
SVGUtils.writeToSVG(new File("lgp-test.svg"), g2.getSVGElement()); |
| 174 |
} |
| 175 |
} |