/* *Zine Zine Zine Zine Zine Zine Zine * a demo program. * Implements popups for comic book style messages * on top of an image. * uses a crummy form of markup language * designed only to be easy to parse with java * (read quick n dirty) *Zine (more...) * an app by Johan van der Hoeven. * johan@rosebud.com * http://www.rosebud.com/rb/rbhome.html * johan_van_der_Hoeven@terc.edu * * can be viewed at: * http://http://www.fanzine.se/java/jo/zine.html (zine stuff) * http://teaparty.terc.edu/java/z/glob.html (network science education research) */ import java.awt.*; import java.io.StreamTokenizer; import java.io.InputStream; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.net.MalformedURLException; import java.util.Vector; import java.util.Enumeration; class Rect { public int lX=0; public int tY=0; public int rX=0; public int bY=0; public Rect(int leftX, int topY, int rightX, int bottomY) {lX=leftX; tY=topY; rX=rightX; bY=bottomY;} public Rect(){}; public boolean isPointInRect(int x, int y) { if(xlX&&y>tY&& yboundR.rX) { //do a horizontal left shift int tmp= Math.min(textR.width(),xa); textR.offset(-tmp,0); } else if(textR.lXboundR.bY) { //do a vertical down shift int tmp= Math.min(textR.height(),yb); textR.offset(0,-tmp); } }; public void Draw(Graphics g) { g.setFont(font); g.setColor(Color.lightGray); g.fill3DRect(textR.lX, textR.tY, textR.width(), textR.height(),true); g.setColor(Color.black); int y=textR.tY; for (Enumeration e = xfrm.strvec.elements(); e.hasMoreElements();) { y+=lineHi; g.drawString((String) e.nextElement() , textR.lX+charWid, y); }; }; } public class Zine extends java.applet.Applet { protected String dana = null; protected String imgna = null; protected String shot=null; protected Image daimg; Rect clipR; Vector framevect; boolean showhot=false; zineFrame cur_znfrm=null; zinePopUp pop=null; Font font; public void init() { font = new java.awt.Font("TimesRoman", Font.PLAIN, 12); framevect= new Vector(); //resize(200, 200); dana = getParameter("dataurl"); getFrames(); imgna = getParameter("imgurl"); daimg= getImage(getCodeBase(), imgna); //if(daimg!=null) //resize(daimg.getWidth(this),daimg.getHeight(this)); //else //resize(10, 10); shot = getParameter("showhot"); if(shot!=null) if(shot.equals("yes")) showhot=true; } public void paint(Graphics g) { g.drawImage(daimg, 0, 0, this); if(showhot) { for ( Enumeration ee = framevect.elements(); ee.hasMoreElements();) { zineFrame fr= (zineFrame)ee.nextElement(); g.draw3DRect(fr.r.lX, fr.r.tY, fr.r.width(), fr.r.height(),true); } } if(pop!=null) { pop.Draw(g); } g.draw3DRect(0, 0, size().width-1, size().height-1, true); } /* end paint */ public void update(Graphics g) { // Clip to the affected area //make sure not outside applet: //(needed else it can paint outside applet --java bug ?) //if (clipR != null) { //g.clipRect(clipR.lX, clipR.tY, clipR.width(), clipR.height()); //} paint(g); } public boolean keyDown(java.awt.Event evt, int k) { System.out.println("key"); if(k=='s'||k=='S') { showhot=!showhot; clipR.lX=0; clipR.rX=size().width; clipR.tY=0; clipR.bY=size().height; repaint(); } return true; } public boolean mouseEnter(java.awt.Event evt) { requestFocus(); return true; } public boolean mouseExit(java.awt.Event evt) { if(pop!=null) { cur_znfrm=null; clipR=pop.clipR; pop=null; repaint(); }; return true; } public boolean mouseMove(java.awt.Event evt, int x, int y) { boolean change=false; if(cur_znfrm==null) { for ( Enumeration ee = framevect.elements(); ee.hasMoreElements();) { zineFrame fr= (zineFrame)ee.nextElement(); if(fr.r.isPointInRect(x,y)) { change=true; cur_znfrm=fr; pop = new zinePopUp(font, getFontMetrics(font)); Rect r=new Rect(0,0,size().width,size().height); pop.doLayout(r,cur_znfrm); clipR=pop.clipR; }; }; } else { if(!cur_znfrm.r.isPointInRect(x,y)) { cur_znfrm=null; clipR=pop.clipR; pop=null; change=true; }; }; if(change) { repaint(); }; return true; } protected void getFrames() { zineFrame zfrm=null; //System.out.println("zine get frames"); InputStream is=null; try { try { is = new URL(getDocumentBase(), dana).openStream(); StreamTokenizer st = new StreamTokenizer(is); st.eolIsSignificant(false); st.commentChar('#'); /* parse file --NOT very clean can be improved alot */ int i=0; while (st.ttype != StreamTokenizer.TT_EOF) { st.nextToken(); /* got a new frame: */ if(st.ttype == '{') { zfrm= new zineFrame(); i=0; continue; }; if(st.ttype == '}') {framevect.addElement(zfrm); continue; } if(st.ttype==st.TT_NUMBER) { int n = (int) st.nval; switch (i) { case 0: zfrm.p.x=n; break; case 1: zfrm.p.y=n; break; case 2: zfrm.r.lX=n; break; case 3: zfrm.r.tY=n; break; case 4: zfrm.r.rX=n; break; case 5: zfrm.r.bY=n; break; }; i++; continue; }; if(st.ttype=='"') { String tmp= st.sval; zfrm.strvec.addElement(tmp); continue; }; }; /* while */ } finally { if (is != null) { is.close(); } } } catch (MalformedURLException e) { } catch (IOException e) { } }; /* end getFrames() */ } /* end zine class */