Eine Schachuhr (Eine Schachuhr), Lektion, page 723659
https://www.purl.org/stefan_ram/pub/schachuhr_java (permalink) is the canonical URI of this page.
Stefan Ram
Android-Entwicklung
Java-Kurs

Eine Schachuhr

Diese Werkstattnotiz zeigt eine Schachuhr, die als JavaFX-Anwendung oder als Android-App verwendet werden kann.

Das Model muß dazu entweder mit der JavaFX-Anwendung oder mit der Android-Anwendung zusammenkopiert werden.

Model

final class Unit

{ long duration;

long startTime;

long endTime;

boolean isRunning;

public void start( final long point )

{ if( !this.isRunning )

{ if( point < endTime )

throw new java.lang.IllegalArgumentException();

this.startTime = point;

this.isRunning = true; }}

public void end( final long point )

{ if( this.isRunning )

{ if( point < startTime )

throw new java.lang.IllegalArgumentException();

this.endTime = point;

this.duration += point - startTime;

this.isRunning = false; }}

public long duration( final long point )

{ return this.duration +

( this.isRunning ? point - this.startTime : 0 ); }}

class Clock

{ final Unit unit0 = new Unit();

final Unit unit1 = new Unit();

boolean unit0IsRunning;

public void start0( final long point )

{ this.unit0.start( point );

this.unit1.end( point );

this.unit0IsRunning = true; }

public void start1( final long point )

{ this.unit0.end( point );

this.unit1.start( point );

this.unit0IsRunning = false; }

public void pause( final long point )

{ this.unit0.end( point );

this.unit1.end( point ); }

public void cont( final long point )

{ if( this.unit0IsRunning )this.start0( point );

else this.start1( point ); }

public long duration0( final long point )

{ return this.unit0.duration( point ); }

public long duration1( final long point )

{ return this.unit1.duration( point ); }

public java.lang.String toString( final long point )

{ return java.lang.String.format( "time: %3d [S0: %3d, S1: %3d]", point,

this.duration0( point ), this.duration1( point )); }}

/* Diese Klasse ist eine Art von Adapterklasse, welche die aufwaertszaehlenden

Uhren "Clock" in abwaertszaehlende Uhren "Credit" wandelt. */

final class Credit extends Clock

{

long credit0;

long credit1;

public void setCredit0( final long credit0 )

{ this.credit0 = credit0; }

public void setCredit1( final long credit1 )

{ this.credit1 = credit1; }

public long getCredit0()

{ return this.credit0; }

public long getCredit1()

{ return this.credit1; }

public long duration0( final long point )

{ return this.credit0 - super.duration0( point ); }

public long duration1( final long point )

{ return this.credit1 - super.duration1( point ); }}

JavaFX-Anwendung

public final class Main extends javafx.application.Application

{

class Timer extends javafx.animation.AnimationTimer

{ public void handle( final long time )

{ Main.this.handle( time ); }}

javafx.scene.control.TextField text0;

javafx.scene.control.TextField text1;

Credit clock;

private final void handle( final long time )

{ text0.setText( java.lang.String.valueOf( clock.duration0( time ) ));

text1.setText( java.lang.String.valueOf( clock.duration1( time ) )); }

public void start( final javafx.stage.Stage primaryStage )

{ text0 = new javafx.scene.control.TextField();

text1 = new javafx.scene.control.TextField();

final javafx.scene.layout.HBox clocks

= new javafx.scene.layout.HBox();

clocks.getChildren().add( text0 );

clocks.getChildren().add( text1 );

final javafx.scene.control.Button button0

= new javafx.scene.control.Button( "white" );

final javafx.scene.control.Button button1

= new javafx.scene.control.Button( "black" );

final javafx.scene.control.Button buttonPause

= new javafx.scene.control.Button( "pause" );

final javafx.scene.control.Button buttonCont

= new javafx.scene.control.Button( "continue" );

button0.setOnAction

( block ->

{ clock.start1( java.lang.System.nanoTime() ); });

button1.setOnAction

( block ->

{ clock.start0( java.lang.System.nanoTime() ); });

buttonPause.setOnAction

( block ->

{ clock.pause( java.lang.System.nanoTime() ); });

buttonCont.setOnAction

( block ->

{ clock.cont( java.lang.System.nanoTime() ); });

final javafx.scene.layout.HBox buttons

= new javafx.scene.layout.HBox();

buttons.getChildren().add( button0 );

buttons.getChildren().add( button1 );

buttons.getChildren().add( buttonPause );

buttons.getChildren().add( buttonCont );

final javafx.scene.layout.VBox all

= new javafx.scene.layout.VBox();

all.getChildren().add( clocks );

all.getChildren().add( buttons );

final javafx.scene.Scene scene = new javafx.scene.Scene( all );

primaryStage.setScene( scene );

clock = new Credit();

clock.setCredit0( 120L * 60L * 1_000_000_000L );

clock.setCredit1( 120L * 60L * 1_000_000_000L );

primaryStage.show();

this.new Timer().start(); }}

