import java.awt.*; import java.awt.event.*; import java.awt.print.*; class Kiri2 extends Canvas implements Printable { public void paint(Graphics g) { g.setColor(Color.black); int W = (int)getSize().getWidth(); int H = (int)getSize().getHeight(); g.drawRect(1, 1, W-3, H-3); g.drawString("Tere!", W/2, H/2); } public int print(Graphics g, PageFormat pf, int lk) throws PrinterException { if (lk >= 2) { return Printable.NO_SUCH_PAGE; } g.translate((int)pf.getImageableX(), (int)pf.getImageableY()); g.setColor(Color.black); paint(g); g.drawString("lk nr. "+(lk+1), 100, (int)pf.getImageableHeight()-50); return Printable.PAGE_EXISTS; } } public class Trykk2 extends Panel implements ActionListener { Kiri2 kiri=new Kiri2(); Button b = new Button("Tryki"); public Trykk2() { b.addActionListener(this); add(b); kiri.setSize(100, 50); add(kiri); } public void actionPerformed(ActionEvent e) { PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(kiri); try { if(pj.printDialog()) pj.print(); } catch (Exception PrintException) { } } public static void main(String s[]) { Frame f = new Frame("Trykkimisraam"); f.add("Center", new Trykk2()); f.pack(); f.setSize(400,300); f.show(); } }