import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class foo extends JFrame {

   public static void main(String[] args) {
      new foo();
   }
   public foo() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }}
      );
      build();
      pack();
      setVisible(true);
   }
   private JPanel m_blank1, m_blank2;
   private JViewport m_vport;
   private int SIZE = 50;

   private void build() {
      final JPanel panel = new JPanel();
      panel.setBorder(BorderFactory.createLoweredBevelBorder());

      //=============================================================
      JButton refresh = new JButton("Refresh");
      refresh.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            panel.repaint();
         }
      });

      //=============================================================
      m_blank1 = new JPanel() {
         public void paintComponent(Graphics g) {
            System.out.println("PAINT BLANK1");
         }
      };
      m_blank1.setPreferredSize(new Dimension(SIZE,SIZE));

      //=============================================================
      DrawArea da = new DrawArea();
      da.setPreferredSize(new Dimension(1000, SIZE));
      JScrollPane sp = new JScrollPane(da,
         JScrollPane.VERTICAL_SCROLLBAR_NEVER,
         JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
      );
      sp.setPreferredSize(new Dimension(500, SIZE));
      m_vport = sp.getViewport();
      m_vport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE );

      //=============================================================
      m_blank2 = new JPanel() {
         public void paintComponent(Graphics g) {
            System.out.println("PAINT BLANK2");
         }
      };
      m_blank2.setPreferredSize(new Dimension(SIZE,SIZE));

      //=============================================================
      panel.add(m_blank1);
      panel.add(sp);
      panel.add(m_blank2);
      
      Container c = getContentPane();
      c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS));
      c.add(refresh);
      c.add(panel);
   }
   //////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////
   private class DrawArea extends JPanel {

      public boolean isOpaque() { return true; }
      public void paintComponent(Graphics g) {
         System.out.println("PAINT DRAW AREA");

         Rectangle clip = g.getClipBounds();
         int x = clip.x;
         int w = clip.width;

         g.setColor(Color.black);
         g.setClip( x-SIZE, clip.y, w+2*SIZE, clip.height);
         g.fillRect(x-SIZE, clip.y, w+2*SIZE, clip.height);

         g.setColor(Color.yellow);
         int min_x = x - SIZE;
         int max_x = x + w + SIZE;
         for (int xloc= -SIZE; xloc <= max_x; xloc += 25) {
            if (xloc >= min_x) {
               g.drawString(Integer.toString(xloc), xloc, 20);
            }
         }
      }
   }
}
