package bandwidth.ui;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JPanel;
import misc.Rect;
public class GraphicCanvas extends JPanel
{
private static final long serialVersionUID = -6853679144415898341L;
private static final float SPACING_RATIO = 0.05f;
private GraphicManagerInterface manager;
public GraphicCanvas(GraphicManagerInterface manager)
{
super();
this.manager = manager;
addComponentListener(new CanvasManager());
}
protected void paintComponent(Graphics g)
{
manager.drawElements(g);
}
private class CanvasManager extends ComponentAdapter
{
public void componentResized(ComponentEvent event)
{
calcMeasures();
}
}
private void calcMeasures()
{
int gripperAreaWidth = manager.getGripper().getWidth();
int width = getWidth();
int height = getHeight();
int horizontalSpacing = (int) (width * SPACING_RATIO);
int verticalSpacing = (int) (height * SPACING_RATIO);
Rect gripperArea = Rect.createWithMeasures(0, 0, gripperAreaWidth, height);
Rect backgroundArea = Rect.create(gripperAreaWidth, 0, width, height);
Rect graphicArea = Rect.create(backgroundArea.getLeft() + horizontalSpacing,
verticalSpacing, width - horizontalSpacing, height - verticalSpacing);
manager.updateRegions(gripperArea, backgroundArea, graphicArea);
}
}