Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * @(#)ColorCyclingMemoryImageSource.java  1.1  2010-08-03
   *
   * Copyright (c) 2009-2010 Werner Randelshofer, Goldau, Switzerland.
   * All rights reserved.
   *
   * You may not use, copy or modify this file, except in compliance with the
   * license agreement you entered into with Werner Randelshofer.
   * For details see accompanying license terms.
  */
 package org.monte.media.ilbm;
 
 import java.awt.Point;
ColorCyclingMemoryImageSource.

Author(s):
Werner Randelshofer
Version:
1.1 2010-08-03 Added method putProperties. Added support for blended color cycles.
1.0.1 2010-11-08 Fixed color cycling rate.
1.0 2009-12-17 Created.
 
 
     private int width;
     private int height;
     private ColorModel model;
     private Object pixels;
     private int pixeloffset;
     private int pixelscan;
     private Hashtable properties;
     private ArrayList<ColorCyclecolorCycles = new ArrayList<ColorCycle>();
     private Timer timer;
     private HashSet<ImageConsumerconsumers = new HashSet<ImageConsumer>();
    
Whether color cycling is available.
 
     private boolean isColorCyclingAvailable;
    
Whether color cycling is started.
 
     private boolean isStarted;
    
Whether color cycles are blended.
 
     private boolean isBlendedColorCycling;
     private volatile ColorModel cycledModel;

    
Constructs an ImageProducer object which uses an array of bytes to produce data for an Image object.

Parameters:
w the width of the rectangle of pixels
h the height of the rectangle of pixels
cm the specified ColorModel
pix an array of pixels
off the offset into the array of where to store the first pixel
scan the distance from one row of pixels to the next in the array
See also:
java.awt.Component.createImage(java.awt.image.ImageProducer)
 
     public ColorCyclingMemoryImageSource(int wint hColorModel cm,
             byte[] pixint offint scan) {
         super(whcmpixoffscan);
         initialize(whcm, (Objectpixoffscannew Hashtable());
     }

    
Constructs an ImageProducer object which uses an array of bytes to produce data for an Image object.

Parameters:
w the width of the rectangle of pixels
h the height of the rectangle of pixels
cm the specified ColorModel
pix an array of pixels
off the offset into the array of where to store the first pixel
scan the distance from one row of pixels to the next in the array
props a list of properties that the ImageProducer uses to process an image
See also:
java.awt.Component.createImage(java.awt.image.ImageProducer)
 
     public ColorCyclingMemoryImageSource(int wint hColorModel cm,
             byte[] pixint offint scan,
             Hashtable<?, ?> props) {
         super(whcmpixoffscanprops);
        initialize(whcm, (Objectpixoffscanprops);
    }

    
Constructs an ImageProducer object which uses an array of integers to produce data for an Image object.

Parameters:
w the width of the rectangle of pixels
h the height of the rectangle of pixels
cm the specified ColorModel
pix an array of pixels
off the offset into the array of where to store the first pixel
scan the distance from one row of pixels to the next in the array
See also:
java.awt.Component.createImage(java.awt.image.ImageProducer)
    public ColorCyclingMemoryImageSource(int wint hColorModel cm,
            int[] pixint offint scan) {
        super(whcmpixoffscan);
        initialize(whcm, (Objectpixoffscannull);
    }

    
Constructs an ImageProducer object which uses an array of integers to produce data for an Image object.

Parameters:
w the width of the rectangle of pixels
h the height of the rectangle of pixels
cm the specified ColorModel
pix an array of pixels
off the offset into the array of where to store the first pixel
scan the distance from one row of pixels to the next in the array
props a list of properties that the ImageProducer uses to process an image
See also:
java.awt.Component.createImage(java.awt.image.ImageProducer)
    public ColorCyclingMemoryImageSource(int wint hColorModel cm,
            int[] pixint offint scan,
            Hashtable<?, ?> props) {
        super(whcmpixoffscanprops);
        initialize(whcm, (Objectpixoffscanprops);
    }
    private void initialize(int wint hColorModel cm,
            Object pixint offint scanHashtable props) {
         = w;
         = h;
         = cm;
         = pix;
         = off;
         = scan;
        if (props == null) {
            props = new Hashtable();
        }
         = props;
    }
    public int getWidth() {
        return ;
    }
    public int getHeight() {
        return ;
    }
    public ColorModel getColorModel() {
        return ;
    }
    public Hashtable getProperties() {
        return ;
    }
    @Override
    public synchronized void newPixels(byte[] newpixColorModel newmodel,
            int offsetint scansize) {
        this. = newpix;
        this. = newmodel;
        this. = offset;
        this. = scansize;
        super.newPixels(newpix == null ? newmodel : offsetscansize);
    }

    
Changes to a new int array to hold the pixels for this image. If the animation flag has been turned on through the setAnimated() method, then the new pixels will be immediately delivered to any ImageConsumers that are currently interested in the data for this image.

Parameters:
newpix the new pixel array
newmodel the specified ColorModel
offset the offset into the array
scansize the distance from one row of pixels to the next in the array
See also:
java.awt.image.MemoryImageSource.newPixels(int,int,int,int,boolean)
setAnimated(boolean)
    @Override
    public synchronized void newPixels(int[] newpixColorModel newmodel,
            int offsetint scansize) {
        this. = newpix;
        this. = newmodel;
        this. = offset;
        this. = scansize;
        super.newPixels(newpix == null ? newmodel : offsetscansize);
    }
    public void addColorCycle(ColorCycle cc) {
        .add(cc);
    }
    @Override
    public void addConsumer(ImageConsumer ic) {
        super.addConsumer(ic);
        .add(ic);
        if ( && !.isEmpty()) {
            startAnimationTimer();
        }
    }
    @Override
    public void removeConsumer(ImageConsumer ic) {
        super.removeConsumer(ic);
        .remove(ic);
        if ( && .isEmpty()) {
            stopAnimationTimer();
        }
    }
    @Override
    public void setAnimated(boolean b) {
        super.setAnimated(b);
         = b;
        if ( && !.isEmpty() && ) {
            startAnimationTimer();
        } else {
            stopAnimationTimer();
        }
    }

    
Starts or stops color cycling.
    public void setColorCyclingStarted(boolean b) {
         = b;
        if ( && !.isEmpty() && ) {
            startAnimationTimer();
        } else {
            stopAnimationTimer();
        }
    }

    
Returns true if color cycling is on.
    public boolean isColorCyclingStarted() {
        return ;
    }

    
Starts color cycling.
    public void start() {
        setColorCyclingStarted(true);
    }

    
Stops color cycling.
    public void stop() {
        setColorCyclingStarted(false);
    }
    public boolean isStarted() {
        return isColorCyclingStarted();
    }
    private synchronized void startAnimationTimer() {
        if ( != null) {
            return;
        }
        if ( instanceof IndexColorModel) {
            final IndexColorModel icm = (IndexColorModel;
            final int[] rgbs = new int[icm.getMapSize()];
            icm.getRGBs(rgbs);
            // Calculate the timer delay
            int delay = 1000;
            int i = 0;
            if () {
                for (ColorCycle cc : ) {
                    if (cc.isActive()) {
                        // Note: we divide 1000 by 4
                        // 2 for Nyquist Theorem (double sample rate)
                        // 2 for blending
                        int ccDelay = 1000 / 4 * cc.getTimeScale() / cc.getRate();
                        if (ccDelay < delay) {
                            delay = Math.max(1, ccDelay);
                        }
                    }
                }
                delay = Math.max(delay, 1000 / 60); // throttle at 60 fps
            } else {
                for (ColorCycle cc : ) {
                    if (cc.isActive()) {
                        // Note: we divide 1000 by 2 (=double sampling rate)
                        // because of Nyquist theorem
                        int ccDelay = 1000 / 2 * cc.getTimeScale() / cc.getRate();
                        if (ccDelay < delay) {
                            delay = Math.max(1, ccDelay);
                        }
                    }
                }
            }
             = new Timer(delaynew ActionListener() {
                private int[] previousCycled = new int[rgbs.length];
                private int[] cycled = new int[rgbs.length];
                long startTime = System.currentTimeMillis();
                @Override
                public void actionPerformed(ActionEvent evt) {
                    long now = System.currentTimeMillis();
                    System.arraycopy(rgbs, 0, , 0, rgbs.length);
                    for (ColorCycle cc : ) {
                        cc.doCycle(now - );
                    }
                    // We only fire new pixels, if the cycles have changed
                    if (!Arrays.equals()) {
                        ColorCyclingMemoryImageSource.super.newPixels((byte[]) //
                                 = new IndexColorModel(8, ., 0, false, -1, .),
                                ,
                                );
                    }
                    // swap cycled colors
                    int[] tmp = ;
                     = ;
                     = tmp;
                }
            });
            .setRepeats(true);
            .start();
        }
    }
    private synchronized void stopAnimationTimer() {
        if ( != null) {
            .stop();
             = null;
             = null;
            // Reset colors to their initial state
            ColorCyclingMemoryImageSource.super.newPixels((byte[]) //
                    ,
                    ,
                    );
        }
    }

    
Creates a BufferedImage which shares its pixel data with this memory image source.
    public BufferedImage toBufferedImage() {
        DataBuffer buf = ( instanceof byte[]) ?//
                new DataBufferByte((byte[])  * ) ://
                new DataBufferInt((int[])  * );
        WritableRaster raster = Raster.createWritableRaster(smbufnew Point());
        return new BufferedImage(rasterfalse);
    }
    public boolean isColorCyclingAvailable() {
        return ;
    }
    @SuppressWarnings("unchecked")
    public void putProperties(Hashtable props) {
        .putAll(props);
    }
    public void setBlendedColorCycling(boolean newValue) {
         = newValue;
        for (ColorCycle cc : ) {
            cc.setBlended(newValue);
        }
        if (isStarted()) {
            stop();
            start();
        }
    }
    public boolean isBlendedColorCycling() {
        return ;
    }
New to GrepCode? Check out our FAQ X