package osBridge;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.image.*;

public class BrPanel extends JPanel{

   private Image bgImage = null;
   private Image redBlock = null;
   private Image yellowBlock = null;
   private Image purpleBlock = null;

   private BufferedImage globalPaintImage = new BufferedImage(220,220,BufferedImage.TYPE_INT_RGB);
   private Graphics2D gpi = (Graphics2D) globalPaintImage.getGraphics();

   private static Point[] yellPoints = { new Point(180,20) , new Point(160,20) , new Point(140,20) , new Point(120,20),
				         new Point(100,20) , new Point(80, 20) , new Point( 70,40) ,
					  // bridge co-ords in here
				         new Point(60,60) , new Point(60,80) , new Point(40,80) , new Point(20,80) ,
				         new Point(20,100), new Point(20,120), new Point(40,120), new Point(60,120),
					 new Point(60,140) , 
					  // endpoints
					 new Point(50,160)  , new Point(40,180)  , new Point(20,180) };

   private static Point[] purpPoints = { new Point(180,180), new Point(160,180), new Point(140,180), new Point(120,180),
				         new Point(100,180) ,  new Point(80,180) , new Point(70,160),
					  // bridge co-ords in reverse here
					 new Point(60,140) , 
				         new Point(60,120) , new Point(40,120) , new Point(20,120) , new Point(20,100) ,
				         new Point(20,80), new Point(40,80), new Point(60,80), new Point(60,60),
					  // endpoints
					  new Point(50,40), new Point(40,20), new Point(20,20) } ;


   private static Point[] redPoints =  { new Point(60,60) , new Point(60,80) , new Point(40,80) , new Point(20,80) ,
				         new Point(20,100), new Point(20,120), new Point(40,120), new Point(60,120),
					 new Point(60,140) }; 

   private static Point redMutPos = new Point(160,100);
   private static Point yellMutPos = new Point(140,100);
   private static Point purpMutPos = new Point(120,100);

   public BrPanel(JApplet src){

      setMinimumSize( new Dimension(220,220) );
      setMaximumSize( new Dimension(220,220) );
      setPreferredSize( new Dimension(220,220) );  // you get the feeling i wanna be 220 x 220???
   
      try{
         bgImage = src.getImage(src.getCodeBase(),"bg1.jpg");
         redBlock = src.getImage(src.getCodeBase(),"red.jpg");
         yellowBlock = src.getImage(src.getCodeBase(),"yellow.jpg");
         purpleBlock = src.getImage(src.getCodeBase(),"purple.jpg");
      }catch(Exception e){
	 e.printStackTrace();
	 try{
	    bgImage = ImageIO.read(getClass().getResource("bg1.jpg").openStream());
	    redBlock = ImageIO.read(getClass().getResource("red.jpg").openStream());
	    yellowBlock = ImageIO.read(getClass().getResource("yellow.jpg").openStream());
	    purpleBlock = ImageIO.read(getClass().getResource("purple.jpg").openStream());
	 }catch(Exception e2){
	 e.printStackTrace();
	 }
      }

   }


   public void paint(Graphics g){
      try{
	 g.drawImage(globalPaintImage,0,0,220,220,this);
      }catch(Exception e){
      }
   }

   

   public synchronized void updateGraphics(boolean redM, boolean yellM, boolean purpM, boolean[] redOn, boolean[] yellOn, boolean[] purpOn){
      gpi.drawImage(bgImage,0,0,220,220,this);
      if(redM){
	 gpi.drawImage(redBlock,redMutPos.x,redMutPos.y,this);
      }

      if(yellM){
	 gpi.drawImage(yellowBlock,yellMutPos.x,yellMutPos.y,this);
      }

      if(purpM){
	 gpi.drawImage(purpleBlock,purpMutPos.x,purpMutPos.y,this);
      }


      for(int i=0;i<yellOn.length && i<yellPoints.length;i++){
	 if(yellOn[i]){
	    gpi.drawImage(yellowBlock,yellPoints[i].x,yellPoints[i].y,this);
	 }

      }

      for(int i=0;i<purpOn.length && i<purpPoints.length;i++){
	 if(purpOn[i]){
	    gpi.drawImage(purpleBlock,purpPoints[i].x,purpPoints[i].y,this);
	 }
      }

      for(int i=0;i<redOn.length && i<redPoints.length;i++){
	 if(redOn[i]){
	    gpi.drawImage(redBlock,redPoints[i].x,redPoints[i].y,this);
	 }
      }


      paint(this.getGraphics());

   }

   public void update(Graphics g){
      ; // overriden
   }

}
