Threads: Denkzeit-Timer

Status
Nicht offen für weitere Antworten.
L

LemieuxFHV

Gast
Hallo!

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

my.php




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
Code:
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
Code:
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
 
G

Guest

Gast
Hier noch die Klasse Player!

Zeile 44 von PanelPlayer wurde noch geändert in:
Code:
		m_thinkTime = player.getTimer();


Klasse Player:
Code:
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;
	}
}
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
S ständig neue Threads Spiele- und Multimedia-Programmierung 4
E Zwei Threads miteinander laufen lassen Spiele- und Multimedia-Programmierung 6
Z Objekte animieren mit Threads Spiele- und Multimedia-Programmierung 4
M Minecraft Minecraft, MySQL und Threads Spiele- und Multimedia-Programmierung 12
J Problem mit Threads Spiele- und Multimedia-Programmierung 8
F Java zwei gleiche Threads mit unterschiedlichen Parametern Spiele- und Multimedia-Programmierung 2
L Synchronisierung von Threads die Antwort? Spiele- und Multimedia-Programmierung 8
J Frage zu Threads Spiele- und Multimedia-Programmierung 5
I Problem mit Threads Spiele- und Multimedia-Programmierung 25
Tapsi Thread wartet auf Threads Spiele- und Multimedia-Programmierung 7
hdi mehrere Threads/Tasks in einem synchronisieren -> TaskPoo Spiele- und Multimedia-Programmierung 36
0 Problem mit zeitsynchroner Hauptschleife/Threads Spiele- und Multimedia-Programmierung 28
D threads in j3d? Spiele- und Multimedia-Programmierung 6
M KeyListener funktioniert nicht während Timer läuft Spiele- und Multimedia-Programmierung 26
G Timer Spiele- und Multimedia-Programmierung 13
R 2D platformer - enemy damage -> TIMER PROBLEM Spiele- und Multimedia-Programmierung 3
A Countdown-Timer für Spiel Spiele- und Multimedia-Programmierung 4
G Schuss mit Timer Spiele- und Multimedia-Programmierung 2

Ähnliche Java Themen

Neue Themen


Oben