public void paint(java.awt.Graphics g) {

      int w = m_bi.getWidth(this);

      int h = m_bi.getHeight(this);

      // int ww= (int)(w*(percentSize/100));

      // int hh=(int)(h*(percentSize/100));

      g.setColor(java.awt.Color.white);

      g.fillRect(0, 0, this.getWidth(), this.getHeight());

      java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;

      g2d.scale(percentSize / 100.0, percentSize / 100.0);

      int ml = (this.getWidth() / 2) - (w / 2);

      int mh = (this.getHeight() / 2) - (h / 2);

      if (m_bi != null) {
        g2d.drawImage(m_bi, ml, mh, this);
      }
    }
    public int print(java.awt.Graphics pg, java.awt.print.PageFormat pageFormat, int pageIndex)
        throws java.awt.print.PrinterException {

      if (pageIndex >= m_maxNumPage || m_bi == null) {
        return NO_SUCH_PAGE;
      }

      pg.translate((int) pageFormat.getImageableX() + 1, (int) pageFormat.getImageableY() + 1);

      int wPage = (int) pageFormat.getImageableWidth() + 1;

      int hPage = (int) pageFormat.getImageableHeight() + 1;

      double scale = percentSize / 100.0;

      wPage = (int) (wPage / scale);

      hPage = (int) (hPage / scale);

      /*

      int wPage = (int)pageFormat.getImageableWidth();

      int hPage = (int)pageFormat.getImageableHeight();



      int w = m_bi.getWidth(this);

      int h = m_bi.getHeight(this);

      if (w == 0 || h == 0)

      return NO_SUCH_PAGE;

      int nCol = Math.max((int)Math.ceil((double)w/wPage), 1);

      int nRow = Math.max((int)Math.ceil((double)h/hPage), 1);

      m_maxNumPage = nCol*nRow;



      int iCol = pageIndex % nCol;

      int iRow = pageIndex / nCol;

      int x = iCol*wPage;

      int y = iRow*hPage;

      int wImage = Math.min(wPage, w-x);

      int hImage = Math.min(hPage, h-y);

      */

      int w = m_bi.getWidth(this);

      int h = m_bi.getHeight(this);

      // int resizedw = (int)(w*(percentSize/100.0));

      // int resizedh = (int)(h*(percentSize/100.0));

      java.awt.Graphics2D g2d = (java.awt.Graphics2D) pg;

      g2d.scale(percentSize / 100.0, percentSize / 100.0);

      if (w == 0 || h == 0) {
        return NO_SUCH_PAGE;
      }

      int nCol = Math.max((int) Math.ceil((double) w / wPage), 1);

      int nRow = Math.max((int) Math.ceil((double) h / hPage), 1);

      m_maxNumPage = nCol * nRow;

      int iCol = pageIndex % nCol;

      int iRow = pageIndex / nCol;

      int x = iCol * wPage;

      int y = iRow * hPage;

      int wImage = Math.min(wPage, w - x);

      int hImage = Math.min(hPage, h - y);

      g2d.drawImage(m_bi, 0, 0, wImage, hImage, x, y, x + wImage, y + hImage, this);

      // pg.drawImage(m_bi, 0, 0, 40, 40,  x, y, x+wImage, y+hImage, this);

      System.gc();

      return PAGE_EXISTS;
    }
    public ImagePanel(Image im) {

      m_bi = (java.awt.image.BufferedImage) im;

      setPreferredSize(new java.awt.Dimension(m_bi.getTileWidth(), m_bi.getHeight()));
    }