Olá pessoal!
Tenho umn JLabel com um imageIcon que uso para exibir uma imagem redimensionável. Consigo alterar o tamanho da imagem sem problemas…
O que está errado é quando tento mover o JLabel (o resultado esperado é “mover a imagem”). O JLabel fica piscnado e aparecendo uns "fantasmas"em tudo quanto é canto absurdo do meu JPanel… Algumas vezes, quando solto o botão do mouse, o JLabel aparece como mágica em um local completamente diferente daquele para onde movi o mouse tentar mover…
Alguma idéia de como resolver isso?!
Aqui vai meu código! Obrigado a todos pela atenção!
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.lang.Math;
public class Resizable extends MouseAdapter implements MouseMotionListener {
int fix_pt_x = 0;
int fix_pt_y = 0;
Component mResizeable;
Cursor mOldcursor;
private final int RESIZE_MARGIN_SIZE = 3;
private double proportion;
private boolean resizing;
private boolean moving;
private JPanel canvas;
private boolean mousePressed = false;
public Resizable(Component c, double proportion, JPanel canvas) {
this.proportion = proportion;
this.canvas = canvas;
this.canvas.setDoubleBuffered(true);
mResizeable = c;
c.addMouseListener(this);
c.addMouseMotionListener(this);
resizing = false;
moving = false;
}
public void mouseEntered(MouseEvent me) {
Component c = (Component)me.getSource();
setCursorType(me.getPoint());
}
private void setCursorType(Point p) {
boolean s = p.y + RESIZE_MARGIN_SIZE >= mResizeable.getHeight() &&
p.y - RESIZE_MARGIN_SIZE <= mResizeable.getHeight();
boolean e = p.x + RESIZE_MARGIN_SIZE >= mResizeable.getWidth() &&
p.x - RESIZE_MARGIN_SIZE <= mResizeable.getWidth();
if (e) {
if (s) {
mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
resizing = true;
moving = false;
return;
}
}
else if(p.x >= 2 && p.x < (mResizeable.getX() + mResizeable.getWidth()-2) &&
p.y >= 2 && p.y < (mResizeable.getY() + mResizeable.getHeight()-2)) {
mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
moving = true;
resizing = false;
return;
}
else {
mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
moving = false;
resizing = false;
}
}
public void mouseExited(MouseEvent me) {
if (mOldcursor != null)
((Component)me.getSource()).setCursor(mOldcursor);
mOldcursor = null;
mousePressed = false;
}
public void mousePressed(MouseEvent me) {
mousePressed = true;
Cursor c = mResizeable.getCursor();
Point loc = mResizeable.getLocation();
if (c.equals(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR))) {
fix_pt_x = loc.x;
fix_pt_y = loc.y;
return;
}
if (c.equals(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR))) {
//fix_pt_x = loc.x;
//fix_pt_y = loc.y;
return;
}
}
public void mouseReleased(MouseEvent me) {
fix_pt_x = -1;
fix_pt_y = -1;
moving = false;
resizing = false;
mousePressed = false;
}
public void mouseMoved(MouseEvent me) {
setCursorType(me.getPoint());
}
public void mouseDragged(MouseEvent me) {
Point p = me.getPoint();
if(moving){ //AQUI EU MOVO O JLabel, ACHO QUE O PROBLEMA ESTÁ AQUI...
mResizeable.setLocation(p.x, p.y);
mResizeable.repaint();
return;
}
int width = fix_pt_x == -1 ? mResizeable.getWidth() : p.x;
if(width < 3) width = 10;
if(resizing)
mResizeable.setSize(new Dimension((width > 3 ) ? width : 3, (int)Math.round(proportion * width)));
if(mResizeable.getClass().getName() == "javax.swing.JLabel" && resizing){
JLabel label = (JLabel)mResizeable;
ImageIcon icon = new ImageIcon("alexandraroth.jpg");
icon.setImage(icon.getImage().getScaledInstance(width, (int)Math.round(proportion * width), 0));
label.setIcon(icon);
return;
}
}
private void debug(String m){
System.out.println(m);
}
}