import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//**********************************************
public class Barve extends Applet implements MouseListener, MouseMotionListener {
 private
         int xPosRed, xPosGreen, xPosBlue;
         int yPosRed, yPosGreen, yPosBlue;
         int red,green, blue;
         int r = 10;
         int xMin = 20,  xMax;
           
   // 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() {
        xMax = xMin + 255;
        dim = getSize(); 
        xPosRed = 250; xPosGreen = 50; xPosBlue = 250;
        yPosRed = 40; yPosGreen = 80; yPosBlue = 120;
        
        setLayout(null);
        this.setBackground(Color.black);
        addMouseListener(this); addMouseMotionListener(this);
  // 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(); 
        red = xPosRed-xMin;  blue = xPosBlue-xMin;   green = xPosGreen-xMin;
        repaint();

  }

    public void mouseDragged(MouseEvent e) { 
      int x, y;
      x=e.getX();
      y=e.getY();
      if ((x< (xPosRed+r))&&(x> (xPosRed-r))&&(y< (yPosRed+r))&&(y> (yPosRed-r))&&(x>(xMin+r))&&(x<(xMax+xMin-2*r+1))) {
          xPosRed = x;
          red = xPosRed-xMin;
      }
      if ((x < (xPosGreen+r))&&(x> (xPosGreen-r))&&(y< (yPosGreen+r))&&(y> (yPosGreen-r))&&(x>(xMin+r))&&(x<(xMax+xMin-2*r+1))) {
          xPosGreen = x;
          green = xPosGreen-xMin;
      }
      if ((x< (xPosBlue+r))&&(x> (xPosBlue-r))&&(y< (yPosBlue+r))&&(y> (yPosBlue-r))&&(x>(xMin+r))&&(x<(xMax+xMin-2*r+1))) {
          xPosBlue = x;
          blue = xPosBlue-xMin;
      }
      repaint();
   }
    
    public void mouseClicked(MouseEvent e) { 
    }

    public void mouseExited(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) { }
    public void mouseReleased(MouseEvent e) { }
    public void mousePressed(MouseEvent e) { }
    public void mouseMoved(MouseEvent e) { }

//**********************************************
  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.red);
    bufferGraphics.fillRect(xMin,yPosRed,xMax- 2*r, 5); // narisemo  crto rdecega drsnika
    bufferGraphics.fillOval(xPosRed - r,yPosRed - r,2*r, 2*r); 
    bufferGraphics.setColor(Color.green);
    bufferGraphics.fillRect(xMin,yPosGreen,xMax - 2*r, 5); // narisemo  crto zelenega drsnika
    bufferGraphics.fillOval(xPosGreen - r,yPosGreen - r,2*r, 2*r); 
    bufferGraphics.setColor(Color.blue);
    bufferGraphics.fillRect(xMin,yPosBlue,xMax - 2*r, 5); // narisemo  crto modrega drsnika
    bufferGraphics.fillOval(xPosBlue - r,yPosBlue - r,2*r, 2*r); 
    bufferGraphics.setColor(new Color(red,0,0));
    bufferGraphics.fillRect(120,140,40, 20); // narisemo rdec pravokotnik
    bufferGraphics.setColor(new Color(0, green, 0));
    bufferGraphics.fillRect(120,160,40, 20); // narisemo zelenn pravokotnik
    bufferGraphics.setColor(new Color(0,0,blue));
    bufferGraphics.fillRect(120,180,40, 20); // narisemo moder pravokotnik
    bufferGraphics.setColor(new Color(red,green,blue));
    bufferGraphics.fillRect(170,140,100, 60); // narisemo pobarvan pravokotnik
    bufferGraphics.setColor(Color.white);
    bufferGraphics.setFont(new Font("Helvetica",Font.BOLD,12));
    bufferGraphics.drawString("Rdeca =  " + Integer.toString(red),xMin,150);
    bufferGraphics.drawString("Zelena = " + Integer.toString(green),xMin, 170);
    bufferGraphics.drawString("Modra =  "+ Integer.toString(blue),xMin, 190);
    g.drawImage(offscreen,0,0,this); 

  }
}
