ComponentColorModel and MemoryImageSource

Discussion in 'Java' started by briberyjoust@gmx.li, Aug 16, 2005.

  1. Guest

    Hi,

    I am trying to do some image visualization with Java. My idea is to use
    a MemoryImageSource and feed it with data from a byte array. For each
    pixel, the byte array has 3 values following each other (one for red,
    green and blue). To define this mapping, I have defined a
    ComponentColorModel.

    The problem is: nothing is shown (only background). For my example
    below, I would expect a red square (red=0xff, green=0, blue=0).

    Thanks for any advice!

    Regards,
    Paul

    -------8<-------------------------------------

    package spiel;

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Transparency;

    import java.awt.Image;

    import java.awt.color.ColorSpace;
    import java.awt.image.ColorModel;
    import java.awt.image.ComponentColorModel;
    import java.awt.image.DataBuffer;
    import java.awt.image.MemoryImageSource;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Example extends JPanel {
    private final Image image;
    private final int width = 800, height = 600;

    public Example() {
    super ();

    // create raw data
    byte[] pixbuf = new byte[width * height * 3];
    for (int i = 0; i < pixbuf.length; i += 3) {
    pixbuf[i + 0] = -128; // red maximum
    pixbuf[i + 1] = 0;
    pixbuf[i + 2] = 0;
    }

    // ColorModel (no alpha channel!)
    ColorModel colorModel = new ComponentColorModel(
    ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8, 8,
    8},
    false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

    // create image out of ColorModel and raw data.
    MemoryImageSource imageSource
    = new MemoryImageSource(width, height, colorModel, pixbuf, 0,
    width * 3);
    image = createImage(imageSource);

    }

    public void paintChildren(Graphics g) {
    super.paintChildren(g);
    g.drawImage(image, 0, 0, width, height, this);
    }

    public Dimension getPreferredSize() {
    return new Dimension(width, height);
    }

    public static void main(String[] args) {
    JFrame frame = new JFrame("View");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new Example());
    frame.pack();
    frame.setVisible(true);
    }
    }
     
    , Aug 16, 2005
    #1
    1. Advertising

  2. jan V Guest

    I'd like to give you a "fix" answer to your problem, but all I can do is
    point you probably the best Java book dealing with Java 2's new
    Graphics/Image internals: Vincent Hardy's "Java 2D API Graphics"

    http://www.phptr.com/bookstore/product.asp?isbn=0130142662&redir=1&rl=1

    I think the knowledge contained in that book should give you the answer to
    your original question.
     
    jan V, Aug 17, 2005
    #2
    1. Advertising

  3. Paul Schwann Guest

    Thanks! I'll try to get a copy of it.

    Paul
     
    Paul Schwann, Aug 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Dameron
    Replies:
    1
    Views:
    620
  2. Timo Nentwig

    J2ME: MemoryImageSource alike?

    Timo Nentwig, Mar 4, 2005, in forum: Java
    Replies:
    17
    Views:
    1,424
    Darryl Pierce
    Mar 22, 2005
  3. JariTapio

    MemoryImageSource and using it...

    JariTapio, May 8, 2005, in forum: Java
    Replies:
    1
    Views:
    4,345
    Thomas Fritsch
    May 9, 2005
  4. Simon Andrews

    Write PNG from MemoryImageSource

    Simon Andrews, Feb 9, 2006, in forum: Java
    Replies:
    8
    Views:
    4,211
    Simon Andrews
    Feb 14, 2006
  5. Dashesy
    Replies:
    0
    Views:
    549
    Dashesy
    May 31, 2006
Loading...

Share This Page