001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.svggen;
020:
021: import java.awt.*;
022: import java.awt.geom.*;
023: import java.awt.image.*;
024:
025: /**
026: * This test validates convertion of Java 2D clip inot SVG clipPath
027: * definition and attributes.
028: *
029: * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
030: * @author <a href="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
031: * @version $Id: Clip.java 475477 2006-11-15 22:44:28Z cam $
032: */
033: public class Clip implements Painter {
034: public void paint(Graphics2D g) {
035: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
036: RenderingHints.VALUE_ANTIALIAS_ON);
037:
038: // Save original clip
039: Shape clipShape = g.getClip();
040: java.awt.geom.AffineTransform transform = g.getTransform();
041:
042: g.setPaint(Color.black);
043:
044: Dimension size = new Dimension(300, 400);
045: int w = 100, h = 50;
046: int vOffset = h + 20;
047: BufferedImage image = new BufferedImage(w, h,
048: BufferedImage.TYPE_INT_RGB);
049: Graphics2D gi = image.createGraphics();
050: gi.setPaint(Color.white);
051: gi.fillRect(0, 0, 100, 50);
052: gi.setPaint(Color.green);
053: gi.fillRect(0, 0, 50, 25);
054: gi.setPaint(Color.black);
055: gi.fillRect(50, 0, 50, 25);
056: gi.setPaint(Color.red);
057: gi.fillRect(50, 25, 50, 25);
058: gi.dispose();
059:
060: // Set simple clip : does not modify the output
061: g.clipRect(0, 0, size.width, size.height);
062: g.drawImage(image, 0, 0, null);
063: g.setClip(clipShape);
064:
065: g.drawString("Clip set to device bounds", 110, 25);
066:
067: g.translate(0, vOffset);
068:
069: // Intersect current clip with a smaller clip : show only
070: // the top right corner of the image
071: g.drawString("Clip set to upper right quarter", 110, 25);
072:
073: g.clipRect(w / 2, 0, w / 2, h / 2);
074: g.drawImage(image, 0, 0, null);
075:
076: // Restore
077: g.setTransform(transform);
078: g.setClip(clipShape);
079: g.translate(0, 2 * vOffset);
080:
081: // Scale before setting the same clip
082: g.drawString("Clip set to upper right quarter", 110, 15);
083: g.drawString("after .5 scale", 110, 30);
084: g.scale(.5, .5);
085: g.clipRect(w / 2, 0, w / 2, h / 2);
086: g.drawImage(image, 0, 0, null);
087:
088: // Restore
089: g.setTransform(transform);
090: g.setClip(clipShape);
091:
092: g.translate(0, 3 * vOffset);
093:
094: // Use a non-rectangle clipping area
095: g.drawString("Non-Rectagular clip", 110, 25);
096: Shape circle = new Ellipse2D.Float(0, 0, w, h);
097: g.clip(circle);
098: g.drawImage(image, 0, 0, null);
099:
100: // Restore
101: g.setTransform(transform);
102: g.setClip(clipShape);
103:
104: g.translate(0, 4 * vOffset);
105:
106: // Use a non-rectangle clipping area again,
107: // after setting a scale transform
108: g.drawString("Non-Rectagular clip after", 110, 15);
109: g.drawString(".5 scale", 110, 30);
110: g.scale(.5, .5);
111: g.clip(circle);
112: g.drawImage(image, 0, 0, null);
113:
114: // Restore
115: g.setTransform(transform);
116: g.setClip(clipShape);
117: g.translate(0, 5 * vOffset);
118:
119: // Use a non-rectangle clipping area again,
120: // before setting a scale transform
121: g.drawString("Non-Rectagular clip before", 110, 15);
122: g.drawString(".5 scale", 110, 30);
123: g.clip(circle);
124: g.scale(.5, .5);
125: g.drawImage(image, 0, 0, null);
126: }
127: }
|