import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//**********************************************
public class AnalogDigital extends Applet implements AdjustmentListener {
 private
         double analognaVrednost = 64.01;
         int digitalnaVrednost;
         int steviloBitov = 3;
         int steviloKvantov;
         int visinaStolpca = 256;
         int kvant;
         int visina;
		 Scrollbar analogSlider;
		 // naslednje je potrebno zaradi dvojnega pomnenja animirane slike..
		 Graphics bufferGraphics; 
         // The image that will contain everything that has been drawn on  bufferGraphics. 
         Image offscreen; 
         // To get the width and height of the applet. 
         Dimension dim; 
         int curX, curY; 


  public void init() {
        dim = getSize(); 
        setLayout(null);
	    this.setBackground(Color.black);
	    analogSlider = new Scrollbar(Scrollbar.VERTICAL, -64, 5, -261,0);
		analogSlider.setBounds(10,20,20,275);
        add(analogSlider);
		analogSlider.addAdjustmentListener(this);
        setBit(3);
		// Create an offscreen image to draw on 
         // Make it the size of the applet, this is just perfect larger size could slow it down unnecessary. 
        offscreen = createImage(dim.width,dim.height); 
        // by doing this everything that is drawn by bufferGraphics will be written on the offscreen image. 
        bufferGraphics = offscreen.getGraphics(); 

		repaint();

  }

  public void setBit(int b) {
         steviloBitov = b;
         steviloKvantov = (int) Math.pow(2.0, steviloBitov);
         kvant = visinaStolpca/steviloKvantov;
       repaint();

 }

 public void setVrednost(double v) {
         analognaVrednost = v;
		 analogSlider.setValue((int)(-5-v));
       repaint();

 }
 
 public void adjustmentValueChanged(AdjustmentEvent ae) {
		if (ae.getAdjustable() == analogSlider) {
			analognaVrednost = -5 -analogSlider.getValue();
			// Upocasnitev animacije, da zmanjsamo trepet slike...
			long startTime = System.currentTimeMillis( ); 
			long stopTime = startTime;
			while ((stopTime - startTime)<30)stopTime = System.currentTimeMillis( ); 
            repaint();
		}

	}


//**********************************************
  public void paint(Graphics  g) {
    int visinaTal = 280;
    // Wipe off everything that has been drawn before , Otherwise previous drawings would also be displayed. 
    bufferGraphics.clearRect(0,0,dim.width,dim.width); 
    bufferGraphics.setColor(Color.cyan);
    bufferGraphics.fillRect(50,visinaTal - (int)analognaVrednost,40,(int)analognaVrednost); // narisemo  analogni stoolpec
    bufferGraphics.drawLine(50,visinaTal - (int)analognaVrednost,250,visinaTal - (int)analognaVrednost); // crta do digi vrednosti
    bufferGraphics.drawRect(200,visinaTal- visinaStolpca,40,visinaStolpca);  // narisemo digitalni stolpec
    // kvantiziramo digitalni stolpec
    visina = visinaTal; digitalnaVrednost = 0;
    for (int i= 0;i< steviloKvantov; i++){
                visina = visina -kvant;
        if((analognaVrednost> i *(visinaStolpca/steviloKvantov))&&(i>0)) {
            bufferGraphics.setColor(Color.yellow);
            bufferGraphics.fillRect(200, visina+kvant, 40, kvant);
            digitalnaVrednost= i *(visinaStolpca/steviloKvantov);
        }
    }
    visina = visinaTal;
    for (int i= 0;i< steviloKvantov; i++){
             visina = visina -kvant;
             bufferGraphics.setColor(Color.white);
             bufferGraphics.drawLine(190,visina,190+30,visina);
    }

    bufferGraphics.setColor(Color.green);
    bufferGraphics.drawLine(0,visinaTal,250,visinaTal); // narisemo tla
    bufferGraphics.setColor(Color.white);
    bufferGraphics.setFont(new Font("Helvetica",Font.BOLD,12));
    bufferGraphics.drawString("Dolžina registra (bitov) = " + Integer.toString(steviloBitov),250,40);
    bufferGraphics.drawString("Velikost kvanta = " + Integer.toString(kvant),250,70);
    bufferGraphics.drawString("Analogna vrednost = "+ Double.toString(analognaVrednost),30,295);
    bufferGraphics.drawString("Digitalna vrednost = " + Integer.toString(digitalnaVrednost),250,295);
	g.drawImage(offscreen,0,0,this); 

  }
}
