salve!
ho un piccolo problema con un JPanel e con il disegno di un rettangolo con il mouse.
in pratica riempio il JPanel con una immagine e poi selezione un'area con il mouse.
quest'area la salvo in un'altra immagine:
codice:
private class ImagePanel extends JPanel implements MouseListener, MouseMotionListener {
private BufferedImage buff;
private Rectangle currentRect;
private String fileDelete;
private int xx = 0;
private int yy = 0;
private int w = 0;
private int h = 0;
boolean done = false;
public ImagePanel(String path) {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(width, height));
fileDelete = path;
try {
buff = ImageIO.read(new File(path));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
capture(e);
}
});
addMouseListener(this);
addMouseMotionListener(this);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
@Override
public void mousePressed(MouseEvent e) {
xx = e.getX();
yy = e.getY();
currentRect = new Rectangle(xx, yy, 0, 0);
}
@Override
public void mouseDragged(MouseEvent e) {
currentRect.setSize(e.getX() - currentRect.x, e.getY() - currentRect.y);
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
w = e.getX() - currentRect.x;
h = e.getY() - currentRect.y;
currentRect.setSize(w, h);
done = true;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(buff, 0, 0, this);
if (done) {
g.drawRect(xx, yy, w, h);
}
}
public void capture(ActionEvent e) {
try {
FileChooserPanel.salva(xx, yy, w, h);
DeleteTemp.delete(fileDelete);
} catch (AWTException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
il problema è che prende un pezzo di parte sopra la selezione e taglia un pezzo di parte sotto.
avete qualche idea??