I have the class CropImage
that works correctly, the method start
is created and there I generate a JFrame
decorado that should show the current image that is drawn in ImagePanel
.
If I put an absolute address works but only with the image that is happening, but the reality is that you should read the current image that is drawn at that time in ImagePanel
... Any ideas?
public class CropImage extends JFrame implements MouseListener, MouseMotionListener
{
int drag_status = 0, c1, c2, c3, c4;
private final LineBorder border = new LineBorder(Color.RED,1);
JMenuBar JMenuCropped = new JMenuBar();
JMenu JMenuCropped1 = new JMenu();
JMenu JMenuCropped2 = new JMenu();
JMenu JMenuCropped3 = new JMenu();
public void start()
{
//Aquí funciona con una dirección absoluta
ImagePanel im = new ImagePanel("C:.........");
add(im);
setUndecorated(true);
getRootPane().setBorder(border);
setFocusable(true);
getContentPane().add(JMenuCropped, BorderLayout.NORTH);
setSize(550,550);
setLocation(460,180);
setVisible(true);
addMouseListener(this);
addMouseMotionListener(this);
JMenuCropped.setPreferredSize(new Dimension(35,35));
JMenuCropped.add(JMenuCropped1);
JMenuCropped.add(JMenuCropped2);
JMenuCropped.add(JMenuCropped3);
JMenuCropped1.setIcon(new javax.swing.ImageIcon(
getClass().getResource("/iconos/verde_c.png"))
);
JMenuCropped1.setToolTipText("Minimize window");
JMenuCropped1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
setExtendedState(JFrame.NORMAL);
}
});
JMenuCropped2.setIcon(
new javax.swing.ImageIcon(getClass().getResource("/iconos/amarillo_c.png"))
);
JMenuCropped2.setToolTipText("Maximize window");
JMenuCropped2.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
setExtendedState(getExtendedState()| FramePrincipal.MAXIMIZED_BOTH);
}
});
JMenuCropped3.setIcon(
new javax.swing.ImageIcon(getClass().getResource("/iconos/rojo_c.png"))
);
JMenuCropped3.setToolTipText("Close window");
JMenuCropped3.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
setVisible(false);
}
});
}
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("Historial_Salidas/Cropped/cropped.jpg");
ImageIO.write(img, "JPG", save_path);
JOptionPane.showMessageDialog(
null,
"<html><h4>"+
"Cropped image saved successfully At:"+
" Historial_Salidas/Cropped/cropped.jpg"+
"</h4></html>",
"Information",
JOptionPane.INFORMATION_MESSAGE
);
setVisible(false);
}
@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.setColor(Color.red);
g.drawRect(c1, c2, w, h);
}
}
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));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}