|
JVSlider |
|
/*
* JVirus is a PacMan clone, written in Java.
*
* Please read "http://jvirus.sourceforge.net/jvirus_licence.txt" for copyrights.
*
* The sourcecode is designed and created with
* Sun J2SDK 1.3 and Microsoft Visual J++ 6.0
*
* JVirus homepage: http://jvirus.sourceforge.net
*
* autor: Slawa Weis
* email: slawaweis@animatronik.net
*
*/
package org.game.JVirus;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* slider for music and siund volume
*
* @see org.game.JVirus.JVirus.Panel
*/
public class JVSlider extends JPanel
{
/**
* min value of slider
*/
protected int sliderMin = 0;
/**
* max value of slider
*/
protected int sliderMax = 100;
/**
* current slider position
*/
protected int sliderPos = 10;
/**
* callback listener list
*/
protected Vector vecEventListener = new Vector();
/**
* main constructor.
*
* @param min min value
* @param max max value
* @param pos start position
*/
public JVSlider(int min, int max, int pos)
{
super(null);
this.sliderMin = min;
this.sliderMax = max;
this.sliderPos = pos;
setPreferredSize(new Dimension(114, 13));
Dragger dragger = new Dragger();
addMouseListener(dragger);
addMouseMotionListener(dragger);
}
/**
* paintComponent overwritten to draw the slider
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Dimension size = getSize();
int h2 = size.height/2;
g.setColor(Color.green);
// g.drawRect(0, 0, size.width-1, size.height-1);
g.drawLine(7, h2, size.width - 7, h2);
g.fillOval(sliderPos+3, h2-4, 9, 9);
}
/**
* add callback listener
*/
public void addChangeListener(ChangeListener cl)
{
vecEventListener.addElement(cl);
}
/**
* remove callback listener
*/
public void removeChangeListener(ChangeListener cl)
{
vecEventListener.removeElement(cl);
}
/**
* get current slider position
*/
public int getPosition()
{
return sliderPos;
}
/**
* set the current position and send a event to the listeners
*/
protected void setSliderPosition(int pos)
{
this.sliderPos = pos;
// System.out.println("" + pos);
ChangeEvent e = new ChangeEvent(this);
Vector vecCopy = (Vector)vecEventListener.clone();
for(int i = 0; i < vecCopy.size(); i++)
{
((ChangeListener)vecCopy.elementAt(i)).stateChanged(e);
}
}
/**
* this class catch the mouse event to move the slider
*/
protected class Dragger extends MouseAdapter implements MouseMotionListener
{
protected boolean drag = false;
protected Point lastLocation = new Point();
protected Rectangle hitRect = new Rectangle();
public void mousePressed (MouseEvent e)
{
drag = false;
int h2 = getSize().height/2;
hitRect.setRect(sliderPos+3, h2-4, 8, 8);
if(hitRect.contains(e.getPoint()))
{
lastLocation.setLocation(e.getPoint());
drag = true;
}
}
public void mouseReleased(MouseEvent e)
{
drag = false;
}
public void mouseDragged(MouseEvent e)
{
if(drag)
{
// sliderPos = sliderPos + (e.getX() - lastLocation.x);
sliderPos = e.getX()-7;
if(sliderPos < 0) sliderPos = 0;
if(sliderPos > 100) sliderPos = 100;
lastLocation.setLocation(e.getPoint());
repaint();
setSliderPosition(sliderPos);
}
}
public void mouseMoved(MouseEvent e)
{
}
}
}
|
JVSlider |
|