ciao!
ho questo JPanel su cui disegnare:
codice:
private class ImagePanel extends JPanel implements MouseListener, MouseMotionListener {
private FileChooserPanel jfc = new FileChooserPanel();
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;
String forma;
public ImagePanel(String path, String forma) {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(width, height));
fileDelete = path;
this.forma = forma;
try {
buff = ImageIO.read(new File(path));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
capture(e);
dispose();
}
});
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);
g.setColor(Color.RED);
g.drawOval(xx, yy, w, h);
}
@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 tutto funziona più o meno, ma ho due domande:
-prima di tutto viene disegnato un ovale e non un cerchio: come posso fare per ottenere un cerchio?
-inoltre la linea si vede solo quando il mouse viene rilasciato: è possibile far vedere le linee in tempo reale??