Android-App

public final class Main extends android.app.Activity

implements android.animation.TimeAnimator.TimeListener

{

final android.app.Activity activity = this;

final android.content.Context context = this;

android.widget.TextView text0;

android.widget.TextView text1;

Credit clock;

public void onTimeUpdate

( android.animation.TimeAnimator animation,

long elapsed, long dt_ms )

{ long time = java.lang.System.nanoTime();

/* Eventuell kann hier ein Parameter an Stelle

von "time" verwendet werden. */

text0.setText( java.lang.String.valueOf

( clock.duration0( time ) ));

text1.setText( java.lang.String.valueOf

( clock.duration1( time ) )); }

@java.lang.Override protected final void onCreate

( final android.os.Bundle bundle )

{ super.onCreate( bundle );

text0 = new android.widget.TextView( Main.this.context );

text1 = new android.widget.TextView( Main.this.context );

android.widget.LinearLayout clocks

= new android.widget.LinearLayout( this );

clocks.addView( text0 );

clocks.addView( text1 );

final android.widget.Button button0

= new android.widget.Button( Main.this.context );

button0.setText( "white" );

final android.widget.Button button1

= new android.widget.Button( Main.this.context );

button1.setText( "black" );

final android.widget.Button buttonPause

= new android.widget.Button( Main.this.context );

buttonPause.setText( "pause" );

final android.widget.Button buttonCont

= new android.widget.Button( Main.this.context );

buttonCont.setText( "continue" );

button0.setOnClickListener

( new android.view.View.OnClickListener()

{ @java.lang.Override public void onClick

( final android.view.View view )

{ clock.start1( java.lang.System.nanoTime() ); }} );

button1.setOnClickListener

( new android.view.View.OnClickListener()

{ @java.lang.Override public void onClick

( final android.view.View view )

{ clock.start0( java.lang.System.nanoTime() ); }} );

buttonPause.setOnClickListener

( new android.view.View.OnClickListener()

{ @java.lang.Override public void onClick

( final android.view.View view )

{ clock.pause( java.lang.System.nanoTime() ); }} );

buttonCont.setOnClickListener

( new android.view.View.OnClickListener()

{ @java.lang.Override public void onClick

( final android.view.View view )

{ clock.cont( java.lang.System.nanoTime() ); }} );

android.widget.LinearLayout buttons

= new android.widget.LinearLayout( this );

buttons.addView( button0 );

buttons.addView( button1 );

buttons.addView( buttonPause );

buttons.addView( buttonCont );

android.widget.LinearLayout all

= new android.widget.LinearLayout( this );

all.setOrientation( android.widget.LinearLayout.VERTICAL );

all.addView( clocks );

all.addView( buttons );

Main.this.activity.setContentView( all );

clock = new Credit();

clock.setCredit0( 120L * 60L * 1000000000L );

clock.setCredit1( 120L * 60L * 1000000000L );

final android.animation.TimeAnimator animator

= new android.animation.TimeAnimator();

animator.setTimeListener( this );

animator.start(); }}

Aussprachehinweise
super ˈsupɚ (sd)

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram723659 stefan_ram:723659 Eine Schachuhr Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd723659, slrprddef723659, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/schachuhr_java