All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.graph.ScrollGraph Maven / Gradle / Ivy

package net.alantea.swing.graph;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

import net.alantea.swing.layout.percent.PercentConstraints;
import net.alantea.swing.layout.percent.PercentLayout;

public class ScrollGraph extends JPanel
{
   private static final long serialVersionUID = 1L;
   
   private Graph graph;
   private JScrollPane scrollPane;
   
   public ScrollGraph()
   {
      this(new Graph());
   }
   
   public ScrollGraph(Graph graph)
   {
      this.graph = graph;
      this.setLayout(new PercentLayout());
      scrollPane = new JScrollPane(graph,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      graph.setLayout(null);

      add(scrollPane, new PercentConstraints(0.0, 0.0, 1.0, 1.0));
   }
   
   public Graph getContent()
   {
      return graph;
   }
   
   public void setDrawSurface(int width, int height)
   {
      graph.setPreferredSize(new Dimension(width,  height));
   }

   public void setVisible(int x, int y)
   {
      JViewport port = scrollPane.getViewport();
      Rectangle rect = port.getViewRect();
      if (!rect.contains(x, y))
      {
         int px = x - rect.width + 100;
         if (px < 0)
         {
            px = 0;
         }
         int py = y - rect.height + 100;
         if (py < 0)
         {
            py = 0;
         }
         port.setViewPosition(new Point(px, py));
      }
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy