1 /* 2 * 3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package java2d.demos.Paint; 33 34 35 import static java.awt.Color.BLACK; 36 import static java.awt.Color.GRAY; 37 import static java.awt.Color.GREEN; 38 import static java.awt.Color.LIGHT_GRAY; 39 import static java.awt.Color.WHITE; 40 import java.awt.BasicStroke; 41 import java.awt.Color; 42 import java.awt.Font; 43 import java.awt.GradientPaint; 44 import java.awt.Graphics2D; 45 import java.awt.Rectangle; 46 import java.awt.Shape; 47 import java.awt.TexturePaint; 48 import java.awt.font.TextLayout; 49 import java.awt.geom.AffineTransform; 50 import java.awt.geom.GeneralPath; 51 import java.awt.image.BufferedImage; 52 import java2d.Surface; 53 54 55 /** 56 * TexturePaint of gradient, buffered image and shapes. 57 */ 58 @SuppressWarnings("serial") 59 public class Texture extends Surface { 60 61 private static TexturePaint bluedots, greendots, triangles; 62 private static TexturePaint blacklines, gradient; 63 64 static { 65 BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); 66 Graphics2D gi = bi.createGraphics(); 67 gi.setBackground(WHITE); 68 gi.clearRect(0, 0, 10, 10); 69 GeneralPath p1 = new GeneralPath(); 70 p1.moveTo(0, 0); 71 p1.lineTo(5, 10); 72 p1.lineTo(10, 0); 73 p1.closePath(); 74 gi.setColor(LIGHT_GRAY); 75 gi.fill(p1); 76 triangles = new TexturePaint(bi, new Rectangle(0, 0, 10, 10)); 77 78 bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); 79 gi = bi.createGraphics(); 80 gi.setColor(BLACK); 81 gi.fillRect(0, 0, 5, 5); 82 gi.setColor(GRAY); 83 gi.fillRect(1, 1, 4, 4); 84 blacklines = new TexturePaint(bi, new Rectangle(0, 0, 5, 5)); 85 86 int w = 30; 87 int h = 30; 88 bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 89 gi = bi.createGraphics(); 90 Color oc = WHITE; 91 Color ic = LIGHT_GRAY; 92 gi.setPaint(new GradientPaint(0, 0, oc, w * .35f, h * .35f, ic)); 93 gi.fillRect(0, 0, w / 2, h / 2); 94 gi.setPaint(new GradientPaint(w, 0, oc, w * .65f, h * .35f, ic)); 95 gi.fillRect(w / 2, 0, w / 2, h / 2); 96 gi.setPaint(new GradientPaint(0, h, oc, w * .35f, h * .65f, ic)); 97 gi.fillRect(0, h / 2, w / 2, h / 2); 98 gi.setPaint(new GradientPaint(w, h, oc, w * .65f, h * .65f, ic)); 99 gi.fillRect(w / 2, h / 2, w / 2, h / 2); 100 gradient = new TexturePaint(bi, new Rectangle(0, 0, w, h)); 101 102 bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); 103 bi.setRGB(0, 0, 0xffffffff); 104 bi.setRGB(1, 0, 0xffffffff); 105 bi.setRGB(0, 1, 0xffffffff); 106 bi.setRGB(1, 1, 0xff0000ff); 107 bluedots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2)); 108 109 bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); 110 bi.setRGB(0, 0, 0xffffffff); 111 bi.setRGB(1, 0, 0xffffffff); 112 bi.setRGB(0, 1, 0xffffffff); 113 bi.setRGB(1, 1, 0xff00ff00); 114 greendots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2)); 115 } 116 117 public Texture() { 118 setBackground(WHITE); 119 } 120 121 @Override 122 public void render(int w, int h, Graphics2D g2) { 123 124 Rectangle r = new Rectangle(10, 10, w - 20, h / 2 - 20); 125 g2.setPaint(gradient); 126 g2.fill(r); 127 g2.setPaint(GREEN); 128 g2.setStroke(new BasicStroke(20)); 129 g2.draw(r); 130 g2.setPaint(blacklines); 131 g2.setStroke(new BasicStroke(15)); 132 g2.draw(r); 133 134 Font f = new Font(Font.SERIF, Font.BOLD, w / 5); 135 TextLayout tl = new TextLayout("Texture", f, g2.getFontRenderContext()); 136 int sw = (int) tl.getBounds().getWidth(); 137 int sh = (int) tl.getBounds().getHeight(); 138 Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw 139 / 2, h * .25 + sh / 2)); 140 g2.setColor(BLACK); 141 g2.setStroke(new BasicStroke(3)); 142 g2.draw(sha); 143 g2.setPaint(greendots); 144 g2.fill(sha); 145 146 r.setLocation(10, h / 2 + 10); 147 g2.setPaint(triangles); 148 g2.fill(r); 149 g2.setPaint(blacklines); 150 g2.setStroke(new BasicStroke(20)); 151 g2.draw(r); 152 g2.setPaint(GREEN); 153 g2.setStroke(new BasicStroke(4)); 154 g2.draw(r); 155 156 f = new Font(Font.SERIF, Font.BOLD, w / 4); 157 tl = new TextLayout("Paint", f, g2.getFontRenderContext()); 158 sw = (int) tl.getBounds().getWidth(); 159 sh = (int) tl.getBounds().getHeight(); 160 sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw / 2, h 161 * .75 + sh / 2)); 162 g2.setColor(BLACK); 163 g2.setStroke(new BasicStroke(5)); 164 g2.draw(sha); 165 g2.setPaint(bluedots); 166 g2.fill(sha); 167 } 168 169 public static void main(String s[]) { 170 createDemoFrame(new Texture()); 171 } 172 }