As like snipping tool I want to snip a portion of my screen. I tried with following code with this I can select the portion but not able to extract image:
public class MySnipTool {
public static Rectangle selectionBounds;
public Graphics2D g2d;
public static void main(String[] args) {
new MySnipTool();
}
public MySnipTool() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new CapturePane());
Rectangle bounds = getVirtualBounds();
frame.setLocation(bounds.getLocation());
frame.setSize(bounds.getSize());
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
});
}
public class CapturePane extends JPanel {
private Point clickPoint;
public CapturePane() {
setOpaque(false);
MouseAdapter mouseHandler = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
System.exit(0);
}
}
@Override
public void mousePressed(MouseEvent e) {
clickPoint = e.getPoint();
selectionBounds = null;
}
@Override
public void mouseReleased(MouseEvent e) {
clickPoint = null;
}
@Override
public void mouseDragged(MouseEvent e) {
Point dragPoint = e.getPoint();
int x = Math.min(clickPoint.x, dragPoint.x);
int y = Math.min(clickPoint.y, dragPoint.y);
int width = Math.max(clickPoint.x - dragPoint.x, dragPoint.x - clickPoint.x);
int height = Math.max(clickPoint.y - dragPoint.y, dragPoint.y - clickPoint.y);
selectionBounds = new Rectangle(x, y, width, height);
repaint();
// CropImage.cropMyImage(new BufferedImage("D:\\NetbeansProjects\\testing\\cropimage.jpg"), x, y, width, height);
}
};
addMouseListener(mouseHandler);
addMouseMotionListener(mouseHandler);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g.create();
g2d.setColor(new Color(255, 255, 255, 128));
Area fill = new Area(new Rectangle(new Point(0, 0), getSize()));
if (selectionBounds != null) {
fill.subtract(new Area(selectionBounds));
}
g2d.fill(fill);
if (selectionBounds != null) {
g2d.setColor(Color.BLACK);
g2d.draw(selectionBounds);
}
g2d.dispose();
}
}
public static Rectangle getVirtualBounds() {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
for (GraphicsDevice gd : lstGDs) {
bounds.add(gd.getDefaultConfiguration().getBounds());
}
return bounds;
}
}
I need to implement crop with the help of the snipping.
Aucun commentaire:
Enregistrer un commentaire