1 18 package org.apache.batik.svggen; 19 20 import java.awt.*; 21 import java.awt.geom.*; 22 import java.awt.image.*; 23 24 32 public class Clip implements Painter { 33 public void paint(Graphics2D g) { 34 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 35 RenderingHints.VALUE_ANTIALIAS_ON); 36 37 Shape clipShape = g.getClip(); 39 java.awt.geom.AffineTransform transform = g.getTransform(); 40 41 g.setPaint(Color.black); 42 43 Dimension size = new Dimension(300, 400); 44 int w=100, h=50; 45 int vOffset = h + 20; 46 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 47 Graphics2D gi = image.createGraphics(); 48 gi.setPaint(Color.white); 49 gi.fillRect(0, 0, 100, 50); 50 gi.setPaint(Color.green); 51 gi.fillRect(0, 0, 50, 25); 52 gi.setPaint(Color.black); 53 gi.fillRect(50, 0, 50, 25); 54 gi.setPaint(Color.red); 55 gi.fillRect(50, 25, 50, 25); 56 gi.dispose(); 57 58 g.clipRect(0, 0, size.width, size.height); 60 g.drawImage(image, 0, 0, null); 61 g.setClip(clipShape); 62 63 g.drawString("Clip set to device bounds", 110, 25); 64 65 g.translate(0, vOffset); 66 67 g.drawString("Clip set to upper right quarter", 110, 25); 70 71 g.clipRect(w/2, 0, w/2, h/2); 72 g.drawImage(image, 0, 0, null); 73 74 g.setTransform(transform); 76 g.setClip(clipShape); 77 g.translate(0, 2*vOffset); 78 79 g.drawString("Clip set to upper right quarter", 110, 15); 81 g.drawString("after .5 scale", 110, 30); 82 g.scale(.5, .5); 83 g.clipRect(w/2, 0, w/2, h/2); 84 g.drawImage(image, 0, 0, null); 85 86 g.setTransform(transform); 88 g.setClip(clipShape); 89 90 g.translate(0, 3*vOffset); 91 92 g.drawString("Non-Rectagular clip", 110, 25); 94 Shape circle = new Ellipse2D.Float(0, 0, w, h); 95 g.clip(circle); 96 g.drawImage(image, 0, 0, null); 97 98 g.setTransform(transform); 100 g.setClip(clipShape); 101 102 g.translate(0, 4*vOffset); 103 104 g.drawString("Non-Rectagular clip after", 110, 15); 107 g.drawString(".5 scale", 110, 30); 108 g.scale(.5, .5); 109 g.clip(circle); 110 g.drawImage(image, 0, 0, null); 111 112 g.setTransform(transform); 114 g.setClip(clipShape); 115 g.translate(0, 5*vOffset); 116 117 g.drawString("Non-Rectagular clip before", 110, 15); 120 g.drawString(".5 scale", 110, 30); 121 g.clip(circle); 122 g.scale(.5, .5); 123 g.drawImage(image, 0, 0, null); 124 } 125 } 126 | Popular Tags |