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

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

The newest version!
package net.alantea.swing.graph;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class GraphElement extends GraphListener
{
   public enum Mode
   {
      NEUTRAL,
      DRAGGED
   }
   
   private Mode mode;
   private int lastX;
   private int lastY;
   
   private Map dataMap = new HashMap<>();
   
   @Override
   protected boolean onMousePressed(MouseEvent event)
   {
      super.onMousePressed(event);
      if (mode == Mode.DRAGGED)
      {
         lastX = event.getX();
         lastY = event.getY();
         return true;
      }
      return false;
   }
   
   public void setMode(Mode newMode)
   {
      mode = newMode;
   }
   
   
   public Object getData(String key)
   {
      return dataMap.get(key);
   }

   public void setData(String key, Object data)
   {
      dataMap.put(key,  data);
   }

   @Override
   protected boolean onMouseDragged(MouseEvent event)
   {
      super.onMouseDragged(event);
      if (mode == Mode.DRAGGED)
      {
         int newX = event.getX();
         int newY = event.getY();
         Rectangle current = getBounds();
         setBounds(current.x + (newX - lastX), current.y + (newY - lastY), current.width, current.height);
         getParent().repaint();
         return true;
      }
      return false;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy