/* @(#)SlidersDemo.java   5 October 2007 */

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;

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

public class SlidersDemo
    extends DisplayPanel
{
    protected int size = 400;
    
    protected int gap = 10;
    
    
    protected BufferedPanel window =
        new BufferedPanel(size, size);
    
    protected MouseActionAdapter adapter =
        window.getMouseActionAdapter();
    
    
    protected SliderView hSlider =
        new SliderView(HORIZONTAL, 0, size - 1, size/2, size, 0, 10,
            null, null, null, null, false);
    
    protected SliderView vSlider =
        new SliderView(VERTICAL,   0, size - 1, size/2, size, 0, 10,
            null, null, null, null, false);
    
    
    protected Object[][] stuff = {
        { null,    hSlider },
        { vSlider, window  }
    };
    
    protected TablePanel panel =
        new TablePanel(stuff, gap, gap, CENTER);
    
    
    protected int radius = 50;
    
    protected Shape circle = new XCircle(0, 0, radius);
    
    protected ShapePaintable paintable =
        new ShapePaintable(circle, PaintMode.FILL_DRAW, Colors.darkorange);
    
    
    protected SimpleAction slidingAction =
        new SimpleAction("Sliding Action") {
            public void perform() { sliding(); }
    };
    
    
    protected MouseAction mousePressAction =
        new MouseAction("Mouse Press Action") {
            public void mouseActionPerformed(MouseEvent mevt) {
                mousePress(mevt);
            }
    };
    
    
    public SlidersDemo() {
        window.appendPaintable(paintable);
        
        adapter.addMousePressedAction(mousePressAction);
        adapter.addMouseDraggedAction(mousePressAction);
        
        vSlider.addSlidingAction(slidingAction);
        hSlider.addSlidingAction(slidingAction);
        
        sliding();
        
        panel.emptyBorder(gap);
        addObject(panel);
    }
    
    
    protected void sliding() {
        int x = hSlider.getValue();
        int y = vSlider.getValue();
        
        paintable.moveCenterTo(x, y);
        
        window.repaint();
    }
    
    
    protected void mousePress(MouseEvent mevt) {
        int x = mevt.getX();
        int y = mevt.getY();
        
        // By passing x,y to the corresponding sliders,
        // the values are forced into the range
        // 0 ... size-1
        
        // If the sliders were not present, this would
        // have to be done in this method
        
        hSlider.setValueHelper(x);
        vSlider.setValueHelper(y);
        
        // Calling the sliding() method then moves the
        // paintable and repaints the window
        
        sliding();
    }
    
    
    public static void main(String[] args) {
        new SlidersDemo().frame("Sliders Demo");
    }
    
}

