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();

}

}

[2004 byte] By [an4327a] at [2007-11-26 23:08:36]
# 1
even if it involves rewriting in anotherlanguage. Is it a threat? :)
kirillga at 2007-7-10 14:03:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

even if it involves rewriting in another

language.

Is it a threat? :)

I find that threats are effective with a stubborn child like Java :)

But seriously, I don't relish rewriting 5000 lines of code into a language I know less well. I'd actually prefer to think the slow speed is my fault, and that there's a way to do this much fater.

an4327a at 2007-7-10 14:03:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

I find that threats are effective with a stubborn child like Java :)

I find comments like that unnecessary as they have nothing to do with the question.

Getting rid of the frame.repaint() drop the CPU by almost half on my machine.

Also, try just using the setLocation() method instead of setBounds() (since that is actually what you are doing when you drag a component).

camickra at 2007-7-10 14:03:19 > top of Java-index,Desktop,Core GUI APIs...