// /* * GammaButton.java * Version 1.0.0 * Written by Elijah Dean Meeker 1/4/96 * elijah@bga.com * http://www.realtime.net/~elijah/ */ import java.awt.Graphics; import java.awt.Event; import java.awt.Image; import java.awt.MediaTracker; import java.net.URL; import java.net.MalformedURLException; import java.lang.InterruptedException; import java.applet.Applet; public class GammaButton extends java.applet.Applet{ private MediaTracker tracker; private Image img[] = new Image[3]; private boolean onButt = false; // 마우스가 버튼 내부에 들어오면 true private boolean pressedButt = false; // 버튼이 눌려지면 true private boolean three_img = true; // 3-상태인지 2-상태인지 알리는 변수. 초기값은 3-상태. private int onIs = 0; // 이미지 초기값은 보통 상태의 버튼 이미지 private URL clickDest; // dest 스트링을 URL로 바꾼 것 private String dest; // 이동할 대상 URL 스트링 public void init(){ String istr; tracker = new MediaTracker(this); // 이 애플릿의 MediaTracker 생성. for (int i = 0; i < 3; i++) { // 변수 image0, image1, image2를 HTML문서에서 읽는다. istr = getParameter("image"+i); if (istr == null){ // 이미지 변수가 하나라도 없으면 3-상태가 아니다. three_img = false; }else{ // 이미지를 읽어들여 미디어트래커에 등록한다. img[i] = getImage(getCodeBase(),istr); tracker.addImage(img[i], 0); try { tracker.waitForAll(); // 미디어트래커에 등록된 모든 이미지를 읽어들이기 시작한다. } catch (InterruptedException e) { System.out.println("Error waiting for image"+i+" to load"); } } } dest = getParameter("dest"); // 버튼이 눌려질 때 이동할 목적 URL을 읽어들인다. try{ clickDest = new URL(dest); // 스트링을 URL로 바꾼다. }catch(MalformedURLException mal){ System.out.println("Malformed URL: Check Applet Tag."); } } // end init public void start(){ repaint(); // update()를 호출한다. } // end start public void stop(){ }//end stop public void destroy(){ }//end destroy // 이벤트 핸들러들 public boolean mouseDown(Event e, int x, int y){ pressedButt = true; // 버튼이 눌려지면 pressedButt 필드를 true로 하고 repaint(); // 다시 update()를 호출 return(true); }//end mouseDown public boolean mouseUp(Event e, int x, int y){ if (pressedButt && onButt){ // 버튼이 눌려졌다가 버튼 안에서 마우스가 놓여지면 pressedButt = false; // pressedButt 필드를 false로 하고 repaint(); // 다시 update()를 호출한 다음, getAppletContext().showDocument(clickDest); // 목적 URL로 이동한다. } else { // 마우스를 버튼 밖에서 눌렀다가 안에서 놓거나, //버튼을 눌렀다가 버튼 밖에서 마우스를 놓은 경우 pressedButt = false; repaint(); // 다시 update() 호출 } return(true); }//end mouseUp public boolean mouseEnter(Event e, int x, int y){ onButt = true; // 마우스가 애플릿(버튼) 안에 들어오면 onButt 필드를 true로 하고 showStatus(dest); // 목적 URL을 브라우저의 상태 바에 나타낸다. repaint(); // update() 호출 return(true); }//end mouseEnter public boolean mouseExit(Event e, int x, int y){ onButt = false; // 마우스가 애플릿(버튼) 밖으로 나가면 onButt 필드를 false로 하고 showStatus(""); // 상태 바에 나타났던 목적 URL을 지운다. repaint(); // update() 호출 return(true); }//end mouseExit // end 이벤트 핸들러 public void update(Graphics g){ // repaint()를 실행하면 결과적으로 update()가 호출된다. if(!onButt) // 마우스가 버튼 밖에 있으면 if(three_img){ // 3-상태 버튼 onIs = 0; // 일반 상태의 버튼 이미지 }else{ // 2-상태 버튼 onIs = 1; // Up 상태의 버튼 이미지 } else if (onButt && !pressedButt) // 마우스가 버튼 내부에 있고 버튼이 눌려지지 않았으면 onIs = 1; // Up 상태의 버튼 이미지 else // 마우스가 버튼 내부에 있고 버튼이 눌려졌으면 onIs = 2; // Down 상태의 버튼 이미지 paint(g); // paint() 호출 }//end update public void paint(Graphics g){ g.drawImage(img[onIs], 0,0,this); // 지정된 이미지로 버튼을 그린다. }//end paint }//end class GammaButton