View Javadoc

1   // Copyright 2000-2005, FreeHEP.
2   package org.freehep.graphicsio.test;
3   
4   import java.awt.Color;
5   import java.awt.GradientPaint;
6   import java.awt.Graphics;
7   import java.awt.Image;
8   import java.awt.MediaTracker;
9   import java.awt.Paint;
10  import java.awt.TexturePaint;
11  import java.awt.geom.Rectangle2D;
12  import java.awt.image.BufferedImage;
13  
14  import org.freehep.graphics2d.VectorGraphics;
15  import org.freehep.util.UserProperties;
16  import org.freehep.util.images.ImageHandler;
17  
18  public class TestPaint extends TestingPanel {
19  
20      public TestPaint(String[] args) throws Exception {
21          super(args);
22          setName("Paint");
23      }
24  
25      public void paintComponent(Graphics g) {
26  
27          VectorGraphics vg = VectorGraphics.create(g);
28  
29          vg.setColor(Color.white);
30          vg.fillRect(0, 0, getWidth(), getHeight());
31  
32          int dw = getWidth() / 3;
33          int dh = getHeight() / 3;
34  
35          MediaTracker t = new MediaTracker(this);
36          Image limage = ImageHandler.getImage("images/BrokenCursor.gif",
37                  TestPaint.class);
38          t.addImage(limage, 0);
39          try {
40              t.waitForAll();
41          } catch (InterruptedException e) {
42              e.printStackTrace();
43          }
44          BufferedImage image = new BufferedImage(limage.getWidth(this), limage
45                  .getHeight(this), BufferedImage.TYPE_INT_RGB);
46          image.getGraphics().drawImage(limage, 0, 0, this);
47  
48          Paint[] paint = new Paint[] {
49                  new TexturePaint(image, new Rectangle2D.Double(0, 0, image
50                          .getWidth(), image.getHeight())),
51                  new TexturePaint(image, new Rectangle2D.Double(0, 0, image
52                          .getWidth() / 2, image.getHeight() / 2)),
53                  new TexturePaint(image, new Rectangle2D.Double(
54                          image.getWidth() / 2, image.getHeight() / 2, image
55                                  .getWidth(), image.getHeight())) };
56  
57          double row = 1;
58          for (int x = 0; x < paint.length; x++) {
59              vg.setColor(Color.black);
60              vg.drawRect(dw * x, dh * row, dw * 0.9, dh * 0.9);
61              vg.setPaint(paint[x]);
62              vg.fillRect(dw * x, dh * row, dw * 0.9, dh * 0.9);
63          }
64  
65          row = 2;
66          for (int x = 0; x < 3; x++) {
67              Paint p = null;
68              switch (x) {
69              case 0:
70                  p = new GradientPaint(dw * x, (int) row * dh, Color.red,
71                          (int) (dw * x + dw * 0.9), (int) (row * dh + 0.9 * dh),
72                          Color.blue);
73                  break;
74              case 1:
75                  p = new GradientPaint(dw * x + dw / 4, (int) row * dh + dh / 4,
76                          Color.green, (int) (dw * x + dw * 0.6),
77                          (int) (row * dh + 0.6 * dh), new Color(255, 0, 255));
78                  break;
79              case 2:
80                  p = new GradientPaint(dw * x, (int) row * dh, Color.red,
81                          (int) (dw * x + dw * 0.2), (int) (row * dh + 0.2 * dh),
82                          Color.yellow, true);
83                  break;
84              }
85              vg.setPaint(p);
86              vg.fillRect(dw * x, row * dh, dw * 0.9, dh * 0.9);
87          }
88  
89          Graphics subgraphics = vg.create();
90          VectorGraphics svg = VectorGraphics.create(subgraphics);
91          double x1 = 0;
92          double y1 = 0;
93          double x2 = x1 + dw * 0.6;
94          double y2 = y1 + dh * 0.6;
95          svg.shear(0.5, 0.5);
96  
97          svg.setPaint(new GradientPaint((int) x1, (int) y1, Color.red, (int) x2,
98                  (int) y2, Color.blue));
99          svg.fillRect(x1, y1, x2 - x1, y2 - y1);
100 
101         svg.setPaint(new TexturePaint(image, new Rectangle2D.Double(0, 0, image
102                 .getWidth(), image.getHeight())));
103         svg.fillRect(x1 + dw, y1 - dh / 2, x2 - x1, y2 - y1);
104 
105         svg.dispose();
106     }
107 
108     public static void main(String[] args) throws Exception {
109         UserProperties p = new UserProperties();
110         new TestPaint(args).runTest(p);
111     }
112 }