package temp;import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.Color;
import java.awt.print.PrinterException;public class PrintPreviewCanvas extends JPanel
{
int m_iCurrentPage = 1;
printer m_jPrinter; //指向预览Applet中的m_jPrinter public PrintPreviewCanvas()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
} private void jbInit() throws Exception
{
this.setBackground(Color.white);
} public void SetPrinter(printer pf)
{
m_jPrinter = pf;
} public void SetCurrentPage(int iCurPage)
{
DrawOut jd = m_jPrinter.GetDrawOut();
if(jd == null)
iCurPage = 1;
else if(iCurPage >= jd.GetPageCount())
iCurPage = jd.GetPageCount();
if(iCurPage < 1)
iCurPage = 1;
m_iCurrentPage = iCurPage;
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(m_jPrinter == null)
return;
PageFormat pf;
DrawOut jd;
pf = m_jPrinter.GetPageFormat();
jd = m_jPrinter.GetDrawOut();
Graphics2D g2 = (Graphics2D)g;
double px = pf.getWidth(),
py = pf.getHeight(),
sx = getWidth(),
sy = getHeight(),
scale = sy / py,
xoff = 0.5 * (sx - scale * px),
yoff = 0;
g2.translate(xoff,yoff);
g2.scale(scale,scale);
Rectangle2D page = new Rectangle2D.Double(0,0,px,py);
g2.setPaint(Color.white);
g2.fill(page);
try
{
jd.print(g2,pf,m_iCurrentPage);
}
catch(PrinterException ex)
{
}
} public int filpPage(int by)
{
int newPage = m_iCurrentPage + by;
if (newPage > 0 && newPage <= GetPageCount())
{
m_iCurrentPage = newPage;
repaint();
}
return m_iCurrentPage;
} public int GetPageCount()
{
if(m_jPrinter == null)
return 0;
if(m_jPrinter.GetDrawOut() == null)
return 0;
return m_jPrinter.GetDrawOut().GetPageCount();
}
}
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.Color;
import java.awt.print.PrinterException;public class PrintPreviewCanvas extends JPanel
{
int m_iCurrentPage = 1;
printer m_jPrinter; //指向预览Applet中的m_jPrinter public PrintPreviewCanvas()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
} private void jbInit() throws Exception
{
this.setBackground(Color.white);
} public void SetPrinter(printer pf)
{
m_jPrinter = pf;
} public void SetCurrentPage(int iCurPage)
{
DrawOut jd = m_jPrinter.GetDrawOut();
if(jd == null)
iCurPage = 1;
else if(iCurPage >= jd.GetPageCount())
iCurPage = jd.GetPageCount();
if(iCurPage < 1)
iCurPage = 1;
m_iCurrentPage = iCurPage;
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(m_jPrinter == null)
return;
PageFormat pf;
DrawOut jd;
pf = m_jPrinter.GetPageFormat();
jd = m_jPrinter.GetDrawOut();
Graphics2D g2 = (Graphics2D)g;
double px = pf.getWidth(),
py = pf.getHeight(),
sx = getWidth(),
sy = getHeight(),
scale = sy / py,
xoff = 0.5 * (sx - scale * px),
yoff = 0;
g2.translate(xoff,yoff);
g2.scale(scale,scale);
Rectangle2D page = new Rectangle2D.Double(0,0,px,py);
g2.setPaint(Color.white);
g2.fill(page);
try
{
jd.print(g2,pf,m_iCurrentPage);
}
catch(PrinterException ex)
{
}
} public int filpPage(int by)
{
int newPage = m_iCurrentPage + by;
if (newPage > 0 && newPage <= GetPageCount())
{
m_iCurrentPage = newPage;
repaint();
}
return m_iCurrentPage;
} public int GetPageCount()
{
if(m_jPrinter == null)
return 0;
if(m_jPrinter.GetDrawOut() == null)
return 0;
return m_jPrinter.GetDrawOut().GetPageCount();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货