/**
 * Counter Version 1.0 Using JDK 1.0
 * @Writer lckung@iis.sinica.edu.tw b6506053@csie.ntu.edu.tw
 */
import java.awt.*; 
import java.applet.*;
import java.io.*;
import java.net.*;
import java.lang.*;
 
public class Counter extends Applet {
		
	Color fgColor,offColor,hlColor,bgColor,bhilite,bcolor,bshadow;
	final int HEIGHT = 7,LED_X = 100, LED_Y=50, LED_WIDTH = 250, LED_HEIGHT= 35;


	boolean laidout=false;
	int counterValue=0;							// holds the value of current count
	Index fontIndex[] = new Index[10];			// fonts data
	ButtonPanel buttonPanel = new ButtonPanel();
	int border,offset;
	
	public void init() {
		// init font
		initFont();
		
		// disable the layout manager
		setLayout(null);
  	  	
  	  	add( buttonPanel);
  	  	
	  	// set background color
		setBackground( Color.blue);

		// 
		offset = 3*border;
		fgColor= new Color(255,0,0); 	// Default red
		offColor= new Color(80,80,80);   // off color
		hlColor= new Color(100,100,100);// highlight color
		bgColor = Color.black;// background color
		bhilite = Color.white;
        bcolor = Color.lightGray;
        bshadow = Color.darkGray;

	}
	
	public void start() {
	}
	
	public void paint(Graphics g) {
		if( !laidout) {	// lay out
			buttonPanel.reshape(0,140,450,70);
			buttonPanel.getLayout().layoutContainer(buttonPanel);
			laidout = true;
		}
		
		drawLedBackground(g);
		drawString(g, ""+counterValue );
	}

	public boolean action(Event e, Object arg) {
            Object target = e.target;
            
            if( target == buttonPanel.arrButton[0] ) { // Add 1 is pressed
				counterValue++;    			
            	repaint();
            	return true;
            }
            else if( target == buttonPanel.arrButton[1] ) { // Sub 1 is pressed
            	if( counterValue > 0 )
            		counterValue--;
            	repaint();
        	    return true;
            }
            else if( target == buttonPanel.arrButton[2] ) { // Reset is pressed
            	counterValue = 0;
            	repaint();
            	return true;
            }
            else
            	return false;
    }
	
	void drawLedBackground(Graphics gr) {

		gr.setColor(bgColor);
		gr.fillRect(LED_X,LED_Y,LED_WIDTH,LED_HEIGHT);
    }
	
   // Draw a pretty little LED
   private void drawLED(int x, int y, boolean on, Graphics gr)
   {
      if(on)
      {
         gr.setColor(fgColor);
      }
      else  // its off
      {
         gr.setColor(offColor);
      }

      // The original size seen in previous versions
      gr.fillRect(x+1,y,2,4);
      gr.fillRect(x,y+1,4,2);
      
      if(!on)
      {
         gr.setColor(hlColor);
         gr.drawLine(x+1,y+1,x+1,y+1);  // the cool little highlight
      }
   }
    
	void drawString(Graphics g, String str) {
		int i,len=str.length();
		int j,k,num,width;
		int x,y;
		final int N_SPOT=5;
		
		x = LED_X + LED_WIDTH - N_SPOT;
		for(i=len-1;i>=0;i--) {
			num = 0 ; 		// set default number to 0
			num = (int)(str.charAt(i)-'0');
			width = fontIndex[num].width;
			
			for(k=0;k<width;k++,x -= N_SPOT) {
				for(j=0,y=LED_Y;
					j<HEIGHT;
					j++,y+=N_SPOT) 
				{
					drawLED( x,y, fontIndex[num].letter[width-k-1][j], g);
				}

			}
			
		}
		
		while( x >= LED_X ) {
			for(y=LED_Y,j=0;j<HEIGHT;j++,y+=N_SPOT)
				drawLED(x,y,false,g);
			x -= N_SPOT;
		}
	}
		

	void initFont() {		
		InputStream file;
   		int width,i,j,k,t;
   		URL url;
   		StreamTokenizer st;   		
   		
   		try {
   			url = new URL( getCodeBase(),getParameter( "font"));
   			// debug information
			System.out.println("Reading font data " + url);
   			
			file = url.openStream();
			st = new StreamTokenizer(file);
			st.resetSyntax();
			st.parseNumbers();
			
			for(i=0;i<10;i++) {
				do {
					st.nextToken();
				} while( st.ttype != st.TT_NUMBER );
				width = (int)st.nval;
				
				fontIndex[i] = new Index(width,HEIGHT);
				for(j=0;j<HEIGHT;j++) {
					for(k=0;k<width;k++) {
					do {
						st.nextToken();
					} while( st.ttype != st.TT_NUMBER );
					t = (int)st.nval;
					
						if( t == 0 ) 
							fontIndex[i].letter[k][j] = false;
						else
							fontIndex[i].letter[k][j] = true;
					}
				}
			}
			
			file.close();

		} catch( MalformedURLException m) {
			System.out.println("Open URL error !");			
		} catch (IOException e) {
        	e.printStackTrace();
		} 
		
	}
};	// end of class Counter


class ButtonPanel extends Panel {

	public Button arrButton[] = new Button[3];
	
	// Constructor	
	public ButtonPanel() {
		// call to class Panel's constructor
		super();
		
		// set Layout Manager of ButtonPanel
		setLayout( new GridLayout(1,3));
		
		// create 3 instances of Button
		arrButton[0] = new Button("Add 1(+1)");
		arrButton[1] = new Button("Substract 1(-1)");
		arrButton[2] = new Button("Reset (to 0)");			
		
		// add Buttons into Panel
		add(arrButton[0]);		
		add(arrButton[1]);		
		add(arrButton[2]);
		
	}

	public Insets insets() {
        return new Insets(4,4,5,5);
    }

};	// end of class ButtonPanel

class Index {
	public boolean letter[][];			// the pixel array represent that letter
	public int width;
	
	Index(int w,int h) {
		width = w;
		letter = new boolean[w][h];
	}
};

