1 package org.freehep.graphicsio.ps.test; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Graphics; 6 import java.awt.GridLayout; 7 import java.io.File; 8 import java.io.FileOutputStream; 9 import java.util.Properties; 10 11 import javax.swing.BorderFactory; 12 import javax.swing.JButton; 13 import javax.swing.JFrame; 14 import javax.swing.JPanel; 15 16 import org.freehep.graphicsio.ps.EPSExportFileType; 17 import org.freehep.util.export.ExportFileType; 18 19 public class OffsetBug { 20 21 public static void main(String[] args) { 22 JFrame frame = new JFrame(); 23 24 JPanel panel = new JPanel(new GridLayout(2, 2)); 25 frame.getContentPane().add(panel); 26 27 JPanel first = new SubPanel("First", Color.red); 28 panel.add(first); 29 JPanel second = new SubPanel("Second", Color.green); 30 panel.add(second); 31 JPanel third = new SubPanel("Third", Color.yellow); 32 panel.add(third); 33 JPanel fourth = new SubPanel("Fourth", Color.cyan); 34 panel.add(fourth); 35 36 frame.pack(); 37 frame.setVisible(true); 38 39 ExportFileType epsOut = new EPSExportFileType(); 40 try { 41 42 File file; 43 FileOutputStream fos; 44 45 System.out.println("\nPrinting first...\n"); 46 file = new File("first.eps"); 47 fos = new FileOutputStream(file); 48 epsOut.exportToFile(fos, first, frame, new Properties(), null); 49 fos.close(); 50 51 System.out.println("\nPrinting second...\n"); 52 file = new File("second.eps"); 53 fos = new FileOutputStream(file); 54 epsOut.exportToFile(fos, second, frame, new Properties(), null); 55 fos.close(); 56 57 System.out.println(); 58 System.out.println("Two EPS files should have been generated:"); 59 System.out.println("first.eps and second.eps. These should "); 60 System.out.println("contain images of the first and second"); 61 System.out.println("buttons properly scaled and clipped."); 62 System.out.println(""); 63 System.out.println("If second.eps is not correct, then check "); 64 System.out.println("the clipping bounds which are initially "); 65 System.out.println("given to the printing component."); 66 System.out.println(); 67 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 71 } 72 73 static private class SubPanel extends JPanel { 74 75 String label; 76 77 public SubPanel(String label, Color color) { 78 super(new BorderLayout()); 79 this.label = label; 80 setOpaque(true); 81 setBackground(color); 82 83 setBorder(BorderFactory.createLineBorder(Color.black, 1)); 84 JButton button = new TalkativeButton(label); 85 add(button, BorderLayout.CENTER); 86 } 87 88 public void paintComponent(Graphics g) { 89 System.out.println("paintComponent called: " + label); 90 super.paintComponent(g); 91 System.out.println("graphics class: " + g.getClass()); 92 System.out.println("Container clip: " + g.getClipBounds()); 93 System.out.println("Container bounds: " + getBounds()); 94 } 95 96 public void paintChildren(Graphics g) { 97 System.out.println("clip (child.) " + g.getClipBounds()); 98 super.paintChildren(g); 99 } 100 } 101 102 static private class TalkativeButton extends JButton { 103 104 public TalkativeButton(String label) { 105 super(label); 106 } 107 108 public void paintComponent(Graphics g) { 109 System.out.println("button: " + getText()); 110 super.paintComponent(g); 111 } 112 } 113 114 }