1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
   |  
 
/*------------------------------------------------------------------------*
 * Originally implemented by Scott Flinn, in association with the
 * Imager Graphics Laboratory at the University of British Columbia
 * (scottflinn@alumni.uwaterloo.ca).
 *
 * This source code may be freely distributed and modified for any purpose
 * as long as these introductory comments are not removed.  Please be
 * aware that this represents the author's initial experiments with the
 * Java platform and should not necessarily be considered good examples
 * of Java programming.
 *------------------------------------------------------------------------*/
 
/*------------------------------------------------------------------------*
 *  The BW class implements the black-white contrast illusion.  It is
 *    meant to be run in a separate window using AppletStarter.
 *------------------------------------------------------------------------*/
 
import java.awt.*;
 
 
/*------------------------------  Class BW  ------------------------------*/
 
public class BWApplication extends Panel
{
  static BWPanel     canvas;
  static BWControls  controls;
  static Button      dismiss;
 
  public static void main(String[] args){
    Frame f = new Frame();
    f.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
            };
        });
 
    /*-----------------------  constructor  ------------------------*/
 
      GridBagLayout       gridbag;
      GridBagConstraints  c;
 
        // Create the layout manager and a constraint object
        gridbag = new GridBagLayout();
        c = new GridBagConstraints();
        f.setLayout( gridbag );
 
        // Create the canvas panel and put it on top
        canvas = new BWPanel();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        gridbag.setConstraints( canvas, c );
        f.add( canvas );
 
        // Create the control panel and put it second from the bottom
        controls = new BWControls( canvas );
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 0.0;
        gridbag.setConstraints( controls, c );
        f.add( controls );
 
        // Create the dismiss button and put it at the bottom
        dismiss = new Button( "Dismiss" );
        gridbag.setConstraints( dismiss, c );
        f.add( dismiss );
        f.validate();
          f.setVisible(true);
          f.setSize(800,800);
 
          f.setTitle("BWApplication");
          f.validate();
    }
 
    /*--------------------------  action  --------------------------*/
 
    public boolean action( Event event, Object obj )
    {
        if ( event.target == dismiss )
        {
            hide();
                System.exit(0);
            return true;
        }
        return false;
    }
}
 
/*---------------------------  Class BWPanel  ----------------------------*/
 
class BWPanel extends Panel
{
  // Gray range and initial values
  public final int  minColour        =   0;
  public final int  maxColour        = 255;
  public final int  startLeftColour  = 128;   // 50%
  public final int  startRightColour = 128;   // 50%
 
  // Drawing colours -- modified through setLeftGray and setRightGray methods
  private Color  leftColour;
  private Color  rightColour;
 
  // Other drawing parameters -- can be modified directly
  public  final int      minCenterSize =   5;   // percent
  public  final int      maxCenterSize = 100;   // percent
  public        int      centerSize    =  40;   // percent
  public        boolean  showCenter    = true;
  public        boolean  inColour      = false;
 
  // Off-screen drawing buffer
  private Dimension  offDimension;
  private Image      offImage;
  private Graphics   offGraphics = null;
 
    /*-----------------------  constructor  ------------------------*/
 
    BWPanel()
    {
      int  c;
 
        // Create colour objects
        c = startLeftColour;
        leftColour  = new Color( c, c, c );
        c = startRightColour;
        rightColour = new Color( c, c, c );
    }
 
    /*--------------------------  paint  ---------------------------*/
 
    public void paint( Graphics g )
    {
        update( g );
    }
 
    /*--------------------------  update  --------------------------*/
 
    public void update( Graphics g )
    {
       // Dimension  d          = size();
         Dimension d  = getSize();
      int        halfWidth  = d.width / 2;
      int        squareSize;
      int        dx, dy;
 
        // Create off-screen image for double buffering
        if ( ( offGraphics == null ) ||
             ( d.width  != offDimension.width  ) ||
             ( d.height != offDimension.height ) )
        {
            offDimension = d;
            offImage = createImage( d.width, d.height );
            offGraphics = offImage.getGraphics();
        }
 
        // Draw left half background
    if ( inColour )
        offGraphics.setColor( showCenter ? Color.green : leftColour );
    else
        offGraphics.setColor( showCenter ? Color.white : leftColour );
        offGraphics.fillRect( 0, 0, halfWidth, d.height );
 
        // Draw right half background
        offGraphics.setColor( showCenter ? Color.black : rightColour );
        offGraphics.fillRect( halfWidth, 0, halfWidth, d.height );
 
        // Draw centers if requested
        if ( d.height < halfWidth )
            squareSize = ( d.height * centerSize ) / 100;
        else
            squareSize = ( halfWidth * centerSize ) / 100;
        if ( showCenter )
        {
            // Get horizontal and vertical border sizes
            dx = ( halfWidth - squareSize ) / 2;
            dy = ( d.height - squareSize ) / 2;
 
            // Draw blocks in selected colour
            offGraphics.setColor( leftColour );
            offGraphics.fillRect( dx, dy, squareSize, squareSize );
            offGraphics.setColor( rightColour );
            offGraphics.fillRect(
                dx + halfWidth, dy, squareSize, squareSize );
        }
 
        // Draw a black border
        offGraphics.setColor( Color.black );
        offGraphics.drawRect( 0, 0, d.width - 1, d.height - 1 );
 
        // Finally, copy the off-screen image to the screen
        g.drawImage( offImage, 0, 0, this );
    }
 
    /*----------------------  setLeftColour  -----------------------*/
 
    public void setLeftColour( int c )
    {
        if ( inColour )
            leftColour  = new Color( 0, c, 0 );
        else
            leftColour  = new Color( c, c, c );
        update( getGraphics() );
    }
 
