Please find the Java code to crop and capture images by mouse press
1. CropImage.java
/**
* @author Gopal Malaker
* @version 1.0
* @since 2018-04-02
*
*/
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 {
private static final long serialVersionUID = -8718753966695793325L;
int drag_status = 0, c1, c2, c3, c4;
public static void main(String args[]) {
new CropImage().start();
}
public void start() {
ImagePanel im = new ImagePanel("C:\\Users\\gmalaker\\Desktop\\Image1.jpeg");
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("C:\\Users\\gmalaker\\Desktop\\Cropped_Image.jpeg");
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);
}
}
2. ImagePanel.java
/**
* @author Gopal Malaker
* @version 1.0
* @since 2018-04-02
*
*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 185260570543109853L;
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));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
setVisible(true);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
The Output is attached below:-
Original Image Frame:-
After Cropping and Capturing Image, we get:-
Please let me know in case of any clarifications required. Thanks!
How do I write a java code that mimics charAt without using java API just primitives and no charAt to be used? I know it comes from primitives but I am confused on how to assemble the loops to derive my own charAt code
47. What is overriding? Describe using java code showing an example of overriding. 48. Does Java allow us to refine behavior in subclasses? If so, show Java code that demonstrates this property. 49. Does Java allow us to replace behavior in subclasses? If so, show Java code that demonstrates this property. 50. Can dynamic binding be used in C++? If so, show how this is accomplished. 51. Is there a cost to providing the dynamic binding capability? If so, describe...
Use below drawings to describe how to measure a). period b). pulse width c). duty cycle using input capture feature of a timer. No need to write code or set up register, just plain English describing how to do the measurement. Hint, you are using the input capture of a timer. 2. K-Pulse Width- Duty cycle = th/h Use below drawings to describe how to measure a). period b). pulse width c). duty cycle using input capture feature of a...
Code in Java. please post as copyable text code, Not an image. LinkedIntList Programming: Add a removeOdd() method to the LinkedIntList class. This method should remove the odd indexed nodes in the stored linked list. Add the test method to the existing test program. If you have a LinkedIntList with contents of: [1,2,3,42,56,78,103] Calling removeOdd() should leave the LinkedIntList looking like: [2,42,78] You must test for possible error conditions. Turn in just your code for the method and your console...
remove noise entire noise from this image using matlab code & get the original image Problem 4 (35 points) 1. Describe in details how would you detect and reduce the periodic noise in the input image shown (you should have received a copy of this image via email) and remove it to get an enhanced image. 2. Use MATLAB to show the results of your answer. Include in your submission your code and resulting image. Sinusoldal Nolse Sinusoidal Noise Problem...
Explain the following object-oriented(OO) concepts with the aid of code examples (either C++ or Java): Inheritance Over-riding . Over-loading Describe any differences between C++ and Java in how these OO concepts are implemented? 3(c) 17 Marks] Describe C++ namespaces using a code example. Describe Java packages, again using a code example. How do C++ namespaces compare to Java packages? 3(d) [5 Marks] What are inline methods in C++/Java? Explain the terms accessor and mutator. Explain the following object-oriented(OO) concepts with...
Hello,, I need Code java to : Loading and Saving of Images with Java 2D (Interface and buttons to upload and to save image) Thank you, Regareds,
In the following graph, write a Java program using Topological Sort. please write java code and show me the output. Thank you :) b.
JAVA. Rolling a die 60,000 times with streams. Modify the code below to capture the time before and after evaluating the stream pipeline (use package java.time which contains instant and duration). Then calculate the difference between the Instants to determine the total time. Use Instant's static method now to get the current time. To determine the difference between two Instants, use class Duration's static method between, which returns a Duration object containing the time difference. Duration provides methods like toMillis...
write an algorithm by using java code to find the approximate roots of the function using the fixed-point method