谁有java打印的源代码
要求必要的注释讲解相应的步骤网上乱拷来蒙人的不给分,我直接放到eclipse和netbeans下就能鉴别如果做成控件的补加分,awt/swing的补加分,web的酌情处理...
要求必要的注释
讲解相应的步骤
网上乱拷来蒙人的不给分,我直接放到eclipse和netbeans下就能鉴别
如果做成控件的补加分,awt/swing的补加分,web的酌情处理
我的分给的比较公道
1楼的程序我看了,还不错,有没有注释,
其他的地方我可以自己写,但是打印这里一头雾水 展开
讲解相应的步骤
网上乱拷来蒙人的不给分,我直接放到eclipse和netbeans下就能鉴别
如果做成控件的补加分,awt/swing的补加分,web的酌情处理
我的分给的比较公道
1楼的程序我看了,还不错,有没有注释,
其他的地方我可以自己写,但是打印这里一头雾水 展开
展开全部
//参考下吧
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.swing.*;
/**
This program demonstrates how to print 2D graphics
*/
public class PrintTest
{
public static void main(String[] args)
{
JFrame frame = new PrintTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
This frame shows a panel with 2D graphics and buttons
to print the graphics and to set up the page format.
*/
class PrintTestFrame extends JFrame
{
public PrintTestFrame()
{
setTitle("PrintTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
canvas = new PrintPanel();
add(canvas, BorderLayout.CENTER);
attributes = new HashPrintRequestAttributeSet();
JPanel buttonPanel = new JPanel();
JButton printButton = new JButton("Print");
buttonPanel.add(printButton);
printButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(canvas);
if (job.printDialog(attributes))
job.print(attributes);
}
catch (PrinterException e)
{
JOptionPane.showMessageDialog(PrintTestFrame.this, e);
}
}
});
JButton pageSetupButton = new JButton("Page setup");
buttonPanel.add(pageSetupButton);
pageSetupButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PrinterJob job = PrinterJob.getPrinterJob();
job.pageDialog(attributes);
}
});
add(buttonPanel, BorderLayout.NORTH);
}
private PrintPanel canvas;
private PrintRequestAttributeSet attributes;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
}
/**
This panel generates a 2D graphics image for screen display
and printing.
*/
class PrintPanel extends JPanel implements Printable
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawPage(g2);
}
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException
{
if (page >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.draw(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight()));
drawPage(g2);
return Printable.PAGE_EXISTS;
}
/**
This method draws the page both on the screen and the
printer graphics context.
@param g2 the graphics context
*/
public void drawPage(Graphics2D g2)
{
FontRenderContext context = g2.getFontRenderContext();
Font f = new Font("Serif", Font.PLAIN, 72);
GeneralPath clipShape = new GeneralPath();
TextLayout layout = new TextLayout("Hello", f, context);
AffineTransform transform = AffineTransform.getTranslateInstance(0, 72);
Shape outline = layout.getOutline(transform);
clipShape.append(outline, false);
layout = new TextLayout("World", f, context);
transform = AffineTransform.getTranslateInstance(0, 144);
outline = layout.getOutline(transform);
clipShape.append(outline, false);
g2.draw(clipShape);
g2.clip(clipShape);
final int NLINES =50;
Point2D p = new Point2D.Double(0, 0);
for (int i = 0; i < NLINES; i++)
{
double x = (2 * getWidth() * i) / NLINES;
double y = (2 * getHeight() * (NLINES - 1 - i))
/ NLINES;
Point2D q = new Point2D.Double(x, y);
g2.draw(new Line2D.Double(p, q));
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.swing.*;
/**
This program demonstrates how to print 2D graphics
*/
public class PrintTest
{
public static void main(String[] args)
{
JFrame frame = new PrintTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
This frame shows a panel with 2D graphics and buttons
to print the graphics and to set up the page format.
*/
class PrintTestFrame extends JFrame
{
public PrintTestFrame()
{
setTitle("PrintTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
canvas = new PrintPanel();
add(canvas, BorderLayout.CENTER);
attributes = new HashPrintRequestAttributeSet();
JPanel buttonPanel = new JPanel();
JButton printButton = new JButton("Print");
buttonPanel.add(printButton);
printButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(canvas);
if (job.printDialog(attributes))
job.print(attributes);
}
catch (PrinterException e)
{
JOptionPane.showMessageDialog(PrintTestFrame.this, e);
}
}
});
JButton pageSetupButton = new JButton("Page setup");
buttonPanel.add(pageSetupButton);
pageSetupButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PrinterJob job = PrinterJob.getPrinterJob();
job.pageDialog(attributes);
}
});
add(buttonPanel, BorderLayout.NORTH);
}
private PrintPanel canvas;
private PrintRequestAttributeSet attributes;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
}
/**
This panel generates a 2D graphics image for screen display
and printing.
*/
class PrintPanel extends JPanel implements Printable
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawPage(g2);
}
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException
{
if (page >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.draw(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight()));
drawPage(g2);
return Printable.PAGE_EXISTS;
}
/**
This method draws the page both on the screen and the
printer graphics context.
@param g2 the graphics context
*/
public void drawPage(Graphics2D g2)
{
FontRenderContext context = g2.getFontRenderContext();
Font f = new Font("Serif", Font.PLAIN, 72);
GeneralPath clipShape = new GeneralPath();
TextLayout layout = new TextLayout("Hello", f, context);
AffineTransform transform = AffineTransform.getTranslateInstance(0, 72);
Shape outline = layout.getOutline(transform);
clipShape.append(outline, false);
layout = new TextLayout("World", f, context);
transform = AffineTransform.getTranslateInstance(0, 144);
outline = layout.getOutline(transform);
clipShape.append(outline, false);
g2.draw(clipShape);
g2.clip(clipShape);
final int NLINES =50;
Point2D p = new Point2D.Double(0, 0);
for (int i = 0; i < NLINES; i++)
{
double x = (2 * getWidth() * i) / NLINES;
double y = (2 * getHeight() * (NLINES - 1 - i))
/ NLINES;
Point2D q = new Point2D.Double(x, y);
g2.draw(new Line2D.Double(p, q));
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询