⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shelf.java

📁 这个出色的小程序可以把你的网站上的链接
💻 JAVA
字号:

package ru.sakva.bsh;

 import java.applet.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;
 import java.net.*;
 import java.io.*;
 import java.util.*;

/**
 Shelf is Java applet for presentation of different data
 (file, weblinks, etc.) as books on bookshelves.
*/

 public class Shelf extends Applet
   implements MouseListener,MouseMotionListener,ComponentListener
/* * */ { /* * */
 static protected final int shelfH=6;
 static protected Hashtable icons = new Hashtable();

/**  Color of walls */
 public Color shelfColor = SystemColor.window;

/**  Color of shelves */
 public Color plainColor = SystemColor.control;

 protected Book taken;
 protected int takenNum;
 int border=0;

 protected Book[] books;
 boolean vertical=false;
 Image im=null;
 int imU,imV,imW,imH;
 int msU,msV;
 int waitTime=1000;

 int[] p = new int[7];
 int[] q = new int[7];

/** Applet init */
 public void init()
{
 addComponentListener(this);
 }

/**  Allocate array for books */
 public void reserve(int n)
{
 books = new Book[n]; int i=0; while(i < n)books[i++]=new Book();
 }

/**  Get params from input or HTML */
 public void getParams()
{
 int i;
 int n=0;
 String par,s;
 MediaTracker mtr = new MediaTracker(this);

// Get common and default parameters if available 
 if((par=getParameter("wait"))!=null)waitTime=Integer.parseInt(par);
 if((par=getParameter("border"))!=null)border=Integer.parseInt(par);
 if((par=getParameter("putBooks"))!=null)
   { if(par.startsWith("vert"))vertical=true; }
 if((par=getParameter("wall.color"))!=null)
   shelfColor=new Color(Integer.parseInt(par,16));
 if((par=getParameter("shelf.color"))!=null)
   plainColor=new Color(Integer.parseInt(par,16));
 if((par=getParameter("cover.color"))!=null)
   Book.defaultCoverColor=new Color(Integer.parseInt(par,16));
 if((par=getParameter("title.color"))!=null)
   Book.defaultTitleColor=new Color(Integer.parseInt(par,16));
 if((par=getParameter("book.min"))!=null) Book.hmin=Integer.parseInt(par);
 if((par=getParameter("book.max"))!=null) Book.hmax=Integer.parseInt(par);
 if((par=getParameter("book.volume"))!=null)Book.volume=Integer.parseInt(par);
 if((par=getParameter("book.size"))!=null)
   {
    Book.height=(Integer.parseInt(par));
    Book.width=Book.height/4;
    Book.thickness=Book.height/8;
    }
 if((par=getParameter("book.thickness"))!=null)
   { Book.thickness=(Integer.parseInt(par)); }

// Get parameters for each book 
 if((par=getParameter("books.number"))!=null)
   {
    n=(Integer.parseInt(par)); reserve(n);
    i=0; while(i < n)
      {
       s="$"+String.valueOf(i+1);
       if((par=getParameter(s+".title"))!=null)books[i].setTitle(par);
       if((par=getParameter(s+".ref"))!=null)books[i].setReference(par);
       if((par=getParameter(s+".volume"))!=null)
         books[i].setVolume(Integer.parseInt(par,16));
       if((par=getParameter(s+".icon"))!=null)
         {
          try  // if book has picture try to get it
            {  // is picture in hashtable also? 
             Image img1 = (Image)icons.get(par);
             if(img1 == null)  // if not read picture 
               {
                InputStream ins = 
                  Shelf.class.getResourceAsStream("/"+par);
                Image src=null;
                if(ins != null)
                  {
                   int l = ins.available();
                   byte[] b = new byte[l]; ins.read(b); ins.close();
                   src = getToolkit().createImage(b,0,l);
                   }
                else src = getImage(new URL(getDocumentBase(),par));
                if(src != null)
                  {
                   if(vertical == false)  //  if books are horisontal
                     {                    //  rotate picture
                      Rot90 imf = new Rot90();
                      imf.clockwise=false;
                      ImageProducer imp = new FilteredImageSource
                        (src.getSource(),imf);
                      img1 = createImage(imp);
                      }
                   else img1=src;
                   mtr.addImage(img1,i,-1,books[i].c);
                   icons.put(par,img1);  // put picture in hashtable
                   }
                }
             books[i].setIcon(img1);  
             }
          catch(Exception e){ System.out.println(e); }
          }
       if((par=getParameter(s+".title.color"))!=null)
          books[i].titleColor=new Color(Integer.parseInt(par,16));
       if((par=getParameter(s+".cover.color"))!=null)
          books[i].coverColor=new Color(Integer.parseInt(par,16));
       i++;
       }
    }
//  Wait for all pictures
 try { mtr.waitForAll(waitTime); }catch(Exception ex){ }

 if(vertical && books != null)
   {  // some tricks to make vertical text
    Book book;
    Image ic,img2;
    Graphics g;
    int w;
    i=0; while(i < books.length)
      {
       if((book = books[i])!=null)
         {
             img2 = createImage(book.c,book.b);
             g = img2.getGraphics();
             g.setColor(book.coverColor);
             g.fillRect(book.u,book.v,book.c,book.b);
             ic = book.icon; w=0;
             if(ic != null)
               {
                w = (int)((float)ic.getHeight(this)
                   /(float)ic.getWidth(this)*book.c);
                g.drawImage(ic,0,0,book.c,-1,this);
                }
             Image img3 = createImage(book.b-w,book.c);
             Graphics gr = img3.getGraphics();
             gr.setColor(Color.black); gr.fillRect(0,0,book.b-w,book.c);
             gr.setColor(Color.white); book.drawTitle(gr,0,0,w);
             Rot90 imf = new Rot90();
             imf.asIs=false;
             imf.front=book.titleColor.getRGB();
             imf.back=0x00000000;
             ImageProducer imp = new FilteredImageSource(img3.getSource(),imf);
             img3 = createImage(imp);
             g.drawImage(img3,0,w,-1,-1,this);
             book.icon = img2;
          }
       i++;
       }
    }
 }

/** Creates offscreen image */
 public void createOffScreen()
{
 Dimension sz = getSize();
 imW=sz.width-2*border; imH=sz.height-2*border;
 imU=imV=0;
 im=createImage(imW,imH);
 }

/** Dedraw books and shelves */
 public void refresh()
{
 if(im==null)createOffScreen();
 if(im != null)draw(im.getGraphics());
 repaint();
 }

/**
 Draw one shelf 
 @param ig  -  Graphics to draw on
 @param v   -  thickness (height) odf shelf
*/
 public void drawShelf(Graphics ig,int v)
{
 int l = Book.width;
 p[0]=0; p[1]=imW-l-3; p[2]=p[3]=imW-1; p[4]=l+2; p[5]=0;
 q[0]=q[1]=v+shelfH; q[2]=q[1]-l-2; q[3]=q[2]-shelfH;
 q[4]=q[3]; q[5]=v;
 ig.setColor(plainColor); ig.fillPolygon(p,q,6);
 ig.setColor(Color.black);
 ig.drawPolygon(p,q,6);

 ig.drawLine(p[0],v,p[1],v);
 ig.drawLine(p[1],v,p[2],q[3]);
 ig.drawLine(p[1],q[1],p[1],v);
 ig.drawLine(p[4],q[4],p[0],v);
 }

/** Draw shelves and books */
 public void draw(Graphics ig)
{
 int u,v,i,j,k,d;
 Book book;
 int[] top = new int[32];  int nt=0;

 int l = Book.width;
 ig.setColor(shelfColor); ig.fillRect(0,0,imW,imH);
 ig.setColor(Color.black);
// ig.drawLine(l,0,l,imH);

 if(books != null)
   {
    if(vertical)
      {
       u=2; v=imH-Book.height-shelfH*2; d=shelfH+Book.height+6;
       top[nt++]=0;
       k=0; j=0; while(j < books.length)
         {
          if((book=books[j]) != null)
            {
             if(u+book.a+book.c+2 > imW)
               {
                top[nt++]=j;
                i=0; while(i < j){ if(books[i]!=null)books[i].v-=d; i++; }
                u=2;
                }
             book.u=u+2; book.v=v-2;
             u+=book.c+2;
             if(book.selected){ book.u-=4; book.v+=4; }
             }
          j++;
          }

       v=imH-shelfH*2;
       k=books.length; j=nt; while(--j >= 0)
         {
          drawShelf(ig,v);
          i=top[j]; while(i < k)
            { if(books[i] != null)books[i].drawVertical(ig); i++; }
          k=top[j];
          v-=d;
          }
       }
    else
      {
       u=2; v=Book.width;
       k=0; j=0; while(j < books.length)
         {
          if((book=books[j]) != null)
            {
             v+=book.c+1;
             if(v > imH-2*shelfH)
               {
                top[nt++]=j;
                d=(imH-2*shelfH)-(v-book.c-1);
                i=k; while(i < j){ if(books[i] != null)books[i].v+=d; i++; }
                k=j; v=Book.width+book.c+1; u+=Book.height+6;
                }
             book.u=u+2; book.v=v-book.c-3;
             if(book.selected){ book.u-=4; book.v+=4; }
             }
          j++;
          }
       top[nt++]=j;
       d=imH-2*shelfH-v;
       i=k; while(i < j){ if(books[i]!=null)books[i].v+=d; i++; }

       drawShelf(ig,imH-shelfH*2);
       k=0; j=0; while(j < nt)
         {
          i=top[j]; while(--i >= k)
            if(books[i] != null)books[i].drawHorisontal(ig);
          k=top[j++];
          }
       }
    }
 }

// Draw border around Applet
 void drawBorder(Graphics g)
{
 int w = imW+2*border; int h = imH+2*border;
 int h1=h-1; int h4=h-border; int w1=w-1; int w4=w-border;
 g.setColor(shelfColor);
 g.fill3DRect(0,0,w,border,true);
 g.fill3DRect(0,h4,w,border,true);
 g.fill3DRect(0,border,border,h4-border,true);
 g.fill3DRect(w4,border,border,h4-border,true);
 g.clipRect(border+1,border+1,w-2*border-1,h-2*border-1);
 }

// update (from Component)
 public void update(Graphics g)
{
 if(im != null)
   {
    Dimension sz = getSize();
    g.clearRect
      (imU+sz.width+border,border,sz.width-imU-2*border,sz.height-2*border);
    g.drawImage(im,imU+border,imV+border,this);
    if(taken != null)
      {
       if(vertical)taken.drawVertical(g);
       else        taken.drawHorisontal(g);
       }
    }
 if(border != 0)drawBorder(g);
 }

// paint (from Component)
 public void paint(Graphics g)
{
 update(g);
 }

/** Returns index of book with coordinatex (u,v) */
 public int getBookIndex(int u,int v)
{
 int i=-1;
 Book book;
 int u1,u2,v1,v2;

 if(books != null)
   {
    i=0; while(i < books.length)
      {
       if((book=books[i])!=null)
         {
          u1=book.u+border; v1=book.v+border;
          u2 = u1+(vertical?book.c:book.b);
          v2 = v1+(vertical?book.b:book.c);
          if(u1 < u && u < u2 && v1 < v && v < v2)break;
          }
       i++;
       }
    }
 return i;
 }

 static ColorModel dcm = new DirectColorModel(24,0xff0000,0xff00,0xff);

/************ ImageObserver implementation */

 public boolean imageUpdate(Image img,int s,int x,int y,int w,int h)
{
 if( (s & ALLBITS) == ALLBITS)
   {
    return false;
    }
 else if( (s & (ABORT|ERROR))!=0)
   {
    return false;
    }
 else return true;
 }

/************ MouseListener implementation */

 public void mousePressed(MouseEvent me)
{
 msU=me.getX(); msV=me.getY();
 }

 public void mouseReleased(MouseEvent me)
{
 }

 public void mouseEntered(MouseEvent me)
{
 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 }

 public void mouseExited(MouseEvent me)
{
 setCursor(Cursor.getDefaultCursor());
 }

 public void mouseClicked(MouseEvent me){ }

 public void mouseMoved(MouseEvent me){ }

 public void mouseDragged(MouseEvent me)
{
 int u = me.getX();
 int du = u-msU; msU=u;
 if(du != 0)
   {
    u=imU+du;
    if(16-getSize().width < u && u <= 0){ imU = u; repaint(); }
    }
 }

/************ ComponentListener implementation */

 public void componentHidden(ComponentEvent ce){  }
 public void componentMoved(ComponentEvent ce){  }
 public void componentResized(ComponentEvent ce){  }

 public void componentShown(ComponentEvent ce)
{
 if(im == null){ createOffScreen(); refresh(); }
 }

/* * */ } /* * */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -