Texture.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 6k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)Texture.java 1.24 99/04/23
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. package demos.Paint;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.geom.Ellipse2D;
  34. import java.awt.geom.GeneralPath;
  35. import java.awt.geom.AffineTransform;
  36. import java.awt.image.BufferedImage;
  37. import java.awt.font.TextLayout;
  38. import java.awt.font.FontRenderContext;
  39. import DemoSurface;
  40. import DemoPanel;
  41. /**
  42.  * TexturePaint of gradient, buffered image and shapes.
  43.  */
  44. public class Texture extends DemoSurface {
  45.     private static TexturePaint bluedots, greendots, triangles;
  46.     private static TexturePaint blacklines, gradient;
  47.     static {
  48.         BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
  49.         Graphics2D gi = bi.createGraphics();
  50.         gi.setBackground(Color.white);
  51.         gi.clearRect(0,0,10,10);
  52.         GeneralPath p1 = new GeneralPath();
  53.         p1.moveTo(0,0);
  54.         p1.lineTo(5,10);
  55.         p1.lineTo(10,0);
  56.         p1.closePath();
  57.         gi.setColor(Color.lightGray);
  58.         gi.fill(p1);
  59.         triangles = new TexturePaint(bi,new Rectangle(0,0,10,10));
  60.         bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  61.         gi = bi.createGraphics();
  62.         gi.setColor(Color.black);
  63.         gi.fillRect(0,0,5,5);
  64.         gi.setColor(Color.gray);
  65.         gi.fillRect(1,1,4,4);
  66.         blacklines = new TexturePaint(bi,new Rectangle(0,0,5,5));
  67.         int w = 30; int h = 30;
  68.         bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  69.         gi = bi.createGraphics();
  70.         Color oc = Color.white; Color ic = Color.lightGray;
  71.         gi.setPaint(new GradientPaint(0,0,oc,w*.35f,h*.35f,ic));
  72.         gi.fillRect(0, 0, w/2, h/2);
  73.         gi.setPaint(new GradientPaint(w,0,oc,w*.65f,h*.35f,ic));
  74.         gi.fillRect(w/2, 0, w/2, h/2);
  75.         gi.setPaint(new GradientPaint(0,h,oc,w*.35f,h*.65f,ic));
  76.         gi.fillRect(0, h/2, w/2, h/2);
  77.         gi.setPaint(new GradientPaint(w,h,oc,w*.65f,h*.65f,ic));
  78.         gi.fillRect(w/2, h/2, w/2, h/2);
  79.         gradient = new TexturePaint(bi,new Rectangle(0,0,w,h));
  80.         bi = new BufferedImage(2,2,BufferedImage.TYPE_INT_RGB);
  81.         bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff);
  82.         bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff0000ff);
  83.         bluedots = new TexturePaint(bi,new Rectangle(0,0,2,2));
  84.         bi = new BufferedImage(2,2,BufferedImage.TYPE_INT_RGB);
  85.         bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff);
  86.         bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff00ff00);
  87.         greendots = new TexturePaint(bi,new Rectangle(0,0,2,2));
  88.     }
  89.     public Texture() {
  90.         setBackground(Color.white);
  91.     }
  92.     public void drawDemo(int w, int h, Graphics2D g2) {
  93.         Rectangle r = new Rectangle(10,10,w-20,h/2-20);
  94.         g2.setPaint(gradient);
  95.         g2.fill(r);
  96.         g2.setPaint(Color.green);
  97.         g2.setStroke(new BasicStroke(20));
  98.         g2.draw(r);
  99.         g2.setPaint(blacklines);
  100.         g2.setStroke(new BasicStroke(15));
  101.         g2.draw(r);
  102.         Font f = new Font("Times New Roman", Font.BOLD, w/5);
  103.         TextLayout tl = new TextLayout("Texture", f, g2.getFontRenderContext());
  104.         int sw = (int) tl.getBounds().getWidth();
  105.         int sh = (int) tl.getBounds().getHeight();
  106.         Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.25+sh/2));
  107.         g2.setColor(Color.black);
  108.         g2.setStroke(new BasicStroke(3));
  109.         g2.draw(sha);
  110.         g2.setPaint(greendots);
  111.         g2.fill(sha);
  112.         r.setLocation(10,h/2+10);
  113.         g2.setPaint(triangles);
  114.         g2.fill(r);
  115.         g2.setPaint(blacklines);
  116.         g2.setStroke(new BasicStroke(20));
  117.         g2.draw(r);
  118.         g2.setPaint(Color.green);
  119.         g2.setStroke(new BasicStroke(4));
  120.         g2.draw(r);
  121.         f = new Font("serif", Font.BOLD, w/4);
  122.         tl = new TextLayout("Paint", f, g2.getFontRenderContext());
  123.         sw = (int) tl.getBounds().getWidth();
  124.         sh = (int) tl.getBounds().getHeight();
  125.         sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.75+sh/2));
  126.         g2.setColor(Color.black);
  127.         g2.setStroke(new BasicStroke(5));
  128.         g2.draw(sha);
  129.         g2.setPaint(bluedots);
  130.         g2.fill(sha);
  131.     }
  132.     public static void main(String s[]) {
  133.         Frame f = new Frame("Java2D Demo - Texture");
  134.         f.addWindowListener(new WindowAdapter() {
  135.             public void windowClosing(WindowEvent e) {System.exit(0);}
  136.         });
  137.         f.add("Center", new DemoPanel(new Texture()));
  138.         f.pack();
  139.         f.setSize(new Dimension(400,300));
  140.         f.show();
  141.     }
  142. }