# Threads: Denkzeit-Timer



## LemieuxFHV (5. Feb 2009)

Hallo!

Ich will für mein Mühle-Spiel einen Timer für die Player realisieren, die die Denkzeit mitzählen. (siehe Bild)









Jetzt habe ich jeweils einen Timer pro Spieler implementiert, weiß aber nicht, wie ich das mit dem abwechselnd warten realisieren soll.
Hat jemand eine Idee oder Lösung?

*TimerLabel*

```
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;

public class TimerLabel extends JLabel implements Runnable {
	
	private static final long serialVersionUID = 1L;
	
	private Date m_time;

	public TimerLabel() {
		super();
		m_time = new Date(0);
	}

	public void run() {
		while (true) {
			
			m_time.setTime(m_time.getTime() +  1000);
			setText(printTime());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public String printTime() {
		
		DateFormat dateTime = new SimpleDateFormat("mm:ss");
		String labelText = dateTime.format(m_time);
		
		return labelText;
	}
}
```


*Panel auf dem der Timer läuft*

```
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PanelPlayer extends JPanel {

	private static final long serialVersionUID = 1L;

	private Icon m_playerIcon;
	private JLabel m_icon;
	private JLabel m_name;
	private TimerLabel m_thinkTime;
	
	public PanelPlayer(Player player) {
		super();
		initGUI(player);
	}
	
	private void initGUI(Player player) {
		
		m_playerIcon = new ImageIcon(PanelInformation.class.getResource(
				"/images/" + player.getPic() + ".jpg"));
		
		FlowLayout layout = new FlowLayout(); 
		setLayout(layout);
		layout.setVgap(20);
		layout.setHgap(20);
		layout.setAlignment(FlowLayout.LEFT);
		
		m_icon = new JLabel(m_playerIcon);
		m_icon.setPreferredSize(new Dimension(60, 60));
		
		m_name = new JLabel(player.getName());
		m_name.setFont(new Font("Dialog", Font.PLAIN, 22));
		m_name.setForeground(Color.WHITE);
		m_name.setPreferredSize(new Dimension(250, 26));
		
		m_thinkTime = new TimerLabel();		
		m_thinkTime.setVerticalAlignment(JLabel.BOTTOM);
		m_thinkTime.setPreferredSize(new Dimension(60, 60));
		m_thinkTime.setForeground(Color.WHITE);
		m_thinkTime.setFont(new java.awt.Font("Dialog", Font.BOLD, 20));
		
		new Thread(m_thinkTime).start();
        
		
		add(m_icon);
		add(m_thinkTime);
		add(m_name);
		
		setOpaque(false);
		setPreferredSize(new Dimension(294, 300));
	}
}
```


m_thinkTime ist das Label mit dem Timer!



Schöne Grüße,
Lemieux


----------



## LemieuxFHV (5. Feb 2009)

Nochmal das Bild, falls es nicht funktioniert hat:

http://img136.imageshack.us/my.php?image=timeryb5.jpg


----------



## Guest (5. Feb 2009)

Hier noch die Klasse Player!

Zeile 44 von PanelPlayer wurde noch geändert in:

```
m_thinkTime = player.getTimer();
```


Klasse Player:

```
public class Player {

	private String m_name;
	private boolean m_host;
	private EnumStoneColor m_color;
	private EnumUserPic m_pic;
	
	private int m_stonesSet;
	private int m_stonesRemoved;
	
	private TimerLabel m_thinkTime;
	
	public Player(String name, boolean host, EnumStoneColor color, EnumUserPic pic) {
		m_name = name;
		m_host = host;
		m_color = color;
		m_pic = pic;
		m_stonesSet = 0;
		m_stonesRemoved = 0;
		
		m_thinkTime = new TimerLabel();
	}
	
	public String getName() {
		return m_name;
	}
	
	public boolean getHost() {
		return m_host;
	}
	
	public EnumStoneColor getColor() {
		return m_color;
	}
	
	public EnumUserPic getPic() {
		return m_pic;
	}
	
	public int getStonesSet() {
		return m_stonesSet;
	}
	
	public int getStonesRemoved() {
		return m_stonesRemoved;
	}
	
	public TimerLabel getTimer() {
		return m_thinkTime;
	}
}
```


----------



## LemieuxFHV (5. Feb 2009)

Problem gelöst!


Trotzdem danke!


----------

