001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jppf.node.screensaver.impl;
019
020import java.awt.*;
021import java.util.*;
022import java.util.Timer;
023
024import javax.swing.*;
025
026import org.jppf.node.screensaver.JPPFScreenSaver;
027import org.jppf.utils.TypedProperties;
028
029/**
030 * A built-in simple screen saver implementation.
031 * It draws 500 discs at random locations with a random color 25 times / second.
032 * Every 5 seconds, the screen is emptied (repainted in black).
033 * @author Laurent Cohen
034 * @since 4.0
035 */
036public class SimpleScreenSaver extends JPanel implements JPPFScreenSaver {
037  /**
038   * The pseudo-random number generator.
039   */
040  private Random rand = new Random(System.nanoTime());
041  /**
042   * Timer used to draw additional shapes efery 40 ms.
043   */
044  private Timer timer = null;
045  /**
046   * Set to <code>true</code> every 5s to indicate the screen should be emptied.
047   */
048  private volatile boolean reset = false;
049
050  /**
051   * Default constructor.
052   */
053  public SimpleScreenSaver() {
054    super(true);
055  }
056
057  @Override
058  public JComponent getComponent() {
059    return this;
060  }
061
062  @Override
063  public void init(final TypedProperties config, final boolean fullscreen) {
064    setBackground(Color.BLACK);
065    timer = new Timer("JPPFScreenSaverTimer");
066    // executes approximately every 40 ms
067    timer.scheduleAtFixedRate(new TimerTask() {
068      @Override
069      public void run() {
070        repaint();
071      }
072    }, 40L, 40L);
073    // executes approximately every 5 s
074    timer.scheduleAtFixedRate(new TimerTask() {
075      @Override
076      public void run() {
077        reset = true;
078      }
079    }, 5000L, 5000L);
080  }
081
082  @Override
083  public void destroy() {
084    if (timer != null) timer.cancel();
085  }
086
087  @Override
088  public void paintComponent(final Graphics g) {
089    // we do not call super.paintComponent(g) because we do not want to have
090    // the backrground repainted each time, which would erase the shapes we draw.
091    int w = getWidth();
092    int h = getHeight();
093    if (reset) {
094      reset = false;
095      g.setColor(Color.BLACK);
096      g.fillRect(0, 0, w, h);
097    } else {
098      int n = 5;
099      for (int i=0; i<500; i++) {
100        int x = rand.nextInt(w-(n-1));
101        int y = rand.nextInt(h-(n-1));
102        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
103        g.fillOval(x, y, n, n);
104      }
105    }
106  }
107}