Swing - need help increasing apparent speed of JComponent dragging
The dragging of JComponents is beginning to really bog down my program. I'm open to any kind of suggestions you guys have on increasing the performance, even if it involves rewriting in another language. At present, CPU usage is a little ridiculous (even for Java), but what's really problematic is the choppy graphics on systems without good cards.
Here's the core of the dragging code, you can compile and run it:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Drag {
private static double mouseX;
private static double mouseY;
private static ArrayList<JLabel> labels;
private static JFrame frame;
public static void main(String args[]) {
frame = new JFrame();
frame.setLayout(null);
frame.setSize(800,800);
int xpos = 300;
int ypos = 300;
labels = new ArrayList<JLabel>();
for(int i = 0; i < 5; i++) {
JLabel lab = new JLabel("JLABEL");
labels.add(lab);
frame.add(lab);
lab.setBounds(xpos, ypos, 55, 35);
lab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
lab.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
mouseX = event.getPoint().getX();
mouseY = event.getPoint().getY();
}
});
lab.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
int xChange = (int)(event.getPoint().getX() - mouseX);
int yChange = (int)(event.getPoint().getY() - mouseY);
moveJLabels(xChange, yChange);
}
});
xpos += 50;
ypos += 50;
}
frame.setVisible(true);
}
public static void moveJLabels(int xChange, int yChange) {
for (JLabel lab: labels) {
Rectangle r = lab.getBounds();
r.translate(xChange, yChange);
lab.setBounds(r);
}
frame.repaint();
}
}

