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