class ScrollablePicture extends JLabel
implements Scrollable,
MouseMotionListener {
private int maxUnitIncrement = 1;
private boolean missingPicture = false;
private Point start;
private int width;
private int height;
private BufferedImage image;
public ScrollablePicture(BufferedImage i, int m) {
super(new ImageIcon(i));
image = i;
if (i == null) {
missingPicture = true;
setText("No picture found.");
setHorizontalAlignment(CENTER);
setOpaque(true);
setBackground(Color.white);
}
maxUnitIncrement = m;
//Let the user scroll by dragging to outside the window.
setAutoscrolls(true); //enable synthetic drag events
addMouseMotionListener(this); //handle mouse drags
this.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
start = e.getPoint();
System.out.println(start);
}
@Override
public void mouseReleased(MouseEvent e) {
Point end = e.getPoint();
System.out.println(end);
width = Math.abs(end.x - start.x);
height = Math.abs(end.y - start.y);
System.out.println(width + " " + height);
if(start.x > end.x)
start.x = end.x;
if(start.y > end.y)
start.y = end.y;
Graphics g = ScrollablePicture.this.getGraphics();
g.setColor(Color.red);
g.drawRect(start.x, start.y, width, height);
try {
Thread.sleep(1500);
} catch (InterruptedException ex) {
Logger.getLogger(ScrollablePicture.class.getName() ).log(Level.SEVERE, null, ex);
}
try {
ImageIO.write(cutImage(image, start, width, height), "jpg", new File("cuttedImage.jpg"));
} catch (IOException ex) {
Logger.getLogger(ScrollablePicture.class.getName() ).log(Level.SEVERE, null, ex);
}
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
});
}
public static BufferedImage cutImage(BufferedImage img, Point start, int width, int height) {
BufferedImage target = new BufferedImage(width > 0 ? width : 1, height > 0 ? height : 1, img.getType());
for(int y = 0; y < height; y++)
for(int x = 0; x < width; x++)
target.setRGB(x, y, img.getRGB(x + start.x, y + start.y));
return target;
}
//Methods required by the MouseMotionListener interface:
@Override
public void mouseMoved(MouseEvent e) { }
public void mouseDragged(MouseEvent e) {
//The user is dragging us, so scroll!
Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
scrollRectToVisible(r);
}
public Dimension getPreferredSize() {
if (missingPicture) {
return new Dimension(320, 480);
} else {
return super.getPreferredSize();
}
}
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction) {
//Get the current position.
int currentPosition = 0;
if (orientation == SwingConstants.HORIZONTAL) {
currentPosition = visibleRect.x;
} else {
currentPosition = visibleRect.y;
}
//Return the number of pixels between currentPosition
//and the nearest tick mark in the indicated direction.
if (direction < 0) {
int newPosition = currentPosition -
(currentPosition / maxUnitIncrement)
* maxUnitIncrement;
return (newPosition == 0) ? maxUnitIncrement : newPosition;
} else {
return ((currentPosition / maxUnitIncrement) + 1)
* maxUnitIncrement
- currentPosition;
}
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction) {
if (orientation == SwingConstants.HORIZONTAL) {
return visibleRect.width - maxUnitIncrement;
} else {
return visibleRect.height - maxUnitIncrement;
}
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
public void setMaxUnitIncrement(int pixels) {
maxUnitIncrement = pixels;
}
}