?? tablecliper.java
字號:
//scale the Graphics newGraphics2D.scale((double)(tableScale)/100.0,(double)(tableScale)/100.0); //if print head,we should print head and table //otherwise,we just need to pring the table if(displayHeaderOnPage(positionY)) { //if need to print a table head JTableHeader header = table.getTableHeader(); if((header.getHeight() == 0) || (header.getWidth() == 0)) header.setSize(header.getPreferredSize()); //get the height of the header //after print the head,the offsetY need to change into (offsetY + headerHeight) int headerHeight = header.getHeight(); //show the offset as the real size,so multiply the table scale with offset newGraphics.translate((int)(offsetX * 100.0 / tableScale) - positionX,(int)(offsetY * 100.0 / tableScale)); newGraphics.clipRect(positionX,0,size.width,size.height + headerHeight); header.paint(newGraphics); //draw v(|) line with table head newGraphics.setColor(Color.gray); newGraphics.drawLine(positionX,0,positionX,headerHeight); //draw h(-) line with table head newGraphics.setColor(Color.gray); newGraphics.drawLine(positionX,0,positionX + size.width,0); //draw the table newGraphics.translate(0,headerHeight - positionY); newGraphics.clipRect(positionX,positionY,size.width,size.height); table.paint(newGraphics); //draw h-line with table head newGraphics.setColor(Color.gray); newGraphics.drawLine(positionX,positionY,positionX,size.height + positionY); //restore the translate newGraphics.translate(0,positionY - headerHeight); newGraphics.translate((int)(positionX - offsetX * (double)(100.0 / tableScale)),(int)(-offsetY *(double)(100.0 / tableScale))); } //print table without head else { //show the offset as the real size,so multiply the table scale with offset newGraphics.translate((int)(offsetX * 100.0 / tableScale - positionX),(int)(offsetY * 100.0 / tableScale - positionY)); //as i said,size has been measured with table scale,so there needn't newGraphics.clipRect(positionX,positionY,size.width,size.height); table.paint(newGraphics); //draw v-line without table head newGraphics.setColor(Color.gray); newGraphics.drawLine(positionX,positionY,positionX + size.width,positionY); //draw h-line without table head newGraphics.setColor(Color.gray); newGraphics.drawLine(positionX,positionY,positionX,size.height + positionY); //restore the translate newGraphics.translate((int)(positionX - offsetX *(double)(100.0 / tableScale)),(int)(positionY - offsetY *(double)(100.0 / tableScale))); } //restore the scale newGraphics2D.scale((double)(100.0 / tableScale),(double)(100.0/tableScale)); //restore the translate if(tableAlignment == 1) newGraphics.translate((int)(((double)(size.width * tableScale / 100) - pageFormat.getImageableWidth())/2),0); else if(tableAlignment == 2) newGraphics.translate((int)((double)(size.width * tableScale / 100) - pageFormat.getImageableWidth()),0); //restore the clip newGraphics.setClip(clipRect); //translate for the main paper newGraphics.translate(0,paperHeight); allOffsetX += 0; allOffsetY += paperHeight; //if foot is exist,show it on the paper if(showFoot) { int footWidth = (int)foot.getWidth(); int footHeight = (int)foot.getHeight(); int footImageableX = (int)foot.getImageableX(); int footImageableY = (int)foot.getImageableY(); int footImageableWidth = (int)foot.getImageableWidth(); int footImageableHeight = (int)foot.getImageableHeight(); newGraphics.setColor(Color.white); newGraphics.fillRect(0,0,footWidth,footHeight); //print the border of the foot if(foot.getBorderType() == 0) { newGraphics.setColor(Color.black); newGraphics.drawLine(footImageableX,footImageableY, footImageableWidth + footImageableX,footImageableY); } else if(foot.getBorderType() == 1) { newGraphics.setColor(Color.black); newGraphics.drawRect(footImageableX,footImageableY, footImageableWidth,footImageableHeight); } //print string of foot StringBuffer content; StringBuffer bakContent; Date localDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); String localNowDate = sdf.format(localDate); if(foot.leftContent != "") { content = new StringBuffer(foot.leftContent); bakContent = new StringBuffer(foot.leftContent); while( foot.leftContent.indexOf('@') != -1 && foot.leftContent.indexOf('@') != foot.leftContent.length() - 1) { int flag = foot.leftContent.indexOf('@'); switch(foot.leftContent.charAt(flag + 1)) { case 'd': content.replace(flag,flag + 2,localNowDate); foot.leftContent = content.toString(); break; case 'p': content.replace(flag,flag + 2, "" + this.getCurrentNumberOfPages(positionX,positionY)); foot.leftContent = content.toString(); break; case 't': content.replace(flag,flag + 2, "" + this.getNumberOfPages()); foot.leftContent = content.toString(); break; default: content.replace(flag,flag + 1, "#$"); foot.leftContent = content.toString(); break; } } for(int index = 0;index < content.length() - 1;index++) if(content.charAt(index) == '#' && content.charAt(index + 1) == '$') content.replace(index,index + 2,"@"); foot.leftContent = bakContent.toString(); newGraphics.setColor(Color.black); newGraphics.drawString(content.toString(),footImageableX + 10,footImageableY + (footImageableHeight / 2 + 6)); } if(foot.midContent != "") { content = new StringBuffer(foot.midContent); bakContent = new StringBuffer(foot.midContent); while( foot.midContent.indexOf('@') != -1 && foot.midContent.indexOf('@') != foot.midContent.length() - 1) { int flag = foot.midContent.indexOf('@'); switch(foot.midContent.charAt(flag + 1)) { case 'd': content.replace(flag,flag + 2,localNowDate); foot.midContent = content.toString(); break; case 'p': content.replace(flag,flag + 2, "" + this.getCurrentNumberOfPages(positionX,positionY)); foot.midContent = content.toString(); break; case 't': content.replace(flag,flag + 2, "" + this.getNumberOfPages()); foot.midContent = content.toString(); break; default: content.replace(flag,flag + 1, "#$"); foot.midContent = content.toString(); break; } } for(int index = 0;index < content.length() - 1;index++) if(content.charAt(index) == '#' && content.charAt(index + 1) == '$') content.replace(index,index + 2,"@"); foot.midContent = bakContent.toString(); newGraphics.setColor(Color.black); newGraphics.drawString(content.toString(),footImageableX + (footImageableWidth / 2) - content.toString().length() * 4,footImageableY + (footImageableHeight / 2 + 6)); } if(foot.rightContent != "") { content = new StringBuffer(foot.rightContent); bakContent = new StringBuffer(foot.rightContent); while( foot.rightContent.indexOf('@') != -1 && foot.rightContent.indexOf('@') != foot.rightContent.length() - 1) { int flag = foot.rightContent.indexOf('@'); switch(foot.rightContent.charAt(flag + 1)) { case 'd': content.replace(flag,flag + 2,localNowDate); foot.rightContent = content.toString(); break; case 'p': content.replace(flag,flag + 2, "" + this.getCurrentNumberOfPages(positionX,positionY)); foot.rightContent = content.toString(); break; case 't': content.replace(flag,flag + 2, "" + this.getNumberOfPages()); foot.rightContent = content.toString(); break; default: content.replace(flag,flag + 1, "#$"); foot.rightContent = content.toString(); break; } } for(int index = 0;index < content.length() - 1;index++) if(content.charAt(index) == '#' && content.charAt(index + 1) == '$') content.replace(index,index + 2,"@"); foot.rightContent = bakContent.toString(); newGraphics.setColor(Color.black); newGraphics.drawString(content.toString(),footImageableX + footImageableWidth - content.toString().length() * 8,footImageableY + (footImageableHeight / 2 + 6)); } newGraphics.translate(0,footHeight); allOffsetX += 0; allOffsetY += footHeight; } //restore all offset newGraphics.translate(-allOffsetX,-allOffsetY); } //decide whether display the header or not protected boolean displayHeaderOnPage(int positionY) { if((headerStatus == 0) || ((headerStatus == 1) && positionY == 0)) return true; else return false; } //get the total page numbers public int getNumberOfPages() { Dimension size = null; int tableWidth = table.getWidth(); int tableHeight = table.getHeight(); int positionX = 0; int positionY = 0; int pageIndex = 0; while(positionY < tableHeight) { positionX = 0; while(positionX < tableWidth) { size = getPrintSize(positionX,positionY); positionX += size.width; pageIndex++; } positionY += size.height; } return pageIndex; } //get current page number public int getCurrentNumberOfPages(int currentPositionX,int currentPositionY) { Dimension size = null; int tableWidth = table.getWidth(); int tableHeight = table.getHeight(); int positionX = 0; int positionY = 0; int pageIndex = 0; while(positionY < tableHeight) { positionX = 0; while(positionX < tableWidth) { size = getPrintSize(positionX,positionY); pageIndex++; System.out.println(currentPositionX); System.out.println(currentPositionY); if((currentPositionX == positionX) && (currentPositionY == positionY)) return pageIndex; positionX += size.width; } positionY += size.height; } return 0; } //get the printable(the canvas) public Printable getPrintable(int index) { return this; } //get the pageFormat(the layout of the paper) public PageFormat getPageFormat(int index) { return (PageFormat)pageFormat; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -