001 package com.croftsoft.apps.savor; 002 003 import java.awt.*; 004 import java.util.*; 005 006 import org.jdesktop.jdic.screensaver.ScreensaverContext; 007 import org.jdesktop.jdic.screensaver.ScreensaverSettings; 008 import org.jdesktop.jdic.screensaver.SimpleScreensaver; 009 010 /********************************************************************* 011 * CroftSoft Savor. 012 * 013 * @version 014 * $Id: Savor2D.java,v 1.1 2006/12/09 06:31:36 croft Exp $ 015 * @since 016 * 2006-09-26 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class Savor2D 022 extends SimpleScreensaver 023 ////////////////////////////////////////////////////////////////////// 024 ////////////////////////////////////////////////////////////////////// 025 { 026 027 private static final String PROPERTY_OVAL_SIZE = "ovalSize"; 028 029 private static final String [ ] PROPERTY_VALUES = { 030 "tiny", 031 "small", 032 "medium", 033 "large" }; 034 035 private static final int [ ] OVAL_SIZE_DIVISORS = { 036 Integer.MAX_VALUE, 037 8, 038 4, 039 2 }; 040 041 private static final int FRAME_PERIOD = 50; 042 043 // 044 045 private final Map<String, Integer> ovalSizeMap; 046 047 private final Random random; 048 049 // 050 051 private int 052 ovalSize, 053 width, 054 height; 055 056 private Image offscreenImage; 057 058 ////////////////////////////////////////////////////////////////////// 059 ////////////////////////////////////////////////////////////////////// 060 061 public Savor2D ( ) 062 ////////////////////////////////////////////////////////////////////// 063 { 064 random = new Random ( ); 065 066 ovalSizeMap = new HashMap<String, Integer> ( ); 067 068 for ( int i = 0; i < PROPERTY_VALUES.length; i++ ) 069 { 070 ovalSizeMap.put ( 071 PROPERTY_VALUES [ i ], 072 new Integer ( OVAL_SIZE_DIVISORS [ i ] ) ); 073 } 074 } 075 076 ////////////////////////////////////////////////////////////////////// 077 ////////////////////////////////////////////////////////////////////// 078 079 @Override 080 public void init ( ) 081 ////////////////////////////////////////////////////////////////////// 082 { 083 int ovalSizeDivisor 084 = OVAL_SIZE_DIVISORS [ OVAL_SIZE_DIVISORS.length - 1 ]; 085 086 final ScreensaverContext screensaverContext = getContext ( ); 087 088 final ScreensaverSettings screensaverSettings 089 = screensaverContext.getSettings ( ); 090 091 String propertyValue 092 = screensaverSettings.getProperty ( PROPERTY_OVAL_SIZE ); 093 094 if ( propertyValue != null ) 095 { 096 propertyValue = propertyValue.trim ( ).toLowerCase ( ); 097 098 final Integer ovalSizeDivisorInteger 099 = ovalSizeMap.get ( propertyValue ); 100 101 if ( ovalSizeDivisorInteger != null ) 102 { 103 ovalSizeDivisor = ovalSizeDivisorInteger.intValue ( ); 104 } 105 } 106 107 final Component component = screensaverContext.getComponent ( ); 108 109 final int componentWidth = component.getWidth ( ); 110 111 final int componentHeight = component.getHeight ( ); 112 113 if ( ( componentWidth > 0 ) 114 && ( componentHeight > 0 ) ) 115 { 116 offscreenImage 117 = component.createImage ( componentWidth, componentHeight ); 118 } 119 120 ovalSize = Math.max ( 121 1, 122 Math.min ( componentWidth, componentHeight ) / ovalSizeDivisor ); 123 124 width = componentWidth + 2 * ovalSize; 125 126 height = componentHeight + 2 * ovalSize; 127 } 128 129 @Override 130 public void paint ( final Graphics graphics ) 131 ////////////////////////////////////////////////////////////////////// 132 { 133 if ( offscreenImage == null ) 134 { 135 return; 136 } 137 138 final Graphics2D offscreenGraphics2D 139 = ( Graphics2D ) offscreenImage.getGraphics ( ); 140 141 offscreenGraphics2D.setRenderingHint ( 142 RenderingHints.KEY_ANTIALIASING, 143 RenderingHints.VALUE_ANTIALIAS_ON ); 144 145 offscreenGraphics2D.setColor ( 146 new Color ( 147 ( float ) Math.random ( ), 148 ( float ) Math.random ( ), 149 ( float ) Math.random ( ) ) ); 150 151 final int ovalWidth = Math.max ( 152 1, ( int ) ( ovalSize * ( 1 + 0.2 * random.nextGaussian ( ) ) ) ); 153 154 final int ovalHeight = Math.max ( 155 1, ( int ) ( ovalSize * ( 1 + 0.2 * random.nextGaussian ( ) ) ) ); 156 157 offscreenGraphics2D.fillOval ( 158 ( int ) ( Math.random ( ) * width ) - ovalSize, 159 ( int ) ( Math.random ( ) * height ) - ovalSize, 160 ovalWidth, 161 ovalHeight ); 162 163 offscreenGraphics2D.dispose ( ); 164 165 graphics.drawImage ( offscreenImage, 0, 0, null ); 166 167 try 168 { 169 Thread.sleep ( FRAME_PERIOD ); 170 } 171 catch ( final InterruptedException ex ) 172 { 173 // ignore 174 } 175 } 176 177 ////////////////////////////////////////////////////////////////////// 178 ////////////////////////////////////////////////////////////////////// 179 }