0

I would like to illustrate a project about railroads.

I decided to use Swing. I have a background map in a JPanel and I draw little circles that moves on the railroads. It works perfectly if I have only one train, but I would like to add more trains.

Here is what I started to do (and works) :

public static void main(String[] args) {

  // JFrame and background panel construction
  JFrame frame = new JFrame();
  JLayeredPane lpane = new JLayeredPane();
  ImagePanel panelBg = new ImagePanel(new ImageIcon("map.jpg").getImage());;

  frame.setPreferredSize(new Dimension(1791, 695));
  frame.setLayout(new BorderLayout());
  frame.add(lpane,BorderLayout.CENTER);
  lpane.setBounds(0,0,panelBg.getImg().getWidth(null),panelBg.getImg().getHeight(null));

  panelBg.setBounds(0,0,panelBg.getImg().getWidth(null),panelBg.getImg().getHeight(null));
  panelBg.setOpaque(true);

  lpane.add(panelBg, new Integer(0), 0);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
  go(lpane,panelBg);  
  }

private static void go(JLayeredPane pan,ImagePanel panBg) {

  Parcours panelP = new Parcours();
  panelP.setBounds(0,0,panBg.getImg().getWidth(null),panBg.getImg().getHeight(null));
  panelP.setOpaque(false);
  pan.add(panelP, new Integer(1), 0);

  for(int i=0; i<panelP.getTable().size(); i++){
    panelP.setPosX(panelP.getTable().get(i).getX()-6);
    panelP.setPosY(panelP.getTable().get(i).getY()-6);
    panelP.repaint();
    try{
      Thread.sleep(100);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

"go" reads an ArrayList containing coordinates where my circle should go.

I really don't know how to create several trains. Should I create several JPanels or only one with all my circles ?

If I remember well, I should use Threads but I tried to implement them and I can't start.

Thank you for your help

2
  • 4
    Swing is not thread safe. You could start reading the Swing concurrency tutorial which would already learn you that you should never call Thread.sleep on the Swing thread. Instead, use a javax.swing.Timer if you want animations Commented Jun 27, 2013 at 12:49
  • 1
    See also FleetPanel. Commented Jun 27, 2013 at 17:23

2 Answers 2

1

You could use a central data object that stores the trains.In every cycle the trains get drawn inside swing. On the other side the trains get updated from your threads.

Another approach contains train objects that get drawn and run a thread inside them to update them self.

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you Robin (see comments of my first post), Swing Timers seem to be the best solution so far. I removed my awful Thread.sleep and set timers instead, it works, thanks again.

Thank you Templar too

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.