/* * @(#)Textures.java 1.18 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Paint; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.awt.font.TextLayout; import java.awt.font.FontRenderContext; import J2DCanvas; /** * TexturePaint of image, text and shapes. */ public class Textures extends J2DCanvas { public Textures() { setBackground(Color.white); } public void drawDemo(Graphics2D g2) { Image img = getImage("images/HotJava-16.gif"); int iw = img.getWidth(this); int ih = img.getHeight(this); BufferedImage bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB); Graphics2D gi = bi.createGraphics(); gi.setBackground(Color.white); gi.clearRect(0,0,iw,ih); gi.drawImage(img, 0, 0, this); TexturePaint tp_img = new TexturePaint(bi,new Rectangle(0,0,iw,ih)); bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); gi = bi.createGraphics(); gi.setBackground(Color.white); gi.clearRect(0,0,5,5); gi.setColor(Color.green); gi.fill(new Ellipse2D.Float(0,0,5,5)); TexturePaint tp_geom1 = new TexturePaint(bi,new Rectangle(0,0,5,5)); bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); gi = bi.createGraphics(); gi.setColor(Color.black); gi.fillRect(0,0,5,5); gi.setColor(Color.gray); gi.fillRect(1,1,4,4); TexturePaint tp_geom2 = new TexturePaint(bi,new Rectangle(0,0,5,5)); FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Times New Roman Italic", Font.PLAIN, 10); TextLayout tl = new TextLayout("Java2D", f,frc); int sw = (int) tl.getBounds().getWidth(); int sh = (int) (tl.getAscent()+tl.getDescent()); bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB); gi = bi.createGraphics(); gi.setBackground(Color.white); gi.clearRect(0,0,sw,sh); gi.setColor(Color.gray); tl.draw(gi, 0, sh-tl.getDescent()); TexturePaint tp_txt = new TexturePaint(bi,new Rectangle(0,0,sw,sh)); Rectangle r = new Rectangle(iw,ih,w/2-iw*2,h/2-ih*2); g2.setPaint(tp_img); g2.fill(r); g2.setPaint(tp_txt); g2.setStroke(new BasicStroke(10)); g2.draw(r); f = new Font("Times New Roman", Font.BOLD, w/13); tl = new TextLayout("Texture", f, frc); sw = (int) tl.getBounds().getWidth(); Shape sha = tl.getOutline(null,(float) (w/2+w/4-sw/2), (float) (h/4 - tl.getDescent())); g2.setPaint(tp_geom2); g2.fill(sha); f = new Font("Times New Roman", Font.BOLD, w/12); tl = new TextLayout("Paint", f, frc); sw = (int) tl.getBounds().getWidth(); sha = tl.getOutline(null,(float) (w/2+w/4-sw/2), //(float) (h/4 + tl.getAscent() + tl.getDescent() + 5)); (float) (h/4 + tl.getBounds().getHeight())); g2.setStroke(new BasicStroke(1.5f)); g2.setColor(Color.black); g2.draw(sha); g2.setPaint(tp_geom1); g2.fill(sha); Ellipse2D e1 = new Ellipse2D.Float(iw, h/2+ih, w/2-iw*2, h/2-ih*2); g2.setStroke(new BasicStroke(10.0f)); g2.setPaint(tp_geom1); g2.fill(e1); g2.setPaint(tp_img); g2.draw(e1); g2.setPaint(tp_txt); Ellipse2D e2 = new Ellipse2D.Float(iw+w/2, h/2+ih, w/2-iw*2, h/2-ih*2); g2.fill(e2); g2.setPaint(tp_geom2); g2.draw(e2); } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("Java2D Demo - Textures"); f.addWindowListener(l); f.add("Center", new Textures()); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } }