1   package org.freehep.graphicsio.emf.test;
2   
3   import java.awt.Color;
4   import java.awt.Dimension;
5   import java.awt.Image;
6   import java.awt.MediaTracker;
7   import java.awt.Point;
8   import java.awt.Rectangle;
9   import java.io.FileOutputStream;
10  import java.io.IOException;
11  import java.util.Vector;
12  
13  import javax.swing.JPanel;
14  
15  import org.freehep.graphicsio.emf.EMFConstants;
16  import org.freehep.graphicsio.emf.EMFHandleManager;
17  import org.freehep.graphicsio.emf.EMFOutputStream;
18  import org.freehep.graphicsio.emf.EMFTag;
19  import org.freehep.graphicsio.emf.gdi.EOF;
20  import org.freehep.graphicsio.emf.gdi.GDIComment;
21  import org.freehep.graphicsio.emf.gdi.SetBkMode;
22  import org.freehep.graphicsio.emf.gdi.SetMapMode;
23  import org.freehep.graphicsio.emf.gdi.SetStretchBltMode;
24  import org.freehep.graphicsio.emf.gdi.SetViewportExtEx;
25  import org.freehep.graphicsio.emf.gdi.SetViewportOrgEx;
26  import org.freehep.graphicsio.emf.gdi.SetWindowExtEx;
27  import org.freehep.graphicsio.emf.gdi.SetWindowOrgEx;
28  import org.freehep.graphicsio.emf.gdi.StretchDIBits;
29  import org.freehep.util.images.ImageHandler;
30  import org.freehep.util.images.ImageUtilities;
31  
32  public class EMFTest extends JPanel {
33  
34      public EMFTest() {
35      }
36  
37      public void run() throws IOException {
38          Rectangle bounds = new Rectangle(0, 0, 800, 600);
39  
40  /*        Point p1[][] = { { new Point(40, 50), new Point(160, 170),
41                  new Point(80, 30) } };
42          Point p2[] = { new Point(20, 20), new Point(40, 60), new Point(30, 50),
43                  new Point(00, 60) };
44  */
45          Vector emf = new Vector();
46  
47          emf.add(new GDIComment("Settings"));
48          emf.add(new SetMapMode(SetMapMode.MM_ANISOTROPIC));
49          emf.add(new SetBkMode(SetBkMode.BKG_TRANSPARENT));
50          emf.add(new SetWindowOrgEx(new Point(0, 0)));
51          emf.add(new SetViewportOrgEx(new Point(0, 0)));
52          emf.add(new SetWindowExtEx(new Dimension(bounds.width, bounds.height)));
53          emf
54                  .add(new SetViewportExtEx(new Dimension(bounds.width,
55                          bounds.height)));
56  
57          /*
58           * emf.add(new SaveDC());
59           * 
60           * emf.add(new GDIComment("Polygon")); LogBrush32 brush = new
61           * LogBrush32(LogBrush32.BS_SOLID, Color.red, 0); emf.add(new
62           * CreateBrushIndirect(1, brush)); emf.add(new SelectObject(1));
63           * ExtLogPen pen1 = new ExtLogPen(ExtLogPen.PS_GEOMETRIC |
64           * ExtLogPen.PS_SOLID | ExtLogPen.PS_ENDCAP_ROUND |
65           * ExtLogPen.PS_JOIN_ROUND, 5, ExtLogPen.BS_SOLID, Color.green, 0, new
66           * int[0]); emf.add(new ExtCreatePen(2, pen1)); emf.add(new
67           * SelectObject(2)); emf.add(new SetMiterLimit(10)); emf.add(new
68           * PolyPolygon(bounds, p1)); emf.add(new DeleteObject(1)); emf.add(new
69           * DeleteObject(2));
70           * 
71           * emf.add(new GDIComment("Polyline")); emf.add(new
72           * ModifyWorldTransform(new AffineTransform(1, 0, 0, 1, 200, 0),
73           * ModifyWorldTransform.MWT_LEFTMULTIPLY)); ExtLogPen pen2 = new
74           * ExtLogPen(ExtLogPen.PS_GEOMETRIC | ExtLogPen.PS_SOLID |
75           * ExtLogPen.PS_ENDCAP_ROUND | ExtLogPen.PS_JOIN_ROUND, 5,
76           * ExtLogPen.BS_SOLID, Color.blue, 0, new int[0]); emf.add(new
77           * ExtCreatePen(1, pen2)); emf.add(new SelectObject(1)); emf.add(new
78           * SetMiterLimit(10)); emf.add(new Polyline(bounds, p2)); emf.add(new
79           * DeleteObject(1));
80           * 
81           * emf.add(new GDIComment("Ellipse")); emf.add(new
82           * ModifyWorldTransform(new AffineTransform(1, 0, 0, 1, 200, 0),
83           * ModifyWorldTransform.MWT_LEFTMULTIPLY)); ExtLogPen pen3 = new
84           * ExtLogPen(ExtLogPen.PS_GEOMETRIC | ExtLogPen.PS_SOLID |
85           * ExtLogPen.PS_ENDCAP_ROUND | ExtLogPen.PS_JOIN_ROUND, 5,
86           * ExtLogPen.BS_SOLID, Color.orange, 0, new int[0]); emf.add(new
87           * ExtCreatePen(1, pen3)); emf.add(new SelectObject(1)); emf.add(new
88           * SetMiterLimit(10)); emf.add(new Ellipse(new Rectangle(20,20,50,30)));
89           * emf.add(new DeleteObject(1));
90           * 
91           * emf.add(new RestoreDC());
92           * 
93           * emf.add(new GDIComment("Arc")); emf.add(new ModifyWorldTransform(new
94           * AffineTransform(1, 0, 0, 1, 0, 200),
95           * ModifyWorldTransform.MWT_LEFTMULTIPLY)); ExtLogPen pen4 = new
96           * ExtLogPen(ExtLogPen.PS_GEOMETRIC | ExtLogPen.PS_SOLID |
97           * ExtLogPen.PS_ENDCAP_ROUND | ExtLogPen.PS_JOIN_ROUND, 5,
98           * ExtLogPen.BS_SOLID, Color.cyan, 0, new int[0]); emf.add(new
99           * ExtCreatePen(1, pen4)); emf.add(new SelectObject(1)); emf.add(new
100          * SetMiterLimit(10)); emf.add(new AngleArc(new Point(100,100), 50, 20,
101          * 280)); emf.add(new DeleteObject(1));
102          * 
103          * emf.add(new GDIComment("Image")); emf.add(new
104          * ModifyWorldTransform(new AffineTransform(1, 0, 0, 1, 0, 200),
105          * ModifyWorldTransform.MWT_LEFTMULTIPLY));
106          */
107         MediaTracker t = new MediaTracker(this);
108         Image image = ImageHandler.getImage("BrokenCursor.gif", getClass());
109         t.addImage(image, 0);
110         try {
111             t.waitForAll();
112         } catch (InterruptedException e) {
113             e.printStackTrace();
114         }
115         emf.add(new SetStretchBltMode(EMFConstants.COLORONCOLOR));
116         emf.add(new StretchDIBits(
117             bounds,
118             0, 0,
119             image.getWidth(this),
120             image.getHeight(this),
121             ImageUtilities.createBufferedImage(image, this, Color.BLACK), 
122             Color.BLACK));
123         /*
124          * emf.add(new GDIComment("Text")); emf.add(new ModifyWorldTransform(new
125          * AffineTransform(1, 0, 0, 1, 0, 200),
126          * ModifyWorldTransform.MWT_LEFTMULTIPLY));
127          * 
128          * LogFontW font = new LogFontW(-82, 0, 0, 0, 265, false, false, false,
129          * 0xCC, 7, 2, 1, 0x22, "Arial TUR"); Panose panose = new Panose();
130          * ExtLogFontW extFont = new ExtLogFontW(font, "", "", 0, 0, 0, new
131          * byte[] {0, 0, 0, 0}, 0, panose); emf.add(new
132          * ExtCreateFontIndirectW(1, extFont));
133          * 
134          * emf.add(new SelectObject(1)); Text text = new Text(new Point (0, 0),
135          * "Test", Text.ETO_OPAQUE, bounds); emf.add(new ExtTextOutA(new
136          * Rectangle(0,0,100,100), ExtTextOutA.GM_COMPATIBLE, 100, 100, text));
137          * emf.add(new DeleteObject(1));
138          * 
139          */
140         emf.add(new GDIComment("End"));
141         emf.add(new EOF());
142 
143         EMFOutputStream out = new EMFOutputStream(new FileOutputStream(
144                 "EMFTest.emf"), bounds, new EMFHandleManager(), "EMFTest",
145                 "TestFile", new Dimension(1024, 768));
146         for (int i = 0; i < emf.size(); i++) {
147             out.writeTag((EMFTag) emf.get(i));
148         }
149         out.close();
150     }
151 
152     public static void main(String[] args) throws IOException {
153         EMFTest test = new EMFTest();
154         test.run();
155         System.exit(0);
156     }
157 }