/* * dif4x.java - revised 31 Jan 07 - width 301, height 241 * @author jackord@kw.igs.net * The diffraction pattern from an array in the form of a cube * or close-packed rhomb * The 41x41 2-D arrays can be assembled into stacks of 1, 3, 11, or 41 * layers (41 layers are required to make a complete cube or rhomb) * Wavelengths are chosen to enhance diffraction from (111) planes in the * simple cube and from (200) planes in the rhomb (fcc lattice) * */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class dif4x extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String b1s="Cube"; Button b1=new Button(b1s); String b2s="Rhomb"; Button b2=new Button(b2s); Choice chnp=new Choice(); public void init() { add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); setBackground(new Color(211, 211, 211)); chnp.setBackground(getBackground()); setFont(new Font("Helvetica", Font.PLAIN, 12)); chnp.addItem("N=1"); chnp.addItem("N=3"); chnp.addItem("N=11"); chnp.addItem("N=41"); add(chnp); chnp.select(0); } public void paint(Graphics g) { // Main Program if (first==0) { b1.setBounds(5, 5, 50, 20); b2.setBounds(5, 30, 50, 20); chnp.setBounds(3, 55, 57, 4); first=1; } double b, by, dd, z, zz, la, r0, qr, qi, rr, ri, sr, si, t, sc; int c, n, nm, np, x0, y0; int [] nl=new int[4]; nl[0]=0; nl[1]=1; nl[2]=5; nl[3]=20; np=nl[chnp.getSelectedIndex()]; double pi=Math.PI; g.setColor(Color.black); // Initial screen g.drawRect(0, 0, 300, 240); n=20; // 41x41 2-D array dd=2.55E-7; x0=180; y0=120; z=30.; zz=z*z; long tt=System.currentTimeMillis(); if (kk==1) { // Cube nm=(2*n+1)*(2*n+1)*(2*np+1); sc=255000./nm/nm; la=2*dd/3; for (int ny=0; ny<=119; ny=ny+1) { // Screen Y loop for (int nx=0; nx<=119; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx+ny*ny); sr=0; si=0; b=2*pi*dd/r0/la; for (int i=-n; i<=n; i=i+1) { // X loop Algorithm D t=b*i*nx; sr=sr+Math.cos(t); si=si+Math.sin(t); } qr=sr; qi=si; for (int i=-n; i<=n; i=i+1) { // Y loop Algorithm D if (i==0) { i=i+1; } rr=Math.cos(i*b*ny); ri=Math.sin(i*b*ny); qr=qr+rr*sr-ri*si; qi=qi+rr*si+ri*sr; } if (np>0) { sr=qr; si=qi; for (int i=-np; i<=np; i=i+1) { // Z loop Algorithm D if (i==0) { i=i+1; } rr=Math.cos(i*b*(z-r0)); ri=Math.sin(i*b*(z-r0)); qr=qr+rr*sr-ri*si; qi=qi+rr*si+ri*sr; } } c=(int)((qr*qr+qi*qi)*sc); if (c>255) { c=255; } Color col=new Color(c, 0, 0); g.setColor(col); g.drawLine(x0+nx, y0+ny, x0+nx, y0+ny); // Plot intensity g.drawLine(x0-nx, y0+ny, x0-nx, y0+ny); // assuming symmetry g.drawLine(x0+nx, y0-ny, x0+nx, y0-ny); g.drawLine(x0-nx, y0-ny, x0-nx, y0-ny); } } g.setColor(Color.black); g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 5, 235); } if (kk==2) { // Close-packed Rhomb nm=(2*n+1)*(2*n+1)*(2*np+1); sc=255000./nm/nm; la=.8165*dd; for (int ny=-119; ny<=119; ny=ny+1) { // Screen Y loop for (int nx=-119; nx<=119; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx+ny*ny); sr=0; si=0; b=2*pi*dd/r0/la; by=b*Math.sqrt(3)/2; for (int i=-n; i<=n; i=i+1) { // X loop Algorithm D t=b*i*nx; sr=sr+Math.cos(t); si=si+Math.sin(t); } qr=sr; qi=si; for (int i=-n; i<=n; i=i+1) { // Y loop Algorithm D if (i==0) { i=i+1; } t=i*b*nx/2+i*by*ny; rr=Math.cos(t); ri=Math.sin(t); qr=qr+rr*sr-ri*si; qi=qi+rr*si+ri*sr; } if (np>0) { sr=qr; si=qi; for (int i=-np; i<=np; i=i+1) { // Z loop Algorithm D if (i==0) { i=i+1; } t=i*b*nx/2+i*by*ny/3+i*b*(z-r0)/Math.sqrt(1.5); rr=Math.cos(t); ri=Math.sin(t); qr=qr+rr*sr-ri*si; qi=qi+rr*si+ri*sr; } } c=(int)((qr*qr+qi*qi)*sc); if (c>255) { c=255; } Color col=new Color(c, 0, 0); g.setColor(col); g.drawLine(x0+nx, y0-ny, x0+nx, y0-ny); // Plot intensity } } g.setColor(Color.black); g.drawString(""+(System.currentTimeMillis()-tt)+" ms", 5, 235); } kk=0; } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } if (b2s.equals(tst)) { kk=2; } repaint(); } }