package ch.werner_randelshofer.gui;/*Copyright (C) 1999 Werner Randelshoferwerner.randelshofer@mythen.chhttp://www.mythen.ch/w.randelshofer/    Permission to use this release of HorizontalSlider is herebygranted without fee provided that the complete copyrightnotice and this permission notice appear in all copiesand in supporting documentation.*/import java.awt.Canvas;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Event;import ch.werner_randelshofer.gui.event.ChangeListener;import ch.werner_randelshofer.gui.event.ChangeEvent;import ch.werner_randelshofer.gui.event.EventListenerList;import ch.werner_randelshofer.gui.model.BoundedRangeModel;import ch.werner_randelshofer.gui.model.DefaultBoundedRangeModel;/**@author	Werner Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland@version	1999-02-28	Setter/getter for thumb width added. ComputeThumbPos						made protected (was private).<br>history	1999-02-21	Revised.*/public class HorizontalSliderextends Canvasimplements ChangeListener	{	private BoundedRangeModel model_;	private int thumbWidth_ = 11, thumbHeight_ = 11;	/**	Last painted position of the thumb.	*/	protected int thumbPos_ = -1;		public HorizontalSlider()		{		model_ = new DefaultBoundedRangeModel();		model_.addChangeListener(this);		}		public void setThumbWidth(int width)		{ thumbWidth_ = width; }	public int getThumbWidth()		{ return thumbWidth_; }	public void setThumbHeight(int height)		{ thumbHeight_ = height; }	public int getThumbHeight()		{ return thumbHeight_; }		public synchronized void setModel(BoundedRangeModel m)		{		if (model_ != null)			{ model_.removeChangeListener(this); }		model_ = m == null ? new DefaultBoundedRangeModel() : m;		if (model_ != null)			{ model_.addChangeListener(this); }		repaint();		}    public Dimension preferredSize()		{        return new Dimension(100, thumbHeight_ + 2);		}    public Dimension minimumSize()		{        return new Dimension(thumbWidth_ * 2 + 2, thumbHeight_ + 2);		}	public void paint(Graphics g)		{		int width = size().width - 1;		int halfThumb = getThumbWidth() / 2;		int thumbPos = computeThumbPos();		thumbPos_ = thumbPos;		g.drawLine(halfThumb,0,width - halfThumb ,0);		g.drawLine(thumbPos,1,thumbPos + halfThumb,halfThumb + 1);		g.drawLine(thumbPos + halfThumb,halfThumb + 1,thumbPos - halfThumb,halfThumb + 1);		g.drawLine(thumbPos - halfThumb,halfThumb + 1,thumbPos,1);		}    public boolean mouseDown(Event e, int x, int y)	    {		moveThumb(x);        return true;		}    public boolean mouseDrag(Event e, int x, int y)	    {        moveThumb(x);        return true;		}    public boolean mouseUp(Event e, int x, int y)	    {        moveThumb(x);        return true;		}	protected void moveThumb(int mousePosition)		{		int width = size().width;		int halfThumb = getThumbWidth() / 2;		if (mousePosition <= halfThumb) { mousePosition = halfThumb + 1; }		if (mousePosition >= width - halfThumb) { mousePosition = width - halfThumb - 1; }		float thumbPos = (mousePosition - halfThumb - 1) / (float)(width - (halfThumb * 2 + 2));		model_.setValue((int)(thumbPos * (model_.getMaximum() - model_.getMinimum())));		}		protected int computeThumbPos()		{		int halfThumb = getThumbWidth() / 2;		BoundedRangeModel m = model_;		if (m == null)			{ return halfThumb; }		int width = size().width - getThumbWidth()-1;		float thumbPos = m.getValue() / (float)((m.getMaximum() - m.getMinimum()));		return (int)(width * thumbPos) + halfThumb;		}		public void stateChanged(ChangeEvent event)		{		if (computeThumbPos() != thumbPos_)			{ repaint(); }		}	}