Olá boa noite,
Estou a tentar fazer drag & drop, mas não esta a correr muito bem, o drag & drop está a funcionar mas a bola ao ser movida fica deformada, e quando carrego na bola dá a sensação que ela sobe e depois voltar para o cursor.
Em baixo deixo o código se alguém puder dar uma ajudinha agradeço
Grato
[code]
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Win4 extends JPanel {
public Win4() {
setLayout(null);
MCircle crc = new MCircle(100, 1);
add(crc);
crc.setLocation(100, 100);
MCircle crc2 = new MCircle(150, 2);
add(crc2);
crc2.setLocation(100, 100);
JFrame f = new JFrame();
f.add(this);
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics g2d = g.create();
int coluna = 200;
int linha = 200;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
g2d.drawOval(linha, coluna, 50, 50);
linha += 60;
}
linha = 200;
coluna += 60;
}
g2d.dispose();
}
public static void main(String[] args) {
new Win4();
}
}[/code]
[code]
import java.awt.;
import java.awt.event.;
class MCircle extends Component {
protected int diameter;
Point p0;
Point pc;
Cursor cursor;
int num;
public MCircle(int diameter, int num) {
super();
this.diameter = diameter;
this.num = num;
setSize(diameter, diameter);
enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
public void paint(Graphics g) {
super.paint(g);
if(num == 1){
g.setColor(Color.red);
} else {
g.setColor(Color.white);
}
g.fillOval(0, 0, diameter - 1, diameter - 1);
}
public void setSize(int w, int h) {
super.setSize(diameter, diameter);
}
public Dimension getPreferredSize() {
return new Dimension(diameter, diameter);
}
public Dimension getMinimumSize() {
return new Dimension(diameter, diameter);
}
public Dimension getMaximumSize() {
return new Dimension(diameter, diameter);
}
public void setLocation(int x, int y) {
int r = diameter / 2;
super.setLocation(x - r, y - r);
}
public void processMouseMotionEvent(MouseEvent e) {
switch (e.getID()) {
case MouseEvent.MOUSE_DRAGGED:
Point p = e.getPoint();
Point np = new Point(pc.x + p.x - p0.x, pc.y + p.y - p0.y);
setLocation(np);
pc = np;
break;
}
super.processMouseEvent(e);
}
public void processMouseEvent(MouseEvent e) {
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
p0 = e.getPoint();
pc = getLocation();
break;
case MouseEvent.MOUSE_ENTERED:
cursor = getCursor();
setCursor(new Cursor(Cursor.HAND_CURSOR));
break;
case MouseEvent.MOUSE_EXITED:
setCursor(cursor);
break;
}
super.processMouseEvent(e);
}
}[/code]