📄 viewerscrollpane.java
字号:
package org.net9.oops.jsee;
import javax.swing.*;
import java.awt.*;
import java.awt.print.*;
//import java.awt.event.*;
import java.awt.print.PrinterJob;
import java.awt.image.*;
/**
* Title:
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
public class ViewerScrollPane extends JScrollPane implements Printable {
JLabel label;
ImageIcon image;
JViewport viewport;
boolean PRINT_LANDSCAPE;
double ZoomRate[] = {0.1, 0.15, 0.2, 0.3, 0.5, 0.7, 1, 1.5, 2.0, 3.0, 5.0, 7.0, 10.0};
private int zI = 6;
public ViewerScrollPane(ImageIcon iIcon) {
super( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED , JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
label = new JLabel( iIcon );
image = iIcon;
viewport = new JViewport();
viewport.setView(label);
this.setViewport(viewport);
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black); //set default foreground color to black
RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = this.getSize(); //get size of document
double panelWidth; //width in pixels
double panelHeight; //height in pixels
double pageHeight; //height of printer page
double pageWidth; //width of printer page
double scale;
if (PRINT_LANDSCAPE) {
pf.setOrientation(PageFormat.LANDSCAPE);
panelWidth = d.width; //width in pixels
panelHeight = d.height; //height in pixels
pageHeight = pf.getImageableWidth(); //height of printer page
pageWidth = pf.getImageableHeight(); //width of printer page
}
else {
pf.setOrientation(PageFormat.PORTRAIT);
panelWidth = d.width; //width in pixels
panelHeight = d.height; //height in pixels
pageHeight = pf.getImageableHeight(); //height of printer page
pageWidth = pf.getImageableWidth(); //width of printer page
}
scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
// Make sure not print empty pages and most likely onle one image is printed
if(pageIndex >= totalNumPages) {
return Printable.NO_SUCH_PAGE;
}
// Shift Graphic to line up with beginning of print-imageable region
g2.translate(pf.getImageableX(), pf.getImageableY());
// Shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex*pageHeight);
// Scale the page so the width fits...
g2.scale(scale, scale);
this.paint(g2); //repaint the page for printing
return Printable.PAGE_EXISTS;
}
public void DoPrint() {
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(this);
Object stringArray[] = {"Horizontal", "Lanscape", "Cancel"};
int selection = JOptionPane.showOptionDialog(this, "Print paper layout", "Select an option",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, stringArray, stringArray[0]);
if (selection != 2) // Cancel button was clicked
{
if (selection == 0)
PRINT_LANDSCAPE = false;
else if (selection == 1)
PRINT_LANDSCAPE = true;
if (pj.printDialog())
{
try {
pj.print();
} catch (Exception PrintException) {}
}
}
}
void loadNewImage(ImageIcon iIcon) {
if (iIcon.getIconHeight() > 0) {
label = new JLabel(iIcon);
image = iIcon;
viewport = new JViewport();
viewport.setView(label);
this.setViewport(viewport);
}
}
int imageWidth() {
return image.getIconWidth();
}
int imageHeight() {
return image.getIconHeight();
}
public void resetZooming(){
zI=6;
}
boolean Zoom(boolean In) {
if (In)
zI++;
else
zI--;
try {
final int imageHeight = image.getIconHeight();
final int imageWidth = image.getIconWidth();
ImageIcon ZoomImage = new ImageIcon(image.getImage().getScaledInstance((int)(imageWidth*ZoomRate[zI]), (int)(imageHeight*ZoomRate[zI]), Image.SCALE_SMOOTH));
label = new JLabel( ZoomImage );
viewport.setView(label);
this.setViewport(viewport);
} catch (OutOfMemoryError oome) {System.out.println("Out of memory - Image too large");}
// disable the zooming before program run out of memory
if (zI == 12 || zI == 0)
return(false);
else
return(true);
}
public void RotateImage() {
try {
Image img = image.getImage();
int iheight = image.getIconHeight();
int iwidth = image.getIconWidth();
int buffer[] = new int[iheight * iwidth];
int rotate[] = new int[iheight * iwidth];
Image rot = null;
MediaTracker tracker = new MediaTracker (this);
tracker.addImage (img, 0);
tracker.waitForAll();
PixelGrabber grabber = new PixelGrabber(img, 0, 0, iwidth, iheight, buffer, 0, iwidth);
try {
grabber.grabPixels();
} catch(InterruptedException e) {
e.printStackTrace();
}
for(int y = 0; y < iheight; y++) {
for(int x = 0; x < iwidth; x++) {
rotate[((iwidth-x-1)*iheight)+y] = buffer[(y*iwidth)+x];
}
}
rot = createImage(new MemoryImageSource(iheight, iwidth, rotate, 0, iheight));
label = new JLabel(image = new ImageIcon(rot));
viewport = new JViewport();
viewport.setView(label);
this.setViewport(viewport);
}
catch (Exception e) {
e.printStackTrace();
}
/* flip
*/
}
public void FlipImage(boolean vertical) {
try {
Image img = image.getImage();
int iheight = image.getIconHeight();
int iwidth = image.getIconWidth();
int buffer[] = new int[iheight * iwidth];
int flip[] = new int[iheight * iwidth];
Image flipImage = null;
MediaTracker tracker = new MediaTracker (this);
tracker.addImage (img, 0);
tracker.waitForAll();
PixelGrabber grabber = new PixelGrabber(img, 0, 0, iwidth, iheight, buffer, 0, iwidth);
try {
grabber.grabPixels();
} catch(InterruptedException e) {
e.printStackTrace();
}
if (!vertical) {// flip horizontally
for(int y = 0; y < iheight; y++) {
for(int x = 0; x < iwidth; x++) {
flip[iwidth-x-1 + (y*iwidth)] = buffer[x+(y*iwidth)];
}
}
} else { // flip vertically
for(int y = 0; y < iheight; y++) {
for(int x = 0; x < iwidth; x++) {
flip[x + (iheight-y-1)*iwidth] = buffer[x+(y*iwidth)];
}
}
}
flipImage = createImage(new MemoryImageSource(iwidth, iheight, flip, 0, iwidth));
label = new JLabel(image = new ImageIcon(flipImage));
viewport = new JViewport();
viewport.setView(label);
this.setViewport(viewport);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -