Cropping a selected part of the image
843804Jan 4 2005 — edited Jan 13 2005hi all i have sample codes here for cropping an image...how do i crop out the image automatically after i release the mouse button? Can anyone pls help me cos i need to know how it works...
package MapWork;
// import packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.Container;
public class CropImage extends JPanel
{
BorderLayout borderLayout1 = new BorderLayout();
private Image img;
Dimension d;
int x, y, width, height;
int startX = 0, startY = 0, endX = 100, endY = 100;
public CropImage()
{
super();
setPreferredSize(new Dimension(800,500));
try
{
jbInit();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
/* Graphics2D is a class for rendering 2-dimensional shapes, text and
images in Java. It is used to construct the coordinates tranformations,
colour management, layout of text etc...*/
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(img, 0, 0, this);
// Math: to perform basic numeric operations. Math.min: return the
// smaller value between the 2 integers
int x = Math.min(startX,endX);
int y = Math.min(startY, endY);
// Math.abs: returns absolute value of a double value
int width = Math.abs(startX-endX);
int height = Math.abs(startY - endY);
// draw rectangle, specify the x & y axis and the width & height
g2d.drawRect(x, y, width, height);
//g2d.setColor(Color.red);
g2d.clipRect(x, y, width*2, height);
// copy the selected area to another frame
g2d.copyArea(x, y, width, height, width, height);
g2d.setClip(x, y, width, height);
}
private final Handler handler = new Handler();
private class Handler implements MouseListener, MouseMotionListener {
// occurs when mouse button is being pushed down
public void mousePressed(MouseEvent e)
{
setStartPoint(e.getPoint());
// Repaint method(): issues a request that a Component be redrawn within
// a specified time or as soon as possible if the time is left unspecified
repaint();
}
// mouseClicked: occurs when a mouse button is pressed and released
public void mouseClicked(MouseEvent e){}
// mouseExited:occurs when mouse cursor exits the unobscured part of
// component's space/geometry
public void mouseExited(MouseEvent e){}
// mouseEntered:occurs when mouse cursor enters the unobscured part of
// component's apace/geometry
public void mouseEntered(MouseEvent e){}
// mouseReleased:occurs when a mouse button is being released
public void mouseReleased(MouseEvent e)
{
setEndPoint(e.getPoint());
// Repaint method(): issues a request that a Component be redrawn within
// a specified time or as soon as possible if the time is left unspecified
repaint();
}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e)
{
setEndPoint(e.getPoint());
repaint();
}
public void setStartPoint(Point p)
{
startX = p.x;
startY = p.y;
}
public void setEndPoint(Point p)
{
endX = p.x;
endY = p.y;
}
}
private void jbInit()throws Exception
{
addMouseListener(handler);
addMouseMotionListener(handler);
img = Toolkit.getDefaultToolkit().getImage("koyomi.jpg");
}
}