home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Paint / Texture.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.9 KB  |  163 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Texture.java    1.29 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Paint;
  41.  
  42.  
  43. import java.awt.*;
  44. import java.awt.geom.Ellipse2D;
  45. import java.awt.geom.GeneralPath;
  46. import java.awt.geom.AffineTransform;
  47. import java.awt.image.BufferedImage;
  48. import java.awt.font.TextLayout;
  49. import java.awt.font.FontRenderContext;
  50. import java2d.Surface;
  51.  
  52.  
  53. /**
  54.  * TexturePaint of gradient, buffered image and shapes.
  55.  */
  56. public class Texture extends Surface {
  57.  
  58.     private static TexturePaint bluedots, greendots, triangles;
  59.     private static TexturePaint blacklines, gradient;
  60.     static {
  61.         BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
  62.         Graphics2D gi = bi.createGraphics();
  63.         gi.setBackground(Color.white);
  64.         gi.clearRect(0,0,10,10);
  65.         GeneralPath p1 = new GeneralPath();
  66.         p1.moveTo(0,0);
  67.         p1.lineTo(5,10);
  68.         p1.lineTo(10,0);
  69.         p1.closePath();
  70.         gi.setColor(Color.lightGray);
  71.         gi.fill(p1);
  72.         triangles = new TexturePaint(bi,new Rectangle(0,0,10,10));
  73.  
  74.         bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  75.         gi = bi.createGraphics();
  76.         gi.setColor(Color.black);
  77.         gi.fillRect(0,0,5,5);
  78.         gi.setColor(Color.gray);
  79.         gi.fillRect(1,1,4,4);
  80.         blacklines = new TexturePaint(bi,new Rectangle(0,0,5,5));
  81.  
  82.         int w = 30; int h = 30;
  83.         bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  84.         gi = bi.createGraphics();
  85.         Color oc = Color.white; Color ic = Color.lightGray;
  86.         gi.setPaint(new GradientPaint(0,0,oc,w*.35f,h*.35f,ic));
  87.         gi.fillRect(0, 0, w/2, h/2);
  88.         gi.setPaint(new GradientPaint(w,0,oc,w*.65f,h*.35f,ic));
  89.         gi.fillRect(w/2, 0, w/2, h/2);
  90.         gi.setPaint(new GradientPaint(0,h,oc,w*.35f,h*.65f,ic));
  91.         gi.fillRect(0, h/2, w/2, h/2);
  92.         gi.setPaint(new GradientPaint(w,h,oc,w*.65f,h*.65f,ic));
  93.         gi.fillRect(w/2, h/2, w/2, h/2);
  94.         gradient = new TexturePaint(bi,new Rectangle(0,0,w,h));
  95.  
  96.         bi = new BufferedImage(2,2,BufferedImage.TYPE_INT_RGB);
  97.         bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff);
  98.         bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff0000ff);
  99.         bluedots = new TexturePaint(bi,new Rectangle(0,0,2,2));
  100.  
  101.         bi = new BufferedImage(2,2,BufferedImage.TYPE_INT_RGB);
  102.         bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff);
  103.         bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff00ff00);
  104.         greendots = new TexturePaint(bi,new Rectangle(0,0,2,2));
  105.     }
  106.  
  107.  
  108.     public Texture() {
  109.         setBackground(Color.white);
  110.     }
  111.  
  112.  
  113.     public void render(int w, int h, Graphics2D g2) {
  114.  
  115.         Rectangle r = new Rectangle(10,10,w-20,h/2-20);
  116.         g2.setPaint(gradient);
  117.         g2.fill(r);
  118.         g2.setPaint(Color.green);
  119.         g2.setStroke(new BasicStroke(20));
  120.         g2.draw(r);
  121.         g2.setPaint(blacklines);
  122.         g2.setStroke(new BasicStroke(15));
  123.         g2.draw(r);
  124.  
  125.         Font f = new Font("Times New Roman", Font.BOLD, w/5);
  126.         TextLayout tl = new TextLayout("Texture", f, g2.getFontRenderContext());
  127.         int sw = (int) tl.getBounds().getWidth();
  128.         int sh = (int) tl.getBounds().getHeight();
  129.         Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.25+sh/2));
  130.         g2.setColor(Color.black);
  131.         g2.setStroke(new BasicStroke(3));
  132.         g2.draw(sha);
  133.         g2.setPaint(greendots);
  134.         g2.fill(sha);
  135.  
  136.         r.setLocation(10,h/2+10);
  137.         g2.setPaint(triangles);
  138.         g2.fill(r);
  139.         g2.setPaint(blacklines);
  140.         g2.setStroke(new BasicStroke(20));
  141.         g2.draw(r);
  142.         g2.setPaint(Color.green);
  143.         g2.setStroke(new BasicStroke(4));
  144.         g2.draw(r);
  145.  
  146.         f = new Font("serif", Font.BOLD, w/4);
  147.         tl = new TextLayout("Paint", f, g2.getFontRenderContext());
  148.         sw = (int) tl.getBounds().getWidth();
  149.         sh = (int) tl.getBounds().getHeight();
  150.         sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.75+sh/2));
  151.         g2.setColor(Color.black);
  152.         g2.setStroke(new BasicStroke(5));
  153.         g2.draw(sha);
  154.         g2.setPaint(bluedots);
  155.         g2.fill(sha);
  156.     }
  157.  
  158.  
  159.     public static void main(String s[]) {
  160.         createDemoFrame(new Texture());
  161.     }
  162. }
  163.