    /*----------------------  setRightColour  ----------------------*/
 
    public void setRightColour( int c )
    {
        if ( inColour )
            rightColour = new Color( 0, c, 0 );
        else
            rightColour = new Color( c, c, c );
        update( getGraphics() );
    }
 
    /*------------------------  setCenter  -------------------------*/
 
    public void setCenter( boolean showCenter )
    {
        this.showCenter = showCenter;
        update( getGraphics() );
    }
 
    /*-----------------------  setInColour  -----------------------*/
 
    public void setInColour( boolean inColour )
    {
        this.inColour = inColour;
        setLeftColour( leftColour.getGreen() );
        setRightColour( rightColour.getGreen() );
        update( getGraphics() );
    }
}
 
/*--------------------------  Class BWControls  --------------------------*/
 
class BWControls extends Panel
{
  Scrollbar  leftSlider;     // Controls gray intensity of left side
  Scrollbar  rightSlider;    // Controls gray intensity of right side
  Scrollbar  sizeSlider;     // Controls size of center block
  Checkbox   showCenter;     // Toggles drawing of center blocks
  Checkbox   inColour;       // Toggles drawing between gray and green
  BWPanel    canvas;         // The main drawing canvas
 
    /*-----------------------  constructor  ------------------------*/
 
    BWControls( BWPanel canvas )
    {
      GridBagLayout       gridbag;
      GridBagConstraints  c;
      Panel               boxPanel;
 
        // Record id of drawing canvas
        this.canvas = canvas;
 
        // Use a GridBag to lay things out
        gridbag = new GridBagLayout();
        c = new GridBagConstraints();
        setLayout( gridbag );
 
        // Create sliders and check boxes
        leftSlider =
            new Scrollbar(
                Scrollbar.HORIZONTAL, canvas.startLeftColour,
                ( canvas.maxColour - canvas.minColour ) / 10,
                canvas.minColour, canvas.maxColour );
        rightSlider =
            new Scrollbar(
                Scrollbar.HORIZONTAL, canvas.startRightColour,
                ( canvas.maxColour - canvas.minColour ) / 10,
                canvas.minColour, canvas.maxColour );
        sizeSlider =
            new Scrollbar(
                Scrollbar.HORIZONTAL, canvas.centerSize,
                ( canvas.maxCenterSize - canvas.minCenterSize ) / 10,
                canvas.minCenterSize, canvas.maxCenterSize );
 
        inColour = new Checkbox( "Colour" );
        showCenter = new Checkbox( "Backgrounds only" );
        showCenter.setState( ! canvas.showCenter );
 
        // Add check boxes in double column using a panel container
        boxPanel = new Panel();
        boxPanel.add( inColour );
        boxPanel.add( showCenter );
        addControl( (Component)boxPanel, 1.0, GridBagConstraints.HORIZONTAL,
            GridBagConstraints.REMAINDER, gridbag, c );
 
        // Add labels and sliders
        addControl( (Component)(new Label( "Left intensity", Label.RIGHT )),
            0.0, GridBagConstraints.NONE, 1, gridbag, c );
        addControl( (Component)leftSlider, 1.0, GridBagConstraints.HORIZONTAL,
            GridBagConstraints.REMAINDER, gridbag, c );
        addControl( (Component)(new Label( "Right intensity", Label.RIGHT )),
            0.0, GridBagConstraints.NONE, 1, gridbag, c );
        addControl( (Component)rightSlider, 1.0, GridBagConstraints.HORIZONTAL,
            GridBagConstraints.REMAINDER, gridbag, c );
        addControl( (Component)(new Label( "Center size", Label.RIGHT )),
            0.0, GridBagConstraints.NONE, 1, gridbag, c );
        addControl( (Component)sizeSlider, 1.0, GridBagConstraints.HORIZONTAL,
            GridBagConstraints.REMAINDER, gridbag, c );
    }
 
    /*------------------------  addControl  ------------------------*/
 
    private void
    addControl( Component component, double wx, int fill, int gw,
        GridBagLayout gb, GridBagConstraints c )
    {
        c.weightx   = (float)wx;
        c.weighty   = 1.0;
        c.fill      = fill;
        c.gridwidth = gw;
        gb.setConstraints( component, c );
        add( component );
    }
 
    /*--------------------------  insets  --------------------------*/
 
    public Insets insets()
    {
        return( new Insets( 5, 5, 5, 5 ) );
    }
 
    /*--------------------------  action  --------------------------*/
 
    public boolean action( Event event, Object obj )
    {
        if ( event.target == showCenter )
        {
            // Toggle showCenter flag of canvas
            canvas.setCenter( ! showCenter.getState() );
            return true;
        }
        if ( event.target == inColour )
        {
            // Toggle inColour flag of canvas
            canvas.setInColour( inColour.getState() );
            return true;
        }
        return false;
    }
 
    /*-----------------------  handleEvent  ------------------------*/
 
    public boolean handleEvent( Event event )
    {
        if ( event.target == leftSlider )
        {
            // adjust left colour and redraw
            canvas.setLeftColour( leftSlider.getValue() );
            return true;
        }
        else if ( event.target == rightSlider )
        {
            // adjust right colour and redraw
            canvas.setRightColour( rightSlider.getValue() );
            return true;
        }
        else if ( event.target == sizeSlider )
        {
            // adjust center block size and redraw
            canvas.centerSize = sizeSlider.getValue();
            canvas.update( canvas.getGraphics() );
            return true;
        }
        else
            return super.handleEvent( event );
    }    
}
 
/*------------------------------------------------------------------------*/ | 
Partager