Pessoal, estou com dificuldade para arrastar a imagem no meu programa.
Usando JLabel eu consegui.
Só que com o JPanel eu não estou conseguindo.
Com o Label, eu faço assim:
public class ArrastarImagem extends MouseAdapter implements MouseMotionListener{
/** */
int baseX = -1;
int baseY = -1;
public void mouseDragged(MouseEvent e) {
Component b = e.getComponent();
if ((this.baseX != -1) && (this.baseY != -1)) {
int x = b.getX() + e.getX() - this.baseX;
int y = b.getY() + e.getY() - this.baseY;
b.setLocation(x, y);
b.getParent().repaint();
}
}
public void mouseMoved(MouseEvent e) {
//
}
public void mousePressed(MouseEvent e) {
this.baseX = e.getX();
this.baseY = e.getY();
}
public void mouseReleased(MouseEvent e) {
this.baseX = -1;
this.baseY = -1;
}
}
ArrastarImagem mouseController = new ArrastarImagem();
imageLabel.addMouseListener(mouseController);
imageLabel.addMouseMotionListener(mouseController);
Agora vou mostrar com o JPanel.
Tenho a minha classe JFlipPanel que extends JPanel.public class JFlipPanel extends JPanel implements MouseInputListener, ActionListener {
protected int rotationX;
//construtor
public JFlipPanel()
{
super();
//add os eventos do mouse
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
//adiciona page 1
paintPage(g2, currentLeftImage, bookBounds.x, bookBounds.y, pageWidth, bookBounds.height, this, false);
// adiciona page 2
paintPage(g2, currentRightImage, bookBounds.x + pageWidth, bookBounds.y, pageWidth, bookBounds.height, this, true);
}
public void mouseDragged(MouseEvent e) {
Component b = e.getComponent();
if ((this.rotationX != -1) && (this.baseY != -1)) {
int x = b.getX() + e.getX() - rotationX;
int y = b.getY() + e.getY() - this.baseY;
b.setLocation(x, y);
b.getParent().repaint();
}
public void mouseReleased(MouseEvent e) {
//this.baseX = -1;
rotationX = -1;
this.baseY = -1;
}
Bom, basicamente é isso.
Não consigo arrastar a imagem que está dentro do JPanel.
Alguém poderia me ajudar a descobrir onde está o erro?
Obrigado