⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 panelcoord.java

📁 科学计算程序 (用于光学镊子
💻 JAVA
字号:
package tr;

import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import java.awt.image.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JFrame;


/**
 * <p>Title: PanelCoord </p>
 * <p>Description: An event handler Jpanel to click and drag traps</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 PanelCoord extends JPanel{

  static int tipoPaint = 0;
  Point pt;
  static int indexSel;
  static FrameHolo frholo;
  int randomIndex, dim0 = 512*384;
  Random generator = new Random();
  static String nom_dll;
  int dimx = 696;
  int dimy = 520;
  //Comment if there is no QICam available
  /*int dcam = dimx * dimy;
  int imagen[] = new int[dcam];
  byte pix[] = new byte[dcam];*/
  Image bimg;
  int inicio = 5;
  int ptx, pty;
  Graphics g;


  //CamThread hilo;//Comment if there is no QICam available

  public PanelCoord() {

    //hilo =  new CamThread();//Comment if there is no QICam available

    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    //hilo.start();

  }

  //Comment if there is no QICam available
  //The next methods call to C functions
  /*public native int initCamera();

  public native int initGrab();

  public native int doGrab(byte[] pix);

  public native int StopGrab();

  //Load the library JNIQCam
  static {
    try {
      System.load(
          "C:/QCamlib/JNIQCam.dll");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }*/

  public void update(Graphics g) {
    paint(g);
  }


  public void paint(Graphics g) {



    g.setColor(Color.black);
    g.fillRect(0, 0, dimx, dimy);

    for (int i = 0; i < FrameHolo.ntrampas; i++) {
      pt = (Point)FrameHolo.vecPuntos.elementAt(i);
      ptx = pt.x + dimx / 2;
      pty = pt.y + dimy / 2;
      g.setColor(Color.green);
      g.drawOval(ptx-5, pty-5, 10, 10);
    }
    if (TRBase.jComboBox1.getSelectedIndex() >= 0) {
      g.setColor(Color.red);
      pt = (Point)FrameHolo.vecPuntos.elementAt(TRBase.jComboBox1.getSelectedIndex());
      ptx = pt.x + dimx / 2;
      pty = pt.y + dimy / 2;
      g.drawOval(ptx-5, pty-5, 10, 10);
    }
    g.setColor(Color.blue);
    g.fillOval(dimx/2-1,dimy/2-1,3,3);


  }

  //Comment if there is no QICam available
  /*public void creaImagen() {
    doGrab(pix);
    for (int i = 0; i < (dcam); i++) {
      imagen[i] = (int) pix[i] + 256;
      imagen[i] = (255 << 24) | (imagen[i] << 16) | (imagen[i] << 8) | imagen[i];
    }


    if(bimg == null){

     DataBuffer data_buffer = new DataBufferInt (imagen, dcam);

     // 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, dimx, dimy, dimx,
                                        band_masks, null);

         // Create a RGB color model
         ColorModel color_model = ColorModel.getRGBdefault ();

         // Finally, build the image
         bimg = new BufferedImage (color_model,write_raster,false,null);


       }



  }*/


  private void jbInit() throws Exception {
    this.setSize(512, 512);
    this.addMouseMotionListener(new PanelCoord_this_mouseMotionAdapter(this));
    this.addMouseListener(new PanelCoord_this_mouseAdapter(this));
  }

  void this_mouseClicked(MouseEvent e) {
    FrameHolo.vuela = false;
    double zeta = 0.;
    Double z= new Double(zeta);
    if (TRBase.fileOpen == true){
      if (frholo == null) {
        frholo = new FrameHolo();
        frholo.initImage();
        frholo.setVisible(true);
        TRBase.zSlider.setEnabled(true);//lite
      }
      pt = e.getPoint();
      pt.x = pt.x - dimx / 2;
      pt.y = pt.y - dimy / 2;
      frholo.vecPuntos.addElement(pt);
      frholo.zTrampas.addElement(z);
      indexSel = FrameHolo.ntrampas;
      TRBase.jComboBox1.addItem(Integer.toString(frholo.ntrampas));
      TRBase.jComboBox1.setSelectedIndex(frholo.ntrampas);
      FrameHolo.ntrampas = frholo.vecPuntos.size();
      if (frholo.discrim == null) {
        if(TRBase.big == true) dim0 = 512*384;
        else dim0 = TRBase.ncol * TRBase.nfil;
        frholo.discrim = new int[dim0];
      }
      else {
        generaRandom();
      }
      Graphics gr = frholo.getGraphics();
      frholo.update(gr);

    }else{
      JFrame fr = new JFrame();
      Object[] options = {"OK"};
      JOptionPane hola = new JOptionPane("A phase map and a aberration correction have to be selected, press Init",JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION,null,options);
      JDialog dialog = hola.createDialog(fr, "");
      dialog.setVisible(true);
    }



  }

  /**
   * Generates the random binary masks whenever a trap is added or deleted.
   * Uses the Java Random class to generate a Row x Column array of random numbers from 0 to N-1,
   * being N the number of traps. The numbers indicate whose trap phase will display the hologram in each array coordinate
   */

  public void generaRandom(){

    for (int i = 0; i < dim0; i++) {
          frholo.discrim[i] = generator.nextInt(FrameHolo.ntrampas);
    }

  }

  void this_mouseDragged(MouseEvent e) {
    FrameHolo.vuela = true;
    pt = e.getPoint();
    pt.x = pt.x - dimx / 2;
    pt.y = pt.y - dimy / 2;
    FrameHolo.vecPuntos.setElementAt(pt,indexSel);
    Graphics gr = frholo.getGraphics();
    frholo.update(gr);
    repaint();//Instead of CamThread used to display the camera image
  }

  void this_mouseReleased(MouseEvent e) {
    FrameHolo.vuela = false;
  }

  /**
   * Removes a trap. Recalculates the binary masks by calling to generaRandom() and recalculates
   * the hologram in order to display N-1 traps.
   */

  public void quitaTrampa(){
    FrameHolo.vuela = false;
    if (TRBase.fileOpen == true){
      int sel = TRBase.jComboBox1.getSelectedIndex();
      TRBase.jComboBox1.removeItemAt(sel);
      frholo.vecPuntos.removeElementAt(sel);
      frholo.zTrampas.removeElementAt(sel);
      frholo.ntrampas = frholo.vecPuntos.size();
      sel = TRBase.jComboBox1.getItemCount();
      for(int i = 0; i< sel; i++){
        TRBase.jComboBox1.removeItemAt(i);
        TRBase.jComboBox1.insertItemAt(Integer.toString(i),i);
      }
      if(sel!=0)TRBase.jComboBox1.setSelectedIndex(0);
      generaRandom();
      Graphics gr = frholo.getGraphics();
      frholo.update(gr);

    }else{
      Frame fr = new Frame();
      Object[] options = {"OK"};
      JOptionPane hola = new JOptionPane("A phase map and a aberration correction have to be selected, press Init",JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION,null,options);
      JDialog dialog = hola.createDialog(fr, "");
      dialog.setVisible(true);
    }


  }


  //Comment if there is no QICam available
  /*class CamThread extends Thread{


    public void run(){
      try{
        this.sleep(1000);
      }catch(InterruptedException ie){}
      while (true) {
        //Comment if there is no QICam available
        //creaImagen();
        repaint();
      }
  }

  }*/

}

class PanelCoord_this_mouseAdapter extends java.awt.event.MouseAdapter {
  PanelCoord adaptee;

  PanelCoord_this_mouseAdapter(PanelCoord adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseClicked(MouseEvent e) {
    adaptee.this_mouseClicked(e);
  }
  public void mouseReleased(MouseEvent e) {
    adaptee.this_mouseReleased(e);
  }
}

class PanelCoord_this_mouseMotionAdapter extends java.awt.event.MouseMotionAdapter {
  PanelCoord adaptee;

  PanelCoord_this_mouseMotionAdapter(PanelCoord adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseDragged(MouseEvent e) {
    adaptee.this_mouseDragged(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -