-2

I have two point and I draw a line with drawline(x1,y1,x2,y2); I would like to move a label on this line (between this two points).

 m=y2-y1/x2-x1;

if(x1<x2)
    for (int i=x1;i<x2;i++)
    {
    label.setbound(i,y1+(x-i)*m,label.getwidth(),label.gethigh());
    }
    else{
    for (int i=x1;i<x2;i--)
    {
    label.setbound(i,y1+(x-i)*m,label.getwidth(),label.gethigh());
    }
    }

I use thread to move.

//

I ask this question because i'm writing a code for Travel salesman problem and i want move a thing on the route ,use thread for change position of label . for example:drawline( 23, 65, 231,124); move salesman on the line.

my IDE is netbeans and jpanel layout is free designed

  • 1
    Why and where do you need help? What's the problem (other than the non-compiling code)? – JB Nizet Feb 08 '13 at 22:52
  • 1
    Is the container that the label is attached to using a layout manager? If not, have you tried repainting the container? – MadProgrammer Feb 08 '13 at 22:52
  • A problem with the posted code is that you never sleep() after setting the location of the label. Since the code executes so fast it will just look like it moves from the start to end position all at once. – camickr Feb 09 '13 at 01:50

1 Answers1

1

There's very little information to go on and so many reasons why you might be having problems.

Below is a simple example of how I might solve your problems. Note- I, personally, don't like null layouts, they mess around to much with how the internals of Swing works and tend to produce more problems then they solve. Instead, when I want to do away with layout managers, I use a JLayeredPane instead.

public class BouncingLabel {

    public static void main(String[] args) {
        new BouncingLabel();
    }

    public BouncingLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JLayeredPane {

        private JLabel label;
        private Point p;
        private int dv = 2;

        public TestPane() {
            p = new Point();
            label = new JLabel("Look Ma, no hands!");
            add(label);

            Timer timer = new Timer(60, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    p.y = (getHeight() - label.getHeight()) / 2;
                    p.x = p.x + dv;
                    if (p.x + label.getWidth() > getWidth()) {
                        dv *= -1;
                        p.x = getWidth() - label.getWidth() + dv;
                    } else if (p.x < 0) {
                        dv *= -1;
                        p.x = 0 + dv;
                    }
                    label.setLocation(p);
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 100);
        }

        @Override
        public void doLayout() {
            super.doLayout();
            label.setSize(label.getPreferredSize());
            label.getLocation(p);
        }

    }

}

Updated

On close inspection of the code, you appear to be violating the Event Dispatching Threads - no blocking rule. All UI updates are maintained within a single thread (the EDT), any action which blocks this thread, will prevent the EDT from dispatching events to the all the components, including repaint requests.

Doing something like this..

for (int i=x1;i<x2;i++)
{
    label.setbound(i,y1+(x-i)*m,label.getwidth(),label.gethigh());
}

Means that you've stopped the EDT from dispatching any required update events to the rest of the UI, preventing it from updating the screen, until you exit the method.

Take a look at Concurrency in Swing for more details.

I would also take a close look at How to use Swing Timers and Swing Worker for some solutions.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • +1, for the Timer to show the animation. Not sure I understand your comment about using a JLayeredPane when you want to do away with layout managers. a JLayeredPane does use a "null layout". A JLayeredPane was designed to include lots of specialized functionality which is not required for this problem. So I would keep it simple and just use a JPanel with a null layout. – camickr Feb 09 '13 at 01:49
  • @camickr Your probably right, I just seem to have endless problems with updating a panel when it's using a null layout and `JLayeredPane` seems to work... – MadProgrammer Feb 09 '13 at 02:13
  • Not sure why you would have problems. Based on your posted code the only difference would be to add a setLayout(null) at the top of the constructor. I agree that in general there are problems when using a null layout, and a null layout should generally be avoided. I would be interested in seeing a SSCCE where the null layout layered pane solution works better than a null layout JPanel solution. Of course I'm talking about using components on a single panel, not multiple layers of panels. – camickr Feb 09 '13 at 02:41
  • I've probably just generated a phobia of null layouts ;) – MadProgrammer Feb 09 '13 at 03:28
  • thanks , I ask this question becouse i'm writing a code for Travel salesman problem and i want move a thing on the route – mohammad javad Feb 09 '13 at 09:02