ColorBufferBrowser.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 package com.generalrobotix.ui.view.vsensor;
00011 
00012 // Java2 SDK
00013 import java.awt.Canvas;
00014 import java.awt.FlowLayout;
00015 import java.awt.Graphics;
00016 import java.awt.Image;
00017 import java.awt.image.ColorModel;
00018 import java.awt.image.MemoryImageSource;
00019 
00020 // Swing
00021 import javax.swing.JFrame;
00022 
00023 // Sun
00024 import com.generalrobotix.ui.view.Grx3DView;
00025 
00031 @SuppressWarnings("serial")
00032 public final class ColorBufferBrowser extends JFrame {
00033         // canvas for drawing image
00034         private CanvasDraw canvas_;
00035 
00036         // working space
00037         private boolean updated_ = false;
00038 
00045         public ColorBufferBrowser( int width, int height, String title ) {
00046                 super(title);
00047 
00048                 canvas_ = new CanvasDraw (
00049                         Grx3DView.graphicsConfiguration,
00050                         width, height
00051                 );
00052                 canvas_.setSize(width, height);
00053                 canvas_.setBackground(java.awt.Color.white);
00054 
00055                 getContentPane().setLayout(new FlowLayout());
00056                 getContentPane().add(canvas_);
00057                 pack();
00058                 setResizable(false);
00059         }
00060 
00066         public void setColorBuffer(int[] colorBuffer) {
00067                 canvas_.setColorBuffer(colorBuffer);
00068                 updated_ = true;
00069                 canvas_.repaint();
00070         }
00071 
00078         @SuppressWarnings("serial")
00079         final class CanvasDraw extends Canvas {
00080                 // raster size
00081                 private int width_;
00082 
00083                 // image source
00084                 private MemoryImageSource       mis_;
00085 
00086                 // color model
00087                 private ColorModel cm_ = ColorModel.getRGBdefault();
00088 
00089                 // color buffer
00090                 private int[]   colorBuffer_;
00091 
00092                 // image
00093                 private Image img_;
00094 
00101                 public CanvasDraw(java.awt.GraphicsConfiguration gconfig, int width, int height) {
00102                         super(gconfig);
00103 
00104                         // raster size
00105                         width_ = width;
00106 
00107                         // create image source
00108                         mis_ = new MemoryImageSource (
00109                                 width,
00110                                 height,
00111                                 null,   // color buffer
00112                                 0,
00113                                 width
00114                         );
00115                 }
00116 
00121                 public void setColorBuffer(int[] colorBuffer) {
00122                         colorBuffer_ = colorBuffer;
00123                         mis_.newPixels(colorBuffer, cm_, 0, width_);
00124                 }
00125 
00130                 public void update(Graphics g) {
00131                         if (updated_)
00132                                 paint(g);
00133                         else
00134                                 return;
00135                 }
00136 
00141                 public void paint(Graphics g) {
00142                         if (!updated_)
00143                                 return;
00144                         updated_ = false;
00145 
00146                         if (colorBuffer_ == null)
00147                                 return;
00148 
00149                         // image
00150                         img_ = createImage(mis_);
00151                         g.drawImage(img_, 0, 0, this);
00152                 }
00153         }
00154 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:15