📄 frameholo.java
字号:
package tr;
import java.util.Random;
import javax.swing.*;
import java.awt.image.*;
import java.awt.*;
import java.util.Vector;
import java.text.DecimalFormat;
/**
* <p>Title: FrameHolo</p>
* <p>Descripton: Calculates the holograms and shows it in a JFrame</p>
* <p>Research Group: Grup de Recerca en Optica Fisica (Universitat de Barcelona)</p>
* @author Encarni Pleguezuelos Aguilera
* @version 1.1
* Copyright (C) 2006 Grup de Recerca en 襭tica F韘ica (Universitat de Barcelona).
* <p>
* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
* <p>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
* <p>
You should have received a copy of the <a href=COPYING.txt>
GNU General Public License</a>
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
public class FrameHolo extends JFrame {
static Vector vecPuntos = new Vector();
static Vector zTrampas = new Vector();
static int ntrampas =0;
long t0,t;
int frames = 0;
double frps = 0.0;
DecimalFormat decFor = new DecimalFormat("#0");
Image img;
BufferedImage bimg;
int nfil,ncol,ndim, randomIndex;
Random generator = new Random();
double x,y;
static double mitadcol,mitadfil,fase,invcol,invfil,dospi,invdospi,factor;
int tmp;
int im[];
MemoryImageSource mis;
JPanel jPanel1 = new JPanel();
static int mapa[] = new int[6284];
static double kk;
int discrim[];
VolatileImage volIm;
static boolean vuela = false;
static double mapaAst[];//noast
static double factorEscX;
static double factorEscY;
double fasetemp;
static int col0 = 512;
static int fil0 = 384;
Point pt;
static int finestra = 1;
static boolean davis = true;
double z = 0.;
Double tempz;
double fasez;
double partx,party;
static double lambda = 0.000532; //mm
static double tampix = 0.019; //19 microns
static double factorz=0;
static double lvortex=0;
static boolean depth_act= false;
static boolean vortex_act = false;
public FrameHolo() throws HeadlessException {
nfil = TRBase.nfil;
ncol = TRBase.ncol;
if(TRBase.big == false){
col0 = ncol;
fil0 = nfil;
}
mitadcol = col0/2.;
mitadfil = fil0/2.;
invfil = factorEscY/fil0;
invcol = factorEscX/col0;
dospi = 2.*Math.PI;
invdospi = 1./dospi;
factor = 255./dospi;
im = new int[nfil*ncol];
t = System.currentTimeMillis();
factorz = tampix*tampix/(1.33*lambda*2.*2.)*0.375*0.375;//lite
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void update(Graphics g){
paint(g);
}
/**
* Generates the hologram with the Random Binary Masks method and displays it.
* This method is called if the Random distrib RadioButton is selected whenever a trap is added or deleted, in response to a mouse click or drag in the Panel or to the depth slider.
* The coordinates of the different traps are used to generate the phase funtions in the different hologram pixels.
* The phase corresponding to a pixel depends on the number of trap, given by the random array calculated in TRBase.generaRandom()
* The aberration is added and then the whole hologram is mapped into the operating modulator curve before displaying the resulting gray levels distribution.
*/
public void creaHolo(){
for (int i = 0; i < col0; i++)
for (int j = 0; j < fil0; j++) {
if(discrim[i+j*col0] == TRBase.jComboBox1.getSelectedIndex()){
randomIndex = discrim[i + j * col0];
x = i - mitadcol;
y = mitadfil - j;
pt = (Point) vecPuntos.elementAt(randomIndex);
partx = x * pt.x * invcol;
party = y * pt.y * invfil;
fase = (partx + party);
//lite
if(depth_act == true){
tempz = (Double) zTrampas.elementAt(randomIndex);
z = tempz.doubleValue() * 1e-6;
fase += factorz * z * (x * x / 2. + y * y);
}
if(vortex_act = true){
fase += lvortex * Math.atan2(Math.sqrt(2) * y, x) * invdospi;
}
//Add aberration correction
fase += mapaAst[i + j * col0] * invdospi;//noast
//Normalize from 0 to 2pi
fase = fase < 0.0 ? dospi * (fase - (int) fase) + dospi :
dospi * (fase - (int) fase);
tmp = mapa[ (int) (fase * 1000)];
//tmp = (int) (255.*fase*invdospi);//nocurva
if(TRBase.big==false)
{
im[i + j * ncol] = (255 << 24) | (tmp << 16) | (tmp << 8) |
tmp;
}else{
im[2 * i + 2 * j * ncol] = (255 << 24) | (tmp << 16) | (tmp << 8) |
tmp;
im[2 * i + 1 + 2 * j * ncol] = (255 << 24) | (tmp << 16) |
(tmp << 8) | tmp;
if ( (2 * j + 1) < nfil) {
im[2 * i + (2 * j + 1) * ncol] = (255 << 24) | (tmp << 16) |
(tmp << 8) | tmp;
im[2 * i + 1 + (2 * j + 1) * ncol] = (255 << 24) | (tmp << 16) |
(tmp << 8) | tmp;
}
}
}
}
}
public void initImage(){
if(bimg == null){
DataBuffer data_buffer = new DataBufferInt (im, ndim);
// Need bit masks for the color bands for the ARGB color model
int [] band_masks = {0xFF0000, 0xFF00, 0xff, 0xff000000};
// Create a WritableRaster that will modify the image
// when the pixels are modified.
WritableRaster write_raster =
Raster.createPackedRaster(data_buffer, ncol, nfil, ncol,
band_masks, null);
// Create a RGB color model
ColorModel color_model = ColorModel.getRGBdefault ();
// Finally, build the image from the
bimg = new BufferedImage (color_model,write_raster,false,null);
}
}
/**
* Generates the hologram with the Gratings and Lenses method and displays it.
* This method is called if the Lenses & gratings RadioButton is selected whenever a trap is added or deleted, in response to a mouse click or drag in the Panel or to the depth slider.
* The hologram is calculated as a sum of the different phase distribution of each trap and then the phase of the resulting distribution is calculated.
* The aberration is added and then the whole hologram is mapped into the operating modulator curve before displaying the resulting gray levels distribution
*/
public void creaHolo(int control){
//Phase and gratings algorithm
double cos,sin;
for (int i = 0; i < col0; i++)
for (int j = 0; j < fil0; j++) {
cos = 0.;
sin = 0.;
fase = 0.;
x = i - mitadcol;
y = mitadfil - j;
for(int n=0;n<vecPuntos.size();n++){
pt = (Point) vecPuntos.elementAt(n);
fase = dospi*(x * pt.x * invcol + y * pt.y * invfil);
if(depth_act == true){
tempz = (Double)zTrampas.elementAt(n);
z = tempz.doubleValue() * 1e-6;
fase += dospi*factorz*z*(x*x/2.+y*y);
}
if(vortex_act == true){
fase += lvortex*Math.atan2(Math.sqrt(2.)*y,x);
}//lite
//fase = dospi*(x * pt.x * invcol + y * pt.y * invfil + factorz*z*(x*x/2.+y*y)+lvortex*Math.atan2(Math.sqrt(2.)*y,x)*invdospi);
cos += Math.cos(fase);
sin += Math.sin(fase);
}
fase = Math.atan2(sin,cos);
//Add aberration correction
fase += mapaAst[i + j * col0];//noast
//Normalization from 0 to 2pi
fase = fase < 0.0 ? (fase + dospi):
(fase - ((int)(fase*invdospi))*dospi);
tmp = mapa[ (int) (fase * 1000)];
if(TRBase.big==false)
{
im[i + j * ncol] = (255 << 24) | (tmp << 16) | (tmp << 8) |
tmp;
}else{
im[2 * i + 2 * j * ncol] = (255 << 24) | (tmp << 16) | (tmp << 8) |
tmp;
im[2 * i + 1 + 2 * j * ncol] = (255 << 24) | (tmp << 16) |
(tmp << 8) | tmp;
if ( (2 * j + 1) < nfil) {
im[2 * i + (2 * j + 1) * ncol] = (255 << 24) | (tmp << 16) |
(tmp << 8) | tmp;
im[2 * i + 1 + (2 * j + 1) * ncol] = (255 << 24) | (tmp << 16) |
(tmp << 8) | tmp;
}
}
}
}
void createBackBuffer() {
if (volIm != null) {
volIm.flush();
volIm = null;
}
volIm = createVolatileImage(getWidth(), getHeight());
}
/**
* Draws a VolatileImage onto the hologram frame. Java class VolatileImage is used to accelerate the displaying process.
*/
public void drawVolatileImage(Image bimg,Graphics g){
if (volIm == null) {
createBackBuffer();
}
do {
// First, we validate the back buffer
int valCode = volIm.validate(getGraphicsConfiguration());
if (valCode == VolatileImage.IMAGE_RESTORED) {
} else if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {
createBackBuffer();
}
// Now we've handled validation, get on with the rendering
//
// rendering to the back buffer:
Graphics gBB = volIm.getGraphics();
gBB.drawImage(bimg,0,0,this);
// copy from the back buffer to the screen
g.drawImage(volIm, 0, 0, this);
// Now we are done; or are we? Check contentsLost() and loop as necessary
} while (volIm.contentsLost());
}
public void paint(Graphics g){
t0 = t;
t = System.currentTimeMillis();
frps = 1e3/(t-t0);
TRBase.labelvel.setText(decFor.format(frps));
if(davis == false){
creaHolo(0);
}else creaHolo();//lite
creaHolo();
if(vuela == false){
g.drawImage(bimg, 0, 0, this);
}else{
drawVolatileImage(bimg,g);
}
}
private void jbInit() throws Exception {
int centrox = 1024+512-ncol/2;
int centroy = 384-nfil/2;
this.setLocation(0,0);
this.setSize(ncol,nfil);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -