package edu.school;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JSlider;

public class ColoredSlider extends JComponent{
	
	private BufferedImage image;
	private BufferedImage sliderImage;
	private int value;
	public ColoredSlider(String backgroundImage, String sliderImagePath)
	{
		setPreferredSize(new Dimension(0, 40));
		try{
			image = ImageIO.read(new File(backgroundImage));
			sliderImage = ImageIO.read(new File(sliderImagePath));
		}
		catch(IOException e)
		{
			
		}
		value = 30;
	}
	public void setValue(int v)
	{
		value = v;
		invalidate();
		repaint();
	}
	
	public void setBackgroundImage(String path)
	{
		try{
	   		image = ImageIO.read(new File(path));
	   		invalidate();
	   		repaint();
	   	}
    	catch(IOException ex)
		{
	    		
	    }
	}
	@Override
	protected void paintComponent(Graphics g)
	{
		 Graphics2D g2d = (Graphics2D) g.create();
		 g2d.scale((double)this.getWidth()/(double)image.getWidth(), (double)image.getHeight()/1.5);
	     g2d.drawImage(image, 0, 3, this);
	     int sliderValue = value;
	     int max = 50;
	     int min = 10;
	     int values = max - min;
	     double v = (double)this.getWidth() / (double)values;
	     double xSliderPos = (sliderValue-min)*v - (double)sliderImage.getWidth()/2.0;
	     double ySliderPos = ((double)this.getHeight() - (double)sliderImage.getHeight())/2.0;
	     g2d = (Graphics2D) g.create();
	     g2d.drawImage(sliderImage, (int)xSliderPos, (int)ySliderPos, this);
	}
}
