/* * [Affirm.java] * * Summary: Displays Living Love or other Affirmations subliminally. * * Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.3 2006-03-04 */ package com.mindprod.affirm; import com.mindprod.common18.EIO; import com.mindprod.common18.JEButton; import javax.swing.JButton; import javax.swing.JTextArea; import javax.swing.JWindow; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.Random; import static java.lang.System.*; /** * Displays Living Love or other Affirmations subliminally. * * @author Roedy Green, Canadian Mind Products * @version 1.3 2006-03-04 * @since 2006 */ public final class Affirm { private static final int FIRST_COPYRIGHT_YEAR = 1999; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2006-03-06"; /** * embedded version string. */ private static final String VERSION_STRING = "1.3"; /** * background colour for window. */ private static final Color BACKGROUND_FOR_APP = new Color( 0x00bfff/* deep sky blue */ ); /** * background where for data entry where user enters a value. */ private static final Color BACKGROUND_FOR_EDITABLE = Color.WHITE; /** * foreground colour for window text */ private static final Color FOREGROUND_FOR_APP = Color.BLACK; /** * data entry text field colour */ private static final Color FOREGROUND_FOR_EDITABLE = Color.BLACK; /** * where we display the Affirmation. */ private static JTextArea affirmation; /** * one String per paragraph/affirmation. Contain embedded \n */ private static String[] affirmations; /** * how many seconds maximum to leave no affirmation displayed. */ private static int maxOff; /** * how many seconds maximum to leave affirmation displayed. */ private static int maxOn; /** * how many seconds minimum to leave no affirmation displayed. */ private static int minOff; /** * how many seconds minimum to leave affirmation displayed. */ private static int minOn; /** * button to stop the program */ private static JButton stop; /** * the window that encloses everything. */ private static JWindow w; /** * constructor not used. */ private Affirm() { } /** * Read in paragraphs, one paragraph per affirmation. */ private static void getAffirmations() { ArrayList affirmationsList = new ArrayList<>( 50 ); try { final StringBuilder sb = new StringBuilder( 200 ); BufferedReader in = EIO.getBufferedReader( Affirm.class.getResourceAsStream( "affirmations.txt" ), 4 * 1024, EIO.UTF8 ); // loop until eof while ( true ) { String line = in.readLine(); if ( line == null ) { break; } line = line.trim(); if ( line.length() == 0 ) { // have hit the end of a paragraph if ( sb.length() != 0 ) { affirmationsList.add( sb.toString() ); sb.setLength( 0 ); } } else { // save up one line of the paragraph if ( sb.length() != 0 ) { sb.append( '\n' ); } sb.append( line ); } } // end while // might have one last paragraph pending if ( sb.length() != 0 ) { affirmationsList.add( sb.toString() ); } } // end try catch ( IOException e ) { err.println( "unable read affirmations.txt resource." ); System.exit( 1 ); } affirmations = affirmationsList.toArray( new String[ affirmationsList .size() ] ); } // end getAffirmations /** * @param args four ints, minMsOn, maxMsOn, minMsOff, maxMsOff */ public static void main( String[] args ) { try { minOn = Integer.parseInt( args[ 0 ] ); maxOn = Integer.parseInt( args[ 1 ] ); minOff = Integer.parseInt( args[ 2 ] ); maxOff = Integer.parseInt( args[ 3 ] ); } catch ( Exception e ) { err.println( "Missing or invalid command line timing parameters; defaults assumed." ); minOn = 200; maxOn = 3000; minOff = 4000; maxOff = 10000; } getAffirmations(); w = new JWindow(); Container contentPane = w.getContentPane(); contentPane.setForeground( FOREGROUND_FOR_APP ); contentPane.setBackground( BACKGROUND_FOR_APP ); contentPane.setLayout( new FlowLayout() ); affirmation = new JTextArea(); affirmation.setForeground( FOREGROUND_FOR_EDITABLE ); affirmation.setBackground( BACKGROUND_FOR_EDITABLE ); contentPane.add( affirmation ); stop = new JEButton( "Stop" ); stop.setToolTipText( "Stop the program" ); contentPane.add( stop ); stop.addActionListener( new ActionListener() { /** * Invoked when an stop clicked */ public void actionPerformed( ActionEvent e ) { System.exit( 0 ); } } ); w.addWindowListener( new WindowAdapter() { /** * Handle request to shutdown. * @param e event giving details of closing. */ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } // end WindowClosing } // end anonymous class );// end addWindowListener line Random wheel = new Random(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); while ( true ) { // pick a random affirmation. affirmation.setText( affirmations[ wheel .nextInt( affirmations.length ) ] ); // shrink to fit w.pack(); // place at top right of screen w.setLocation( dim.width - w.getWidth(), 0 ); w.setVisible( true ); // leave display visible for a random interval try { // sleep for random number of seconds in bounds Thread.sleep( wheel.nextInt( maxOn - minOn ) + minOn ); } catch ( InterruptedException e ) { } w.setVisible( false ); // hide the display for a random interval try { Thread.sleep( wheel.nextInt( maxOff - minOff ) + minOff ); } catch ( InterruptedException e ) { } } // end while } // end main } // end Affirm