📄 printpreviewdialog.java
字号:
String name, int preferredWidth,
int preferredHeight) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension bestCursorSize = toolkit.getBestCursorSize(
preferredWidth, preferredHeight);
int width = image.getWidth(null);
int height = image.getHeight(null);
if (bestCursorSize.width==width && bestCursorSize.height==height) {
return toolkit.createCustomCursor(image, hotspot, name);
}
else {
BufferedImage cursorImage = new BufferedImage(
bestCursorSize.width, bestCursorSize.height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = cursorImage.createGraphics();
g2d.drawImage(image, 0,0, null);
return toolkit.createCustomCursor(cursorImage, hotspot, name);
}
}
/*****************************************************************************/
/**
* Returns the text on the "Next Page" button.
*
* @return The text on the Next Page button.
* @see #setNextPageButtonText
*/
public final String getNextPageButtonText() {
return nextPageButton.getText();
}
/*****************************************************************************/
/**
* Returns the border to use on page preview panels.
*
* @return The border.
*/
private synchronized final Border getPagePreviewBorder() {
if (pagePreviewBorder==null)
pagePreviewBorder = new MatteBorder(1, 1, 2, 2,
Color.BLACK);
return pagePreviewBorder;
}
/*****************************************************************************/
/**
* Returns the text on the "Prev Page" button.
*
* @return The text on the Previous Page button.
* @see #setPrevPageButtonText
*/
public final String getPrevPageButtonText() {
return prevPageButton.getText();
}
/*****************************************************************************/
/**
* Returns the text on the "Print" button.
*
* @return The text on the Print button.
* @see #setPrintButtonText
*/
public final String getPrintButtonText() {
return printButton.getText();
}
/*****************************************************************************/
/**
* Increments the first preview page, if possible, and updates the
* screen. This method is designed as an alternative to
* <code>setPreviewPage()</code> that performs better with the more
* preview pages visible, as only one new page ever has to be created.
*/
public void incrementPreviewPage() {
if (pageImage==null || currentPage==numPages-numVisiblePages)
return;
currentPage++;
// Grab the "first" page displayed. We'll remove this one
// from the preview panel, push any pages following it
// "back," then reuse this one for the new page and add it
// back at the end.
PagePreview pp = pageImage[0];
previewPanel.remove(pp);
// Move 'forward' one page in all visible pages. This should work
// even if some of the pages are null (i.e., no more pages to view),
// as we'd just be setting stuff to null.
for (int i=0; i<numVisiblePages-1; i++)
pageImage[i] = pageImage[i+1];
pageWidth = (int)(pageFormat.getWidth());
pageHeight = (int)(pageFormat.getHeight());
try {
// Reuse the preview panel since its master image size is still valid.
BufferedImage tempImage = pp.getSourceImage();
Graphics g = tempImage.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0, pageWidth,pageHeight);
if (masterImage.print(g, pageFormat, currentPage+numVisiblePages-1) == Printable.PAGE_EXISTS) {
int w = (int)(pageWidth*scale/100);
int h = (int)(pageHeight*scale/100);
pp.setScaledSize(w, h); // Re-initialize the scaled (displayed) version.
pageImage[numVisiblePages-1] = pp;
previewPanel.add(pp); // Add back to the end.
}
else {
pageImage[numVisiblePages-1] = null;
}
g.dispose();
previewPanel.revalidate();
previewPanel.repaint(); // Needed, but why?
} catch (PrinterException pe) {
pe.printStackTrace();
}
}
/*****************************************************************************/
// Called whenever the user changes the value in one of the comboboxes.
public void itemStateChanged(ItemEvent e) {
JComboBox source = (JComboBox)e.getSource();
if (e.getStateChange()==ItemEvent.SELECTED) {
setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
if (source.equals(sizeComboBox)) {
String choice = (String)e.getItem();
choice = choice.substring(0, choice.length()-1);
scale = Integer.parseInt(choice);
Cursor newCursor = getCursorForScale(scale);
if (pageImage!=null && pageImage.length>0) {
int w = (int)(pageWidth*scale/100);
int h = (int)(pageHeight*scale/100);
boolean setNewCursor = pageImage[0].getCursor()!=newCursor;
for (int i=0; i<pageImage.length; i++)
if (pageImage[i]!=null) {
pageImage[i].setScaledSize(w,h);
if (setNewCursor==true)
pageImage[i].setCursor(newCursor);
}
}
previewPanel.revalidate();
}
else if (source.equals(numPagesComboBox)) {
int choice = Integer.parseInt((String)e.getItem());
choice = Math.min(choice, numPages);
setNumVisiblePages(choice);
// We MUST reset the preview page to page 0 because if we
// don't, the user could be previewing the last page with
// visible pages set to 1, then up visible pages 4, but then
// if there aren't 4 pages in the document and the user
// then clicks "Previous", an Exception will be thrown.
setPreviewPage(0);
}
updateButtons();
setCursor( Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
/*****************************************************************************/
/**
* Sets the text on the "Close" button.
*
* @param text The text for the Close button.
* @see #getCloseButtonText
*/
public void setCloseButtonText(String text) {
closeButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the text on the "Next Page" button.
*
* @param text The text for the Next Page button.
* @see #getNextPageButtonText
*/
public void setNextPageButtonText(String text) {
nextPageButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the number of pages currently being previewed.
*
* @param num The number of pages to preview. If <code>num</code> is less
* than <code>1</code>, then it is set to <code>1</code>. If it
* is greater than the number of pages in the document, it is
* set to the number of pages in the document.
*/
private void setNumVisiblePages(int num) {
if (numPages<=0)
numPages = 1;
if (num>numPages)
num = numPages;
numVisiblePages = num;
}
/*****************************************************************************/
/**
* Sets the text on the "Prev Page" button.
*
* @param text The text on the Previous Page button.
* @see #getPrevPageButtonText
*/
public void setPrevPageButtonText(String text) {
prevPageButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the text on the "Print" button.
*
* @param text The text on the Print button.
* @see #getPrintButtonText
*/
public void setPrintButtonText(String text) {
printButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the first page being previewed.
*
* @param pageNumber The first page to preview.
*/
private void setPreviewPage(int pageNumber) {
previewPanel.removeAll(); // Remove all currently-viewed pages.
currentPage = pageNumber;
pageWidth = (int)(pageFormat.getWidth());
pageHeight = (int)(pageFormat.getHeight());
int w = (int)(pageWidth*scale/100);
int h = (int)(pageHeight*scale/100);
try {
Cursor cursorToUse = getCursorForScale(scale);
for (int i=0; i<numVisiblePages; i++) {
BufferedImage img = new BufferedImage(pageWidth,pageHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, pageWidth,pageHeight);
if (masterImage.print(g, pageFormat, currentPage+i) == Printable.PAGE_EXISTS) {
pageImage[i] = new PagePreview(w,h, img, cursorToUse);
previewPanel.add(pageImage[i]);
}
else
pageImage[i] = null;
g.dispose();
}
previewPanel.revalidate();
previewPanel.repaint(); // Needed, but why?
} catch (PrinterException pe) {
pe.printStackTrace();
}
}
/*****************************************************************************/
/**
* Sets what scale to make the preview images of the pages.
*
* @param newScale The scale to use when sizing the page
* previews, in percent (i.e., 33 => 33%).
*/
public void setScale(int newScale) {
sizeComboBox.setSelectedItem(Integer.toString(newScale)+"%");
}
/*****************************************************************************/
/**
* Updates the buttons to be enabled/disabled as appropriate.
*/
private void updateButtons() {
prevPageButton.setEnabled(currentPage>0);
nextPageButton.setEnabled(currentPage<numPages-numVisiblePages);
}
/*****************************************************************************/
/*********************** PRIVATE INNER CLASSES *******************************/
/*****************************************************************************/
/**
* A panel representing the image of a page.
*/
class PagePreview extends JPanel {
/**
*
*/
private static final long serialVersionUID = -5739638207425923837L;
protected int width;
protected int height;
protected BufferedImage sourceImage;
protected Image drawImage;
public PagePreview(int width, int height, BufferedImage source,
Cursor cursorToUse) {
sourceImage = source;
setScaledSize(width, height);
setBorder(getPagePreviewBorder());
setCursor(cursorToUse);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getPreferredSize() {
Insets ins = getInsets();
return new Dimension(width+ins.left+ins.right,
height+ins.top+ins.bottom);
}
public BufferedImage getSourceImage() {
return sourceImage;
}
public void paintComponent(Graphics g) {
g.drawImage(drawImage, 0, 0, this);
}
protected void processMouseEvent(MouseEvent e) {
if (e.getID()==MouseEvent.MOUSE_PRESSED &&
e.getClickCount()==1 &&
e.getButton()==MouseEvent.BUTTON1) {
setScale(getCursor()==zoomInCursor ? 100 : 25);
}
super.processMouseEvent(e);
}
public void setScaledSize(int width, int height) {
this.width = width;
this.height = height;
drawImage = sourceImage.getScaledInstance(width, height,
Image.SCALE_SMOOTH);//Image.SCALE_DEFAULT);
repaint();
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -