1   package org.freehep.graphicsio.ps.test;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.Dimension;
6   import java.awt.Graphics;
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.BoxLayout;
13  import javax.swing.JButton;
14  import javax.swing.JFrame;
15  import javax.swing.JPanel;
16  import javax.swing.JScrollBar;
17  import javax.swing.JScrollPane;
18  
19  import org.freehep.graphicsio.ps.EPSExportFileType;
20  import org.freehep.util.export.ExportFileType;
21  
22  public class ScrollTest {
23  
24      public static void main(String[] args) {
25          JFrame frame = new JFrame();
26  
27          JPanel jcomponent = new TestPanel();
28          jcomponent.setLayout(new BoxLayout(jcomponent, BoxLayout.X_AXIS));
29  
30          JScrollPane scroll = new JScrollPane(jcomponent);
31          frame.setContentPane(scroll);
32          scroll.setPreferredSize(new Dimension(300, 50));
33  
34          // Make a long row of buttons.
35          int xmax = 32;
36          for (int x = 0; x < xmax; x++) {
37              JPanel button = new TestSubPanel("Test:" + x);
38              jcomponent.add(button);
39          }
40  
41          // Make the frame visible.
42          frame.pack();
43          frame.setVisible(true);
44  
45          // Reset the scroll bar to the middle of the row.
46          JScrollBar bar = scroll.getHorizontalScrollBar();
47          int max = bar.getMaximum();
48          int min = bar.getMinimum();
49          bar.setValue((max + min) / 2);
50  
51          ExportFileType epsOut = new EPSExportFileType();
52          try {
53  
54              File file;
55              FileOutputStream fos;
56  
57              System.out.println("\nPrinting image...\n");
58              file = new File("ScrollTest.eps");
59              fos = new FileOutputStream(file);
60              epsOut.exportToFile(fos, scroll, frame, new Properties(), null);
61              fos.close();
62  
63              System.out.println();
64              System.out.println("An image of the frame should have been ");
65              System.out.println("produced (ScrollTest.eps).  If this image ");
66              System.out.println("is not correct, then check the clipping");
67              System.out.println("rectangles in the graphics code.");
68              System.out.println();
69  
70          } catch (Exception e) {
71              e.printStackTrace();
72          }
73      }
74  
75      static class TestSubPanel extends JPanel {
76  
77          private String label = null;
78  
79          public TestSubPanel(String label) {
80              super(new BorderLayout(), false);
81              this.label = label;
82              setBorder(BorderFactory.createLineBorder(Color.black, 1));
83              JButton button = new JButton(label);
84              add(button, "North");
85          }
86  
87          public String getLabel() {
88              return label;
89          }
90  
91          public void paintComponent(Graphics g) {
92              System.out.println("Draw subpanel: " + label + " " + g.getClip());
93              super.paintComponent(g);
94          }
95      }
96  
97      static class TestPanel extends JPanel {
98          public TestPanel() {
99          }
100 
101         public void paintComponent(Graphics g) {
102             System.out.println("Draw panel: " + g.getClip());
103             super.paintComponent(g);
104         }
105     }
106 
107 }