Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Paint/Texture.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Paint;323334import static java.awt.Color.BLACK;35import static java.awt.Color.GRAY;36import static java.awt.Color.GREEN;37import static java.awt.Color.LIGHT_GRAY;38import static java.awt.Color.WHITE;39import java.awt.BasicStroke;40import java.awt.Color;41import java.awt.Font;42import java.awt.GradientPaint;43import java.awt.Graphics2D;44import java.awt.Rectangle;45import java.awt.Shape;46import java.awt.TexturePaint;47import java.awt.font.TextLayout;48import java.awt.geom.AffineTransform;49import java.awt.geom.GeneralPath;50import java.awt.image.BufferedImage;51import java2d.Surface;525354/**55* TexturePaint of gradient, buffered image and shapes.56*/57@SuppressWarnings("serial")58public class Texture extends Surface {5960private static TexturePaint bluedots, greendots, triangles;61private static TexturePaint blacklines, gradient;6263static {64BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);65Graphics2D gi = bi.createGraphics();66gi.setBackground(WHITE);67gi.clearRect(0, 0, 10, 10);68GeneralPath p1 = new GeneralPath();69p1.moveTo(0, 0);70p1.lineTo(5, 10);71p1.lineTo(10, 0);72p1.closePath();73gi.setColor(LIGHT_GRAY);74gi.fill(p1);75triangles = new TexturePaint(bi, new Rectangle(0, 0, 10, 10));7677bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);78gi = bi.createGraphics();79gi.setColor(BLACK);80gi.fillRect(0, 0, 5, 5);81gi.setColor(GRAY);82gi.fillRect(1, 1, 4, 4);83blacklines = new TexturePaint(bi, new Rectangle(0, 0, 5, 5));8485int w = 30;86int h = 30;87bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);88gi = bi.createGraphics();89Color oc = WHITE;90Color ic = LIGHT_GRAY;91gi.setPaint(new GradientPaint(0, 0, oc, w * .35f, h * .35f, ic));92gi.fillRect(0, 0, w / 2, h / 2);93gi.setPaint(new GradientPaint(w, 0, oc, w * .65f, h * .35f, ic));94gi.fillRect(w / 2, 0, w / 2, h / 2);95gi.setPaint(new GradientPaint(0, h, oc, w * .35f, h * .65f, ic));96gi.fillRect(0, h / 2, w / 2, h / 2);97gi.setPaint(new GradientPaint(w, h, oc, w * .65f, h * .65f, ic));98gi.fillRect(w / 2, h / 2, w / 2, h / 2);99gradient = new TexturePaint(bi, new Rectangle(0, 0, w, h));100101bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);102bi.setRGB(0, 0, 0xffffffff);103bi.setRGB(1, 0, 0xffffffff);104bi.setRGB(0, 1, 0xffffffff);105bi.setRGB(1, 1, 0xff0000ff);106bluedots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2));107108bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);109bi.setRGB(0, 0, 0xffffffff);110bi.setRGB(1, 0, 0xffffffff);111bi.setRGB(0, 1, 0xffffffff);112bi.setRGB(1, 1, 0xff00ff00);113greendots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2));114}115116public Texture() {117setBackground(WHITE);118}119120@Override121public void render(int w, int h, Graphics2D g2) {122123Rectangle r = new Rectangle(10, 10, w - 20, h / 2 - 20);124g2.setPaint(gradient);125g2.fill(r);126g2.setPaint(GREEN);127g2.setStroke(new BasicStroke(20));128g2.draw(r);129g2.setPaint(blacklines);130g2.setStroke(new BasicStroke(15));131g2.draw(r);132133Font f = new Font(Font.SERIF, Font.BOLD, w / 5);134TextLayout tl = new TextLayout("Texture", f, g2.getFontRenderContext());135int sw = (int) tl.getBounds().getWidth();136int sh = (int) tl.getBounds().getHeight();137Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw138/ 2, h * .25 + sh / 2));139g2.setColor(BLACK);140g2.setStroke(new BasicStroke(3));141g2.draw(sha);142g2.setPaint(greendots);143g2.fill(sha);144145r.setLocation(10, h / 2 + 10);146g2.setPaint(triangles);147g2.fill(r);148g2.setPaint(blacklines);149g2.setStroke(new BasicStroke(20));150g2.draw(r);151g2.setPaint(GREEN);152g2.setStroke(new BasicStroke(4));153g2.draw(r);154155f = new Font(Font.SERIF, Font.BOLD, w / 4);156tl = new TextLayout("Paint", f, g2.getFontRenderContext());157sw = (int) tl.getBounds().getWidth();158sh = (int) tl.getBounds().getHeight();159sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw / 2, h160* .75 + sh / 2));161g2.setColor(BLACK);162g2.setStroke(new BasicStroke(5));163g2.draw(sha);164g2.setPaint(bluedots);165g2.fill(sha);166}167168public static void main(String[] s) {169createDemoFrame(new Texture());170}171}172173174