/* * df5.java - revised 20 Dec 2010 - width 321, height 241 * @author jackord@kw.igs.net * X-ray diffraction patterns for cubic crystals generated by superimposing * Huygens' wavelets from an nxnxn array of single atoms (SC) or a basis of * two (BCC) or four (FCC) atoms, where n can be scanned from = 2 to 1024 * or set to 256. * The wavelength can be chosen to enhance diffraction from * (111), (201), or (211) planes. */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class df5 extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String tst; String b1s="SC"; Button b1=new Button(b1s); String b2s="BCC"; Button b2=new Button(b2s); String b3s="FCC"; Button b3=new Button(b3s); Choice chla=new Choice(); CheckboxGroup chg=new CheckboxGroup(); Checkbox[] ch=new Checkbox[2]; CheckboxGroup cgg=new CheckboxGroup(); Checkbox[] cg=new Checkbox[2]; Image bim; Graphics bgr; public void init() { add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); add(b3); b3.addActionListener(this); chla.addItem("la=2a/3"); chla.addItem("la=2a/5"); chla.addItem("la=2a/6"); add(chla); chla.select(0); add(ch[0]=new Checkbox("Transmit", chg, true)); add(ch[1]=new Checkbox("Reflect", chg, false)); add(cg[0]=new Checkbox("Scan n", cgg, true)); add(cg[1]=new Checkbox("n=256", cgg, false)); setBackground(new Color(211, 211, 211)); chla.setBackground(getBackground()); ch[0].setBackground(getBackground()); ch[1].setBackground(getBackground()); cg[0].setBackground(getBackground()); cg[1].setBackground(getBackground()); setFont(new Font("TimesRoman", Font.PLAIN, 12)); } public void paint(Graphics g) { // Main Program b1.setBounds(10, 2, 60, 20); b2.setBounds(10, 25, 60, 20); b3.setBounds(10, 48, 60, 20); ch[0].setBounds(5, 73, 70, 20); ch[1].setBounds(5, 98, 70, 20); cg[0].setBounds(5, 123, 70, 20); cg[1].setBounds(5, 148, 70, 20); chla.setBounds(5, 173, 70, 20); int c, di, ni, n1, n2, x0, y0, del; long tt; double a, b, d, z, zz, r0, qr, qi, rr, ri, sr, si, t, sc, sn; double [] la=new double[3]; if (first==0) { bim=createImage(240, 240); bgr=bim.getGraphics(); first=1; } a=3.615E-7; x0=120; y0=120; z=30.; zz=z*z; // Cu lattice parameter la[0]=2*a/3; la[1]=2*a/5; la[2]=2*a/6; double pi=Math.PI; g.setColor(Color.black); // Initial screen g.drawRect(0, 0, 320, 240); if (cg[0].getState()==true) { n1=1; n2=10; } else { n1=8; n2=n1; } if (ch[0].getState()==true) di=1; else di=-1; // Transmit / Reflect if (kk>0) { // Default Simple Cubic for (int n=n1; n<=n2; n=n+1) { for (int ny=-120; ny<=120; ny=ny+1) { // Screen Y loop for (int nx=-120; nx<=120; nx=nx+1) { // Screen X loop r0=Math.sqrt(zz+nx*nx+ny*ny); b=2*pi*a/r0/la[chla.getSelectedIndex()]; sr=1; si=0; ni=1; sn=1.0; if (kk==2) { // Body-Centred Cubic t=b/2*(nx+ny+di*z-r0); rr=Math.cos(t); ri=Math.sin(t); qr=sr+rr*sr-ri*si; si=si+rr*si+ri*sr; sr=qr; } if (kk==4) { // Face-Centred Cubic t=b/2*(nx+ny); rr=Math.cos(t); ri=Math.sin(t); qr=sr+rr*sr-ri*si; qi=si+rr*si+ri*sr; t=b/2*(nx+di*z-r0); rr=Math.cos(t); ri=Math.sin(t); qr=qr+rr*sr-ri*si; qi=qi+rr*si+ri*sr; t=b/2*(ny+di*z-r0); rr=Math.cos(t); ri=Math.sin(t); qr=qr+rr*sr-ri*si; qi=qi+rr*si+ri*sr; sr=qr; si=qi; } for (int i=1; i<=n; i=i+1) { // Array X loop t=b*ni*nx; rr=Math.cos(t); ri=Math.sin(t); qr=sr+rr*sr-ri*si; si=si+rr*si+ri*sr; sr=qr; ni=2*ni; sn=2*sn; } ni=1; // Array Y loop for (int i=1; i<=n; i=i+1) { t=ni*b*ny; rr=Math.cos(t); ri=Math.sin(t); qr=sr+rr*sr-ri*si; si=si+rr*si+ri*sr; sr=qr; ni=2*ni; sn=2*sn; } ni=1; for (int i=1; i<=n; i=i+1) { // Array Z loop t=ni*b*(di*z-r0); rr=Math.cos(t); ri=Math.sin(t); qr=sr+rr*sr-ri*si; si=si+rr*si+ri*sr; sr=qr; ni=2*ni; sn=2*sn; } sc=2550/sn/sn/kk/kk; // Intensity scale c=(int)((sr*sr+si*si)*sc); if (c>255) { c=255; } Color col=new Color(0, c, 0); bgr.setColor(col); bgr.drawLine(x0+nx, y0-ny, x0+nx, y0-ny); // Plot intensity } } bgr.setColor(Color.white); bgr.drawString(tst+" n = "+(int)(Math.pow(2, n)), 5, 235); g.drawImage(bim, 80, 0, null); // Show plot buffer tt=System.currentTimeMillis(); while ((System.currentTimeMillis()-tt)<1000) { } } } kk=0; } public void actionPerformed(ActionEvent e) { // Buttons tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } if (b2s.equals(tst)) { kk=2; } if (b3s.equals(tst)) { kk=4; } repaint(); } }