// // WMFWriterDemo.java // // Copyright (c) 1999,2003 Piet Jonas. All Rights Reserved. // // You may not distributed this code except in binary form, incorporated // into a java .class file. You may use this code freely for personal // purposes, but you may not incorporate it into any commercial product // without my express permission in writing. // // PIET JONAS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR // NON-INFRINGEMENT. PIET JONAS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED // BY A LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE // OR ITS DERIVATIVES. // import java.io.*; import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.event.KeyAdapter; import java.awt.font.*; import com.pietjonas.wmfwriter2d.*; /** * This class demonstrates the creation of Windows Metafiles with the * help of the WMFWriter package. It runs with JDK 1.0.2, JDK 1.1 and * JDK 1.2
* Press any key to save WMFWriterDemo.wmf (Placeable WMF) and * WMFWriterDemo2.wmf (simple WMF). * * @author Piet Jonas (jonas@physik.uni-greifswald.de) * @version 1.0 * @see WMFGraphics, WMF */ public class WMFWriterDemo extends Frame { private WMFWriterDemoCanvas canvas = new WMFWriterDemoCanvas(); private MenuBar menu; public WMFWriterDemo() { } public void init() { setTitle("WMFWriterDemo - Press any key to save and copy WMF"); setLayout(new BorderLayout()); add(canvas, BorderLayout.CENTER); setSize(610, 450); canvas.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { canvas.saveWMF(); canvas.copyWMF(); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); menu = new MenuBar(); Menu filemenu = new Menu("File"); menu.add(filemenu); MenuItem savemenu = new MenuItem("Save WMFWriterDemo.wmf"); savemenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.saveWMF(); } }); filemenu.add(savemenu); Menu editmenu = new Menu("Edit"); menu.add(editmenu); MenuItem copymenu = new MenuItem("Copy"); copymenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.copyWMF(); } }); editmenu.add(copymenu); setMenuBar(menu); show(); } public static void main(String arg[]) { WMFWriterDemo frmWMF = new WMFWriterDemo(); frmWMF.init(); } } /** * This is the Canvas, which is the drawing area for WMFWriterDemo. * * @author Piet Jonas (piet@jonas.com) * @version 1.0 * @see WMFGraphics, WMF */ class WMFWriterDemoCanvas extends Canvas { private ClipboardCopy clipboardCopy; public void paint(Graphics g) { drawGraphics(g); g.translate(0, 200); drawGraphics(g); Graphics g2 = g.create(200, -200, 200, 200); drawWMFGraphics(g2); g2.dispose(); g.setClip(null); Graphics g3 = g.create(200, 0, 200, 200); drawGraphics2D(g3); Graphics g4 = g.create(400, 0, 200, 200); drawText2D(g4); g.drawString("WMF commands", 430, -100); } /** * Draw with the standard Graphics object. */ protected void drawGraphics(Graphics g) { Image img = Toolkit.getDefaultToolkit().getImage("baskball.gif"); MediaTracker mt = new MediaTracker(this); mt.addImage(img, 1); try { mt.waitForAll(); } catch (InterruptedException e) { } if (!mt.isErrorAny()) { g.drawImage(img, 0, 0, 100, 100, new Color(255, 255, 224), this); } g.setColor(Color.black); g.drawRect(0, 0, 200, 200); g.fillRect(5, 0, 1, 200); g.drawLine(5, 10, 10, 15); g.drawRect(10, 0, 0, 200); g.setColor(Color.blue); g.drawLine(200, 0, 100, 100); g.setColor(Color.cyan); Polygon p = new Polygon(); p.addPoint(150, 70); p.addPoint(30, 100); p.addPoint(180, 150); p.addPoint(120, 70); g.fillPolygon(p); g.setColor(Color.black); g.drawPolygon(p); g.setColor(Color.yellow); g.fillArc(80, 140, 30, 70, 60, 130); g.setColor(Color.black); g.drawArc(80, 140, 30, 70, 60, 130); g.setColor(Color.orange); g.fillRoundRect(70, 70, 60, 60, 10, 30); g.setColor(Color.magenta); g.fillOval(80, 80, 40, 40); g.drawImage(img, 10, 140, this); g.drawImage(img, 150, 155, 190, 195, 40, 40, 0, 0, this); g.setColor(Color.green); g.clipRect(30, 40, 30, 30); g.fillRect(10, 20, 50, 50); g.setColor(Color.lightGray); g.fill3DRect(15, 25, 40, 40, true); g.draw3DRect(20, 30, 30, 30, false); g.setClip(null); g.setColor(new Color(0, 0, 128)); g.setFont(new Font("sansserif", Font.BOLD, 16)); String type = "Graphics"; if (g instanceof Graphics2D) { type += "2D"; } g.drawString(type + " standard", 30, 20); } /** * Draw with special WMFGraphics commands. */ protected void drawWMFGraphics(Graphics g) { boolean wmfg = g instanceof WMFGraphics; WMFGraphics gw = null; if (wmfg) { gw = (WMFGraphics) g; } if (wmfg) { gw.setPenStyle(WMF.PS_DASH); } g.setColor(Color.black); g.drawRect(10, 10, 180, 180); if (wmfg) { gw.setBrushFillStyle(WMF.BS_HATCHED); gw.setBrushHatch(WMF.HS_FDIAGONAL); } g.setColor(Color.blue); g.fillRect(20, 20, 160, 160); if (wmfg) { gw.setBrushFillStyle(WMF.BS_SOLID); } g.setColor(Color.yellow); g.fillOval(30, 30, 140, 140); g.setColor(Color.black); if (wmfg) { g.setColor(Color.blue); gw.setBrushFillStyle(WMF.BS_HATCHED); gw.setBrushHatch(WMF.HS_BDIAGONAL); g.fillOval(30, 30, 140, 140); } if (wmfg) { Image pattern = Toolkit.getDefaultToolkit().getImage("pattern.gif"); MediaTracker mt = new MediaTracker(this); mt.addImage(pattern, 1); try { mt.waitForAll(); } catch (InterruptedException e) { } if (!mt.isErrorAny()) { gw.setBrushPattern(pattern); gw.setBrushFillStyle(WMF.BS_PATTERN); } } g.fillOval(60, 70, 80, 60); if (wmfg) { gw.setPenStyle(WMF.PS_SOLID); gw.setPenWidth(3); } g.setColor(Color.red); g.drawOval(30, 30, 140, 140); if (wmfg) { gw.setFontEscapement(450); } g.setColor(Color.red); g.setFont(new Font("monospaced", Font.ITALIC + Font.BOLD, 16)); g.drawString("WMFGraphics extra", 20, 180); if (wmfg) { gw.reset(); //set back to standard AWT } } /** * Draw in the WMF directly with supported GDI commands. */ protected void drawWMF(WMF wmf) { //draw filled chord int pen = wmf.createPenIndirect(WMF.PS_DOT, 1, Color.blue); wmf.selectObject(pen); wmf.setBKColor(Color.yellow); wmf.setBKMode(WMF.OPAQUE); int brush = wmf.createBrushIndirect(WMF.BS_HATCHED, Color.black, WMF.HS_DIAGCROSS); wmf.selectObject(brush); wmf.chord(50, 0, 100, 100, 50, 0, 100, 100); //draw text with opaque background int font = wmf.createFontIndirect( -16, 0, 3050, 0, WMF.FW_BOLD, false, true, false, WMF.ANSI_CHARSET, WMF.OUT_DEFAULT_PRECIS, WMF.CLIP_DEFAULT_PRECIS, WMF.DEFAULT_QUALITY, WMF.FF_DONTCARE, "Comic Sans MS"); wmf.selectObject(font); wmf.setBKMode(WMF.OPAQUE); wmf.setBKColor(Color.lightGray); wmf.setTextColor(Color.blue); wmf.textOut(100, 50, "WMF commands"); wmf.extTextOut( 140, 50, WMF.ETO_CLIPPED, new Rectangle(140, 60, 200, 100), "WMF commands"); //draw bitmap wmf.setStretchBltMode(WMF.STRETCH_ORSCANS); Image img = Toolkit.getDefaultToolkit().getImage("baskball.gif"); MediaTracker mt = new MediaTracker(this); mt.addImage(img, 1); try { mt.waitForAll(); } catch (InterruptedException e) { } if (!mt.isErrorAny()) { int w = img.getWidth(this); int h = img.getHeight(this); int pix[] = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pix, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { return; } if ((pg.status() & ImageObserver.ABORT) != 0) { return; } int backrgb = Color.yellow.getRGB(); for (int i = 0; i < pix.length; i++) { if ((pix[i] & 0xFF000000) == 0) { pix[i] = backrgb; } } wmf.bitBlt(50, 120, 3 * w / 4, 2 * h / 3, 0, 0, WMF.SRCCOPY, pix, w, h); } //invert box wmf.patBlt(70, 70, 70, 70, WMF.DSTINVERT); //draw bounding box with thick line wmf.deleteObject(pen); pen = wmf.createPenIndirect(WMF.PS_SOLID, 2, Color.blue); wmf.selectObject(pen); wmf.deleteObject(brush); brush = wmf.createBrushIndirect(WMF.BS_HOLLOW, Color.black, WMF.HS_DIAGCROSS); wmf.selectObject(brush); wmf.rectangle(1, 1, 200, 200); //clean up wmf.deleteObject(pen); wmf.deleteObject(brush); wmf.deleteObject(font); //put in Escape commands int MFCOMMENT = 15; String message = "Made with WMFWriter2D, (c) Piet Jonas, piet@jonas.com"; byte[] data = new byte[message.length() + 1]; data = message.getBytes(); wmf.escape(MFCOMMENT, data); } protected void drawGraphics2D(Graphics g) { Graphics2D g2d = (Graphics2D) g; // Test stroking g2d.setStroke( new BasicStroke( 5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 2, new float[] { 5.0f, 8.0f }, 0.0f)); g2d.drawRect(10, 10, 50, 50); // Test gradient g2d.setPaint( new GradientPaint(15, 15, Color.red, 30, 30, Color.blue, true)); g2d.fillOval(15, 15, 40, 40); // Load test images Image pattern = Toolkit.getDefaultToolkit().getImage("pattern.gif"); Image img = Toolkit.getDefaultToolkit().getImage("baskball.gif"); MediaTracker mt = new MediaTracker(this); mt.addImage(pattern, 1); mt.addImage(img, 2); try { mt.waitForAll(); } catch (InterruptedException e) { } if (!mt.isErrorAny()) { // Test AffineTransform and drawImage() AffineTransform oldtrans = g2d.getTransform(); g2d.setStroke(new BasicStroke(1)); g2d.setColor(Color.gray); g2d.drawRect(10, 80, img.getWidth(this), img.getHeight(this)); g2d.shear(0.5, 0.2); g2d.rotate(30, 50, 130); g2d.scale(1, 0.5); g2d.drawImage(img, 10, 80, this); g2d.setStroke(new BasicStroke(2)); g2d.setColor(Color.black); g2d.drawRect(10, 80, img.getWidth(this), img.getHeight(this)); g2d.setTransform(oldtrans); // Test clipping and drawImage() g2d.drawLine(130, 130, 200, 200); Shape clip = g2d.getClip(); g2d.setClip(new RoundRectangle2D.Float(150, 150, 30, 30, 20, 20)); g2d.drawImage(img, 140, 140, this); g2d.setClip(clip); // Test odd shaped clipping FontRenderContext frc = g2d.getFontRenderContext(); String clipstring = new String("Clip Shape"); TextLayout tl = new TextLayout(clipstring, g2d.getFont(), frc); Shape outline = tl.getOutline(AffineTransform.getTranslateInstance(100, 100)); g2d.setClip(outline); g2d.setColor(Color.blue); g2d.fillRect(80, 80, 50, 100); g2d.drawImage(img, 150, 65, this); g2d.setClip(clip); g2d.setStroke(new BasicStroke(1)); g2d.setColor(Color.red); g2d.draw(outline); // Test TexturePaint fill BufferedImage img2 = new BufferedImage( pattern.getWidth(this), pattern.getHeight(this), BufferedImage.TYPE_4BYTE_ABGR); img2.getGraphics().drawImage(pattern, 0, 0, this); g2d.setPaint( new TexturePaint( img2, new Rectangle(0, 0, img2.getWidth(), img2.getHeight()))); g2d.fillRect(70, 10, 30, 30); g2d.setStroke(new BasicStroke(4)); g2d.drawOval(130, 10, 50, 30); } // Test AffineTransform and text g2d.rotate(-50); g2d.setColor(Color.orange); g2d.drawString("WMFGraphics2D", 10, 20); // Test curves QuadCurve2D.Double quad = new QuadCurve2D.Double(); Point2D.Double start, end, control; start = new Point2D.Double(); end = new Point2D.Double(); control = new Point2D.Double(); start.setLocation(170, 150); end.setLocation(120, 180); control.setLocation(140, 100); quad.setCurve(start, control, end); g2d.setColor(Color.black); g2d.setStroke(new BasicStroke(1)); g2d.drawLine( (int) start.x, (int) start.y, (int) control.x, (int) control.y); g2d.drawLine((int) end.x, (int) end.y, (int) control.x, (int) control.y); g2d.setColor(Color.red); g2d.setStroke(new BasicStroke(2)); g2d.draw(quad); g2d.setColor(Color.red); g2d.fillOval((int) control.x - 2, (int) control.y - 2, 5, 5); } protected void drawText2D(Graphics g) { Graphics2D g2d = (Graphics2D) g; AffineTransform org = g2d.getTransform(); Font font = new Font("serif", Font.PLAIN, 25); g2d.setFont(font); g2d.setColor(Color.blue); g2d.drawString("Font test - serif", 20, 25); g2d.setFont(font.deriveFont(20.0f)); g2d.setColor(Color.blue.darker()); g2d.scale(1.5, 1.5); g2d.drawString("uniform scale", 7, 40); g2d.setTransform(org); g2d.scale(1, 1.5); g2d.drawString("nonuniform scale", 10, 60); g2d.setTransform(org); font = new Font("monospaced", Font.BOLD, 17); g2d.setFont(font); g2d.shear(0.5, 0); g2d.drawString("sheared monospaced", -40, 110); g2d.setTransform(org); font = new Font("sansserif", Font.PLAIN, 20); g2d.setFont(font); g2d.rotate(0.2); g2d.drawString("rotated sansserif", 50, 140); g2d.setTransform(org); g2d.setFont(new Font("dialoginput", Font.ITALIC + Font.BOLD, 20)); g2d.drawString("clipped dialoginput", 10, 195); g2d.setTransform(org); g2d.setFont(new Font("dialog", Font.BOLD, 10)); AffineTransform t1 = new AffineTransform(); t1.rotate(1.0); t1.scale(1.2, 1.2); g2d.scale(1.5, 1.5); g2d.rotate(-0.2); g2d.setFont(g2d.getFont().deriveFont(t1)); g2d.setColor(Color.red); g2d.drawString("dialog font transform", 20, 30); } /** * Example usage of the WMF package. Creates a Placeable Windows * Metafile 'WMFWriterDemo.java' and a simple Windows Metafile * 'WMFWriterDemo2.wmf'. */ public void saveWMF() { System.out.println("save WMF"); try { saveTo("WMFWriterDemo.wmf", true); System.out.println("WMF saved"); } catch (IOException e) { System.out.println("IO Exception while saving WMF:" + e); } } private void saveTo(String name, boolean withHeader) throws FileNotFoundException, IOException { //create a WMF object int width = 600; int height = 400; WMF wmf = new WMF(); //create the Graphics2D object to draw in the WMF object WMFGraphics2D g2d = new WMFGraphics2D(wmf, width, height, getForeground(), getBackground()); //draw in the WMF with the standard Graphics methods g2d.translate(0, 200); drawGraphics(g2d); //or draw in the WMF with the Graphics2D object Graphics g3 = g2d.create(200, 0, 200, 200); drawGraphics2D(g3); g3.dispose(); Graphics g4 = g2d.create(400, 0, 200, 200); drawText2D(g4); g2d.setClip(null); //create the standard Graphics object to draw in the WMF object WMFGraphics g = new WMFGraphics(wmf, width, height, getForeground(), getBackground()); //draw in the WMF with the standard Graphics object g.clipRect(0, 0, 200, 200); drawGraphics(g); //or use special WMFGraphics methods Graphics g2 = g.create(200, 0, 200, 200); drawWMFGraphics(g2); g2.setClip(null); g2.dispose(); //or draw in the WMF directly with supported GDI commands wmf.setWindowOrg(-400, 0); drawWMF(wmf); //save the WMF to an OutputStream with a placeable WMF header FileOutputStream out = new FileOutputStream(name); if (withHeader) { wmf.writePlaceableWMF( out, 0, 0, width, height, Toolkit.getDefaultToolkit().getScreenResolution()); } else { wmf.writeWMF(out); } out.close(); } public void copyWMF() { if (clipboardCopy == null) { try { clipboardCopy = new ClipboardCopy(); } catch (Exception e) { System.out.println("Can't access clipboard: " + e); return; } } System.out.println("copy WMF"); try { copyToClipboard(); System.out.println("WMF copied"); } catch (IOException e) { System.out.println("IO Exception while saving WMF:" + e); } } private void copyToClipboard() throws FileNotFoundException, IOException { File temp = File.createTempFile("WMFWriter", ".wmf"); temp.deleteOnExit(); saveTo(temp.getAbsolutePath(), false); clipboardCopy.copyWithPixelSize(temp.getAbsolutePath(), 600, 400, false); /* // Alternative: Do the pixel to metric conversion yourself int dpi = Toolkit.getDefaultToolkit().getScreenResolution(); clipboardCopy.copy( temp.getAbsolutePath(), 600 * 2540 / dpi, 400 * 2540 / dpi, false); */ temp.delete(); } }