We will learn from the following steps:
Enter an image to specify the path of the image to be processed
Allow users to drag and drop parts to trim
Use the Robot class to determine the trim part's coordinates after selection
Trim the selected image and keep
Next we start the coding section.
LISTING1: The introduced class
Copy Code code as follows:
Import Java.awt.Graphics;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.event.MouseMotionListener;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO;
Import Javax.swing.JFrame;
Description
The Graphics class contains methods for drawing rectangles
We use the Rectangle class as a drag-and-drop rectangular area for tailoring.
Robot class for capturing screenshots
Use mouse listeners to get drag time for the mouse
Robot class uses BufferedImage to do image processing
The file class is used to open an image file
The ImageIO class is used to write an image to a PNG or JPG image file
JFrame for display interface
Now we write the entry class that contains the Main method
LISTING2: Entrance class
Copy Code code as follows:
public class Cropimage extends JFrame implements MouseListener, Mousemotionlistener
{
int drag_status=0,c1,c2,c3,c4;
public static void Main (String args[])
{
New Cropimage (). Start ();
}
Description
A class named Cropimage was written
This class extends the JFrame to implement all of the functions of the frame
Implements a different mouse event listener to know when the user starts dragging the mouse pointer
The drag_status variable is used to save the coordinates when the mouse starts dragging
We define the main method to invoke a Start method, which is defined below
Next is the Start method.
Listing 2
Copy Code code as follows:
public void Start ()
{
Imagepanel im=new Imagepanel ("f:\\wallpaper\\wallpapers\\1.jpg");
Add (IM);
SetSize (400,400);
SetVisible (TRUE);
Addmouselistener (this);
Addmousemotionlistener (this);
Setdefaultcloseoperation (Exit_on_close);
}
Description
We define a class called Imagepanel, using the image to be processed as a parameter
Place the Imagepanel in the JFrame to show the picture and start listening for mouse events
Let's define a method for handling mouse events
Listing 3: Mouse event handler function
Copy Code code as follows:
@Override
public void mouseclicked (MouseEvent arg0) {
}
@Override
public void mouseentered (MouseEvent arg0) {
}
@Override
public void mouseexited (MouseEvent arg0) {
}
@Override
public void mousepressed (MouseEvent arg0) {
Repaint ();
C1=arg0.getx ();
C2=arg0.gety ();
}
@Override
public void mousereleased (MouseEvent arg0) {
Repaint ();
if (drag_status==1)
{
C3=arg0.getx ();
C4=arg0.gety ();
Try
{
Draggedscreen ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
@Override
public void mousedragged (MouseEvent arg0) {
Repaint ();
Drag_status=1;
C3=arg0.getx ();
C4=arg0.gety ();
}
@Override
public void mousemoved (MouseEvent arg0) {
}
public void Paint (Graphics g)
{
Super.paint (g);
int w = C1-C3;
int h = C2-C4;
w = w *-1;
H = h *-1;
if (w<0)
w = w *-1;
G.drawrect (c1, C2, W, h);
}
Description
Stores the current coordinates when the mouse is pressed to C1 and C2
Set the drag state variable Drag_status to True when the mouse is pressed and starts dragging
When the mouse button is released, the image clipping area has been selected and the Draggedscreen method is called
The Paint method is used to drag the rectangular display, drawing the rectangle through the coordinates of the current and initial records.
The following is the code for the Draggedscreen method
Copy Code code as follows:
Listing 4:draggedscreen method
public void Draggedscreen () throws Exception
{
int w = C1-C3;
int h = C2-C4;
w = w *-1;
H = h *-1;
Robot Robot = new Robot ();
BufferedImage img = robot.createscreencapture (New Rectangle (C1, c2,w,h));
File Save_path=new file ("Screen1.jpg");
Imageio.write (IMG, "JPG", Save_path);
System.out.println ("Cropped image saved successfully.");
}}
Description
First calculate the height and width of the image
Use the Robot class to take a screenshot of the trimmed area and hold it to another file screen1.jpg
The complete code
Copy Code code as follows:
Listing 5:imagepanel.java
Import java.awt.Dimension;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Javax.swing.ImageIcon;
Import Javax.swing.JPanel;
Class Imagepanel extends JPanel {
Private Image img;
Public Imagepanel (String img) {
This (new ImageIcon (IMG). GetImage ());
}
Public Imagepanel (Image img) {
This.img = img;
Dimension size = new Dimension (Img.getwidth (null), img.getheight (null));
Dimension size = new Dimension (10,10);
Setpreferredsize (size);
Setminimumsize (size);
Setmaximumsize (size);
SetSize (size);
SetLayout (NULL);
}
public void Paintcomponent (Graphics g) {
G.drawimage (IMG, 0, 0, NULL);
}
}
Listing 6:cropimage.java
Import Java.awt.Graphics;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.event.MouseMotionListener;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO;
Import Javax.swing.JFrame;
public class Cropimage extends JFrame implements MouseListener, Mousemotionlistener
{
int drag_status=0,c1,c2,c3,c4;
public static void Main (String args[])
{
New Cropimage (). Start ();
}
public void Start ()
{
Imagepanel im=new Imagepanel ("f:\\wallpaper\\wallpapers\\1.jpg");
Add (IM);
SetSize (400,400);
SetVisible (TRUE);
Addmouselistener (this);
Addmousemotionlistener (this);
Setdefaultcloseoperation (Exit_on_close);
}
public void Draggedscreen () throws Exception
{
int w = C1-C3;
int h = C2-C4;
w = w *-1;
H = h *-1;
Robot Robot = new Robot ();
BufferedImage img = robot.createscreencapture (New Rectangle (C1, c2,w,h));
File Save_path=new file ("Screen1.jpg");
Imageio.write (IMG, "JPG", Save_path);
System.out.println ("Cropped image saved successfully.");
}
@Override
public void mouseclicked (MouseEvent arg0) {
}
@Override
public void mouseentered (MouseEvent arg0) {
}
@Override
public void mouseexited (MouseEvent arg0) {
}
@Override
public void mousepressed (MouseEvent arg0) {
Repaint ();
C1=arg0.getx ();
C2=arg0.gety ();
}
@Override
public void mousereleased (MouseEvent arg0) {
Repaint ();
if (drag_status==1)
{
C3=arg0.getx ();
C4=arg0.gety ();
Try
{
Draggedscreen ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
@Override
public void mousedragged (MouseEvent arg0) {
Repaint ();
Drag_status=1;
C3=arg0.getx ();
C4=arg0.gety ();
}
@Override
public void mousemoved (MouseEvent arg0) {
}
public void Paint (Graphics g)
{
Super.paint (g);
int w = C1-C3;
int h = C2-C4;
w = w *-1;
H = h *-1;
if (w<0)
w = w *-1;
G.drawrect (c1, C2, W, h);
}
}