|
|
DownLoad PhotoAlbum.lzh(ソースコード付き) //////////////////////////////////////////////////////////////////////// // WebAlbumアプレット / I-ichirow Suzuki // webAlbum.java // import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; import netscape.javascript.JSObject; //--------------------------------------------------------------------- public class webAlbum extends Applet implements Runnable { static int BUTTON_WIDTH = 75; static int BUTTON_HEIGHT = 20; static int FONT_SIZE = 18; static int CORNER_SIZE = 10; String appletInfo; Thread Runner; URL Host; Image Picture; Image os_Image; Graphics os_Graphics; String textNext; String textPrevious; String textOf; String textIdle; String textClickNext; String textClickPrevious; String textClickPicture; String textLoadingImage; String textLoadingAlbum; String textMoment; String clickPicture; String Title; String BasePath; String Address[]; String Caption[]; String DefaultFont; String Status; String defaultTarget; int Total; int Pointer; int Delay; int buttonState; int CaptionLines; Color c_Background; Color c_Border; Color c_Corners; Color c_Title; Color c_Text; Color c_Button; Font f_Default; FontMetrics fm; boolean IsValid; boolean IsRunning; boolean IsBuffering; boolean Registered; boolean ImageBuffering; boolean ForgotAlbum; boolean InvalidAlbum; boolean BorderOn; boolean CornersOn; boolean AlbumReady; boolean PlaySound; boolean SlideShow; oButton b_Back; oButton b_Forward; int s_ButtonWidth; int s_ButtonHeight; int s_Font; int imgX; int imgL; int imgY; int imgM; AudioClip clickAu; //----------------------------------------------------- public webAlbum() { } //----------------------------------------------------- public void init() { Total = 0; Pointer = 0; IsRunning = false; AlbumReady = false; IsBuffering = false; BorderOn = true; CornersOn = true; Registered = true; handleParameters(); if(getSize().width < 225 || getSize().height < 225) { s_ButtonWidth = 0; s_ButtonHeight = 0; s_Font = (FONT_SIZE * 3) / 4; } else if(getSize().width < 350 || getSize().height < 350) { s_ButtonWidth = (BUTTON_WIDTH * 3) / 4; s_ButtonHeight = (BUTTON_HEIGHT * 3) / 4; s_Font = (FONT_SIZE * 3) / 4; } else { s_ButtonWidth = BUTTON_WIDTH; s_ButtonHeight = BUTTON_HEIGHT; s_Font = FONT_SIZE; } os_Image = createImage(getSize().width, getSize().height); os_Graphics = os_Image.getGraphics(); c_Background = Color.black; int i = getSize().width / 8 - s_ButtonWidth / 2; int j = getSize().height - 15 - s_ButtonHeight / 2; int k = i + s_ButtonWidth; int l = j + s_ButtonHeight; b_Back = new oButton(textPrevious, i, j, k, l, this); i = (getSize().width * 7) / 8 - s_ButtonWidth / 2; k = i + s_ButtonWidth; b_Forward = new oButton(textNext, i, j, k, l, this); String s = getParameter("album"); if(s != null) { try { Host = new URL(getCodeBase(), s); } catch(MalformedURLException _ex) { System.out.println("Bad URL: " + Host); } ForgotAlbum = false; return; } else { ForgotAlbum = true; return; } } //----------------------------------------------------- public void start() { if(Runner == null) { Runner = new Thread(this); Runner.start(); } } //----------------------------------------------------- public void stop() { if(Runner != null) { Runner.stop(); Runner = null; } } //----------------------------------------------------- public void run() { if(IsRunning) { setPicture(0); return; } IsRunning = true; loadAlbum(); bufferImages(); setPicture(0); if(!SlideShow) { return; } do { do { repaint(); long l1 = System.currentTimeMillis() + (long)Delay; long l = System.currentTimeMillis(); if(l < l1) try { Thread.sleep((int)(l1 - l)); } catch(InterruptedException _ex) { } setPicture(1); } while(!PlaySound); clickAu.play(); } while(true); } //----------------------------------------------------- public void update(Graphics g) { paint(g); } //----------------------------------------------------- public void paint(Graphics g) { if(s_ButtonWidth == 0) { Status = "Dimensions Too Small"; paintStatus("<< Too Small >>", "Minimum 225x225 pixels", os_Graphics); g.drawImage(os_Image, 0, 0, this); AlbumReady = false; return; } if(ForgotAlbum) { Status = "Missing Parameter"; paintStatus("<< Missing Parameter >>", "Please specify an \"album\" source in your HTML file ...", os_Graphics); g.drawImage(os_Image, 0, 0, this); return; } if(InvalidAlbum) { Status = "Invalid Album / Non-existent"; paintStatus("<< Invalid Album / Non-existent >>", "Please make sure filename is correct ...", os_Graphics); g.drawImage(os_Image, 0, 0, this); return; } if(!AlbumReady) { Status = textLoadingAlbum; paintStatus(textLoadingAlbum, textMoment, os_Graphics); g.drawImage(os_Image, 0, 0, this); return; } if(IsBuffering) return; paintButtons(os_Graphics, false); g.drawImage(os_Image, 0, 0, this); setBackground(c_Background); os_Graphics.setColor(c_Background); os_Graphics.fillRect(0, 0, getSize().width, getSize().height); if(DefaultFont != null) { f_Default = new Font(DefaultFont, 1, s_Font); fm = getFontMetrics(f_Default); String s = "( " + (Pointer + 1) + " " + textOf + " " + Total + " )"; os_Graphics.setFont(f_Default); os_Graphics.setColor(c_Title); os_Graphics.drawString(Title, getSize().width / 8 - s_ButtonWidth / 2, 16); f_Default = new Font(DefaultFont, 0, s_Font); os_Graphics.setFont(f_Default); fm = getFontMetrics(f_Default); os_Graphics.setColor(c_Text); os_Graphics.drawString(s, ((getSize().width * 7) / 8 + s_ButtonWidth / 2) - fm.stringWidth(s), 16); } if(Picture != null && IsValid) { imgX = getSize().width / 2 - Picture.getWidth(this) / 2; imgY = getSize().height / 2 - Picture.getHeight(this) / 2; imgL = imgX + Picture.getWidth(this); imgM = imgY + Picture.getHeight(this); String s2; if(imgX < getSize().width / 2 - 5) { String s1 = Caption[Pointer]; if(!s1.equalsIgnoreCase("none")) paintCaption(s1, os_Graphics); if(BorderOn) paintBorder(imgX, imgY, os_Graphics); os_Graphics.drawImage(Picture, imgX, imgY, this); if(CornersOn) paintCorners(imgX, imgY, os_Graphics); } else { s2 = textLoadingImage; } } if(!IsValid) { String s3 = "<< Invalid URL / Non-existent >>"; Font font = new Font(DefaultFont, 0, s_Font); fm = getFontMetrics(font); os_Graphics.setFont(font); os_Graphics.setColor(c_Text); os_Graphics.drawString(s3, getSize().width / 2 - fm.stringWidth(s3) / 2, getSize().height / 2 + 20); } paintButtons(os_Graphics, true); g.drawImage(os_Image, 0, 0, this); } //----------------------------------------------------- void setPicture(int i) { if(i == -1) { if(Pointer == 0) Pointer = Total - 1; else Pointer--; } else if(i == 1) if(Pointer == Total - 1) Pointer = 0; else Pointer++; loadImage(Pointer); repaint(); } //----------------------------------------------------- public boolean mouseDown(Event event, int i, int j) { if(b_Back.handleMouse(event.x, event.y, true)) { setPicture(-1); if(PlaySound) clickAu.play(); } else if(b_Forward.handleMouse(event.x, event.y, true)) { setPicture(1); if(PlaySound) clickAu.play(); } else if(event.x >= imgX && event.x <= imgL && event.y >= imgY && event.y <= imgM) { if(PlaySound) clickAu.play(); int k; String s; if((k = Caption[Pointer].indexOf(124)) != -1) s = Caption[Pointer].substring(k + 1, Caption[Pointer].length()).trim(); else s = Address[Pointer]; if((k = s.indexOf(124)) != -1) s = s.substring(0, k - 1).trim(); String s1; if((k = s.indexOf(61)) != -1) { s1 = s.substring(k + 1, s.length()).trim(); s = s.substring(0, k - 1).trim(); } else { s1 = defaultTarget; } URL url = null; if(s.indexOf("javascript:") != -1) { } else { try { if(s.indexOf("http://") == -1 && s.indexOf("https://") == -1 && s.indexOf("ftp://") == -1) { if(BasePath.trim().equalsIgnoreCase("none")) url = new URL(getDocumentBase(), s); else url = new URL(BasePath + s); } else { url = new URL(s); } } catch(MalformedURLException _ex) { IsValid = false; } getAppletContext().showDocument(url, s1); } } return true; } //----------------------------------------------------- public boolean mouseUp(Event event, int i, int j) { return mouseMove(event, i, j); } //----------------------------------------------------- public boolean mouseMove(Event event, int i, int j) { if(b_Back.isOver(event.x, event.y)) Status = textClickPrevious; else if(b_Forward.isOver(event.x, event.y)) Status = textClickNext; else if(event.x >= imgX && event.x <= imgL && event.y >= imgY && event.y <= imgM) Status = clickPicture; else if(AlbumReady) if(Registered) Status = textIdle; else Status = appletInfo; if(b_Back.handleMouse(event.x, event.y, false)) repaint(); else if(b_Forward.handleMouse(event.x, event.y, false)) repaint(); showStatus(Status); return true; } //----------------------------------------------------- public void loadAlbum() { InvalidAlbum = false; URLConnection urlconnection; try { urlconnection = Host.openConnection(); urlconnection.setUseCaches(false); urlconnection.connect(); } catch(IOException _ex) { InvalidAlbum = true; return; } try { InputStream inputstream = urlconnection.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream, "SJIS"); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); Title = loadLine(bufferedreader); String s = loadLine(bufferedreader); if(Registered) c_Background = new Color(Integer.valueOf(s, 16).intValue()); else c_Background = new Color(Integer.valueOf("eeeeee", 16).intValue()); s = loadLine(bufferedreader); if(Registered) try { c_Border = new Color(Integer.valueOf(s, 16).intValue()); } catch(NumberFormatException _ex) { BorderOn = false; } else c_Border = new Color(Integer.valueOf("cccccc", 16).intValue()); s = loadLine(bufferedreader); if(Registered) try { c_Corners = new Color(Integer.valueOf(s, 16).intValue()); } catch(NumberFormatException _ex) { CornersOn = false; } else CornersOn = false; s = loadLine(bufferedreader); if(Registered) c_Title = new Color(Integer.valueOf(s, 16).intValue()); else c_Title = Color.black; s = loadLine(bufferedreader); if(Registered) c_Text = new Color(Integer.valueOf(s, 16).intValue()); else c_Text = new Color(Integer.valueOf("000088", 16).intValue()); s = loadLine(bufferedreader); if(Registered) c_Button = new Color(Integer.valueOf(s, 16).intValue()); else c_Button = new Color(Integer.valueOf("000088", 16).intValue()); DefaultFont = loadLine(bufferedreader); BasePath = loadLine(bufferedreader); s = loadLine(bufferedreader); PlaySound = s.equalsIgnoreCase("yes"); if(PlaySound) { clickAu = getAudioClip(getCodeBase(), "webAlbum.au"); if(clickAu == null) PlaySound = false; } s = loadLine(bufferedreader); SlideShow = s.equalsIgnoreCase("yes"); s = loadLine(bufferedreader); Delay = Integer.parseInt(s) * 1000; s = loadLine(bufferedreader); if(s.equalsIgnoreCase("overlay")) buttonState = oButton.OBS_NORMAL; else if(s.equalsIgnoreCase("frozen")) buttonState = oButton.OBS_OVERLAY; else buttonState = oButton.OBS_OFF; b_Back.setDefaultState(buttonState); b_Forward.setDefaultState(buttonState); s = loadLine(bufferedreader); Total = Integer.valueOf(s).intValue(); f_Default = new Font(DefaultFont, 0, s_Font); b_Back.setLabelFont(f_Default); b_Back.setLabelColor(c_Button); b_Forward.setLabelFont(f_Default); b_Forward.setLabelColor(c_Button); Address = new String[Total]; Caption = new String[Total]; String s1; for(int i = 0; (s1 = loadLine(bufferedreader)) != null && i < Total; i++) { int j = s1.indexOf(124); if(j == -1) { Address[i] = s1; Caption[i] = "None"; } else { Address[i] = s1.substring(0, j).trim(); Caption[i] = s1.substring(j + 1, s1.length()).trim(); } } } catch(IOException ioexception) { System.out.println("IO Error: " + ioexception.getMessage()); return; } AlbumReady = true; } //----------------------------------------------------- public String loadLine(BufferedReader bufferedreader) { String s = null; try { for(s = bufferedreader.readLine(); s != null && (s.length() == 0 || s.charAt(0) == ';'); s = bufferedreader.readLine()); } catch(IOException ioexception) { System.out.println("IO Error: " + ioexception.getMessage()); } return s; } //----------------------------------------------------- public void paintStatus(String s, String s1, Graphics g) { setBackground(Color.black); g.setColor(Color.black); g.fillRect(0, 0, getSize().width, getSize().height); Font font = new Font("Serif", 0, s_Font); FontMetrics fontmetrics = getFontMetrics(font); g.setFont(font); g.setColor(new Color(200, 200, 200)); g.drawString(s, getSize().width / 2 - fontmetrics.stringWidth(s) / 2, getSize().height / 2 - s_Font / 2); g.drawString(s1, getSize().width / 2 - fontmetrics.stringWidth(s1) / 2, getSize().height / 2 + s_Font / 2 + 2); } //----------------------------------------------------- void paintBorder(int i, int j, Graphics g) { g.setColor(c_Border); g.fillRect(i - 7, j - 7, Picture.getWidth(this) + 14, Picture.getHeight(this) + 14); } //----------------------------------------------------- void paintCorners(int i, int j, Graphics g) { g.setColor(c_Corners); int ai[] = { i - 7, i - 7, i + CORNER_SIZE }; int ai1[] = { j - 7, j + CORNER_SIZE, j - 7 }; g.fillPolygon(new Polygon(ai, ai1, ai.length)); ai[0] = i + Picture.getWidth(this) + 7; ai[1] = i + Picture.getWidth(this) + 7; ai[2] = (i + Picture.getWidth(this)) - CORNER_SIZE; g.fillPolygon(new Polygon(ai, ai1, ai.length)); ai1[0] = j + Picture.getHeight(this) + 7; ai1[1] = (j + Picture.getHeight(this)) - CORNER_SIZE; ai1[2] = j + Picture.getHeight(this) + 7; g.fillPolygon(new Polygon(ai, ai1, ai.length)); ai[0] = i - 7; ai[1] = i - 7; ai[2] = i + CORNER_SIZE; g.fillPolygon(new Polygon(ai, ai1, ai.length)); g.setColor(getForeground()); } //----------------------------------------------------- void paintButtons(Graphics g, boolean flag) { if(DefaultFont == null) return; if(buttonState == -1) { return; } else { b_Back.drawButton(g, flag); b_Forward.drawButton(g, flag); return; } } //----------------------------------------------------- void paintCaption(String s, Graphics g) { clickPicture = textClickPicture; int i; if((i = s.indexOf(124)) != -1) { clickPicture = s.substring(i + 1, s.length()).trim(); s = s.substring(0, i - 1).trim(); } if((i = clickPicture.indexOf(124)) != -1) clickPicture = clickPicture.substring(i + 1, clickPicture.length()).trim(); if(s.equalsIgnoreCase("none")) return; f_Default = new Font(DefaultFont, 0, s_Font); g.setFont(f_Default); g.setColor(c_Text); int j = -1; int l = 0; int i1 = 0; String as[] = new String[CaptionLines]; if(fm.stringWidth(s) > getSize().width - 20) { for(int j1 = 0; j1 < s.length(); j1++) { if(s.charAt(j1) == ' ') j = j1; if(fm.stringWidth(s.substring(l, j1)) > getSize().width - 20) { if(j == -1) j = j1; if(i1 < CaptionLines) { as[i1] = s.substring(l, j); i1++; } j1 = j; j = -1; l = j1; } } } if(l != s.length() - 1) { int k = s.length(); if(i1 < CaptionLines) { as[i1] = s.substring(l, k); i1++; } } imgY -= (i1 * (fm.getAscent() + 1)) / 2; imgM -= (i1 * (fm.getAscent() + 1)) / 2; int l1 = imgY + Picture.getHeight(this) + 7; for(int i2 = 0; i2 < i1; i2++) { int k1 = getSize().width / 2 - fm.stringWidth(as[i2]) / 2; l1 += fm.getAscent() + 1; g.drawString(as[i2], k1, l1); } } //----------------------------------------------------- void loadImage(int i) { if(!AlbumReady) return; if(ForgotAlbum) return; if(InvalidAlbum) return; if(i >= Total) i -= Total; if(i < 0) i = Total + i; IsValid = true; URL url = null; try { if(Address[i].indexOf("http://") == -1 && Address[i].indexOf("https://") == -1 && Address[i].indexOf("ftp://") == -1) { if(BasePath.equalsIgnoreCase("none")) url = new URL(getDocumentBase(), Address[i]); else url = new URL(BasePath + Address[i]); } else { url = new URL(Address[i]); } } catch(MalformedURLException _ex) { IsValid = false; } Picture = getImage(url); } //----------------------------------------------------- void bufferImages() { if(!ImageBuffering) return; if(!Registered) { ImageBuffering = false; return; } IsBuffering = true; for(int i = 0; i < Total; i++) { loadImage(i); os_Graphics.drawImage(Picture, 0, 0, this); } os_Graphics.setColor(c_Background); os_Graphics.fillRect(0, 0, getSize().width, getSize().height); IsBuffering = false; } void handleParameters() { textNext = getParameter("next"); textPrevious = getParameter("previous"); textOf = getParameter("of"); textIdle = getParameter("idle"); textClickNext = getParameter("clickNext"); textClickPrevious = getParameter("clickPrevious"); textClickPicture = getParameter("clickPicture"); textLoadingImage = getParameter("loadingImage"); textLoadingAlbum = getParameter("loadingAlbum"); textMoment = getParameter("moment"); if(textNext == null) textNext = "Next"; if(textPrevious == null) textPrevious = "Previous"; if(textOf == null) textOf = "of"; if(textIdle == null) textIdle = ""; if(textClickNext == null) textClickNext = "Next Picture"; if(textClickPrevious == null) textClickPrevious = "Previous Picture"; if(textClickPicture == null) textClickPicture = "Download Picture"; if(textLoadingImage == null) textLoadingImage = "Loading image ..."; if(textLoadingAlbum == null) textLoadingAlbum = "Loading album"; if(textMoment == null) textMoment = "One moment, please ..."; CaptionLines = 5; String s = getParameter("CaptionLines"); if(s != null) CaptionLines = Integer.valueOf(s).intValue(); ImageBuffering = true; s = getParameter("ImageBuffering"); if(s != null && s.equalsIgnoreCase("off")) ImageBuffering = false; defaultTarget = getParameter("Target"); if(defaultTarget == null) defaultTarget = "_self"; } } //////////////////////////////////////////////////////////////////////////// //////////////////////////////// // oButton.java // import java.applet.Applet; import java.awt.*; //-------------------------------------------------------------------------- public class oButton { public static int OBS_OFF = -1; public static int OBS_NORMAL; public static int OBS_OVERLAY = 1; public static int OBS_PRESSED = 2; int State; int defaultState; String Label; Color c_HiEdge; Color c_LoEdge; Color c_Label; Font f_Label; int X1; int Y1; int X2; int Y2; int x_Label; int y_Label; Applet Source; //---------------------------------------------------------------- public oButton(String s, int i, int j, int k, int l, Applet applet) { Source = applet; defaultState = OBS_NORMAL; State = OBS_NORMAL; c_Label = new Color(0, 0, 175); c_HiEdge = new Color(200, 200, 200); c_LoEdge = new Color(100, 100, 100); Label = s; setLabelFont(new Font("Serif", 0, 10)); setPosition(i, j, k, l); } //---------------------------------------------------------------- public void setPosition(int i, int j, int k, int l) { X1 = i; Y1 = j; X2 = k; Y2 = l; setLabelPos(); } //---------------------------------------------------------------- public void drawButton(Graphics g) { drawButton(g, true); } //---------------------------------------------------------------- public void drawButton(Graphics g, boolean flag) { g.setColor(c_Label); g.setFont(f_Label); if(State == OBS_OVERLAY) { if(flag) g.drawString(Label, x_Label, y_Label); g.setColor(c_HiEdge); g.drawLine(X1, Y1, X2, Y1); g.drawLine(X1, Y1, X1, Y2); g.setColor(c_LoEdge); g.drawLine(X2, Y2, X1, Y2); g.drawLine(X2, Y2, X2, Y1); return; } if(State == OBS_PRESSED) { if(flag) g.drawString(Label, x_Label + 1, y_Label + 1); g.setColor(c_LoEdge); g.drawLine(X1, Y1, X2, Y1); g.drawLine(X1, Y1, X1, Y2); g.setColor(c_HiEdge); g.drawLine(X2, Y2, X1, Y2); g.drawLine(X2, Y2, X2, Y1); return; } else { g.drawString(Label, x_Label, y_Label); return; } } //---------------------------------------------------------------- public void setLabelFont(Font font) { f_Label = font; setLabelPos(); } //---------------------------------------------------------------- public void setLabelColor(Color color) { c_Label = color; } //---------------------------------------------------------------- public boolean isPressed() { return State == OBS_PRESSED; } //---------------------------------------------------------------- public boolean isOver(int i, int j) { return i >= X1 && i <= X2 && j >= Y1 && j <= Y2 && defaultState != OBS_OFF; } //---------------------------------------------------------------- public boolean handleMouse(int i, int j, boolean flag) { if(defaultState == OBS_OFF) return false; if(defaultState == OBS_OVERLAY) return isOver(i, j); if(isOver(i, j)) { if(State != OBS_OVERLAY) { State = OBS_OVERLAY; return true; } if(flag && State != OBS_PRESSED) { State = OBS_PRESSED; return true; } else { return false; } } if(State == OBS_OVERLAY || State == OBS_PRESSED) { State = defaultState; return true; } else { return false; } } //---------------------------------------------------------------- void setLabelPos() { FontMetrics fontmetrics = Source.getFontMetrics(f_Label); int i = fontmetrics.stringWidth(Label); int j = fontmetrics.getHeight(); x_Label = X1 + ((X2 - X1) / 2 - i / 2); y_Label = Y1 + ((Y2 - Y1) / 2 + j / 4); x_Label++; y_Label++; } //---------------------------------------------------------------- public void setDefaultState(int i) { defaultState = i; State = defaultState; } } |