import java.awt.*; import java.util.Calendar; import java.util.Date; import java.util.Vector; import java.util.StringTokenizer; import java.awt.print.*; class ToendPrint extends Canvas implements Printable { public String departmentText, schoolHeaderText; public String nameText, surnameText, schoolText,gradeText, youText, usText, sourceText; public String addressText; public String birthDate; private Calendar nowDate; private String lText, nowText; private int myWidth; public ToendPrint() { setSize(524,733); } public void init() { myWidth = 453; youText = "Teie............................."; usText = "Meie............................."; sourceText = "Tõendi väljaandja nimi"; lText = "Käesolevaga tõendame, et " + nameText + " " + surnameText + " (sündinud "; lText = lText + birthDate; lText = lText + ") õpib " + schoolText + " " + gradeText + " klassis."; nowDate = Calendar.getInstance(); nowText = "" + nowDate.get(Calendar.DATE) + "/" + (nowDate.get(Calendar.MONTH)+1) + "/" + nowDate.get(Calendar.YEAR); repaint(); } public int print(Graphics g, PageFormat pf, int lnr) { if (lnr > 0) { return (Printable.NO_SUCH_PAGE); } else { drawScreen(g); } return (Printable.PAGE_EXISTS); } private Vector splitLines(String line, FontMetrics fm, int printWidth) { Vector v = new Vector(); Vector intern = new Vector(); StringTokenizer st = new StringTokenizer(line); int windex, r; String token, lastWord; while (st.hasMoreTokens()) { intern.addElement(st.nextToken()); } windex = 0; token = ""; lastWord = ""; while (intern.size() > 0) { while ( (fm.stringWidth(token + " " + lastWord) < printWidth) && (windex < intern.size()) ) { if (token == "") token = lastWord; else token = token + " " + lastWord; lastWord = (String)intern.elementAt(windex); windex++; } if (windex == intern.size()) token = token + " " + lastWord; v.addElement(token); token = ""; for (r = 0; r < windex; r++) intern.removeElementAt(0); windex = 0; } return (v); } private void drawScreen(Graphics g) { Font headerFont = new Font("Serif",Font.BOLD, 16); Font normalFont = new Font("Serif",Font.PLAIN, 12); FontMetrics fm; int startTop; Vector lineBuf; int r; int zeroPoint = 73; g.setColor(Color.white); g.fillRect(0,0,myWidth, getSize().height); g.setColor(Color.black); g.setFont(headerFont); fm = g.getFontMetrics(); startTop = fm.getHeight() * 4; g.drawString(departmentText, zeroPoint + (myWidth - zeroPoint/2 - fm.stringWidth(departmentText)) / 2, startTop); startTop = fm.getHeight() + startTop; g.drawString(schoolHeaderText, zeroPoint + (myWidth - zeroPoint/2 - fm.stringWidth(schoolHeaderText)) / 2, startTop); g.setFont(normalFont); fm = g.getFontMetrics(); startTop = startTop + fm.getHeight() * 3; g.drawString (youText, myWidth + zeroPoint - fm.stringWidth(youText), startTop); startTop = startTop + fm.getHeight() * 2; g.drawString (usText, myWidth + zeroPoint - fm.stringWidth(youText), startTop); lineBuf = splitLines(lText, fm, myWidth - 20); startTop = startTop + fm.getHeight() * 4; r = 0; while (r < lineBuf.size()) { g.drawString ((String)lineBuf.elementAt(r), zeroPoint , startTop + r * fm.getHeight() ); r++; } startTop = startTop + fm.getHeight() * 8; g.drawString (sourceText, zeroPoint, startTop); startTop = startTop + fm.getHeight() * 3; g.drawString ("Kuupäev " + nowText, zeroPoint, startTop); g.drawLine (zeroPoint,getSize().height-30, zeroPoint + myWidth, getSize().height-30); g.drawString (addressText, zeroPoint + (myWidth - fm.stringWidth(addressText)) / 2, getSize().height - 30 + fm.getHeight()); } public void doPrint() { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(this); try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } } public class ToendPrintFinal { public ToendPrintFinal() { ToendPrint tp = new ToendPrint(); tp.departmentText = "KAASIKU MAAKONNA HARIDUSOSAKOND"; tp.schoolHeaderText = "KALMKÜLA KOLMAS KESKKOOL"; tp.nameText = "Tauno-Kaldemar"; tp.surnameText = "Seinaväljajuga"; tp.schoolText = "Kalmküla 3. Keskkoolis"; tp.gradeText = "11."; tp.birthDate = ""; tp.addressText = "Vana-Siili 2, Kalmküla, 10224 Tel. +372 8244 423"; tp.init(); tp.doPrint(); } public static void main(String arg[]) { ToendPrintFinal tpf = new ToendPrintFinal(); } }