import java.awt.*; import java.lang.*; public class Xrayb extends java.applet.Applet { int X0=10,Y0=350,H=300,W=450; int MAXATOMS = 5; final int HRange=5,KRange=5; int x[] = new int[MAXATOMS+1]; int y[] = new int[MAXATOMS+1]; int highlight[] = new int[MAXATOMS+1]; int xtest,ytest; int current=1; final int nk=(2*HRange+1)*(2*KRange+1); double h[] = new double[nk+1]; double k[] = new double[nk+1]; double Ire[] = new double[nk+1]; double Iim[] = new double[nk+1]; double Itestre[] = new double[nk+1]; double Itestim[] = new double[nk+1]; double Intensity[] = new double[nk+1]; double testIntensity[] = new double[nk+1]; boolean highlighted = false; boolean isTestAtom = false; boolean newDiffPattern = false; TextField xpos,ypos,message; public void init() { for (int n = 1;n<=MAXATOMS;n++) { x[n] = 0; y[n] = 0; highlight[n] = 0; } for (int f=1;f<=nk;f++) { h[f] = (f-1)/(2*HRange+1) - HRange; k[f] = (f-1)%(2*KRange+1) - KRange; Intensity[f] = 0; } setLayout(new FlowLayout() ); xpos = new TextField(String.valueOf(0),3); ypos = new TextField(String.valueOf(0),3); message = new TextField("",25); add(new Label("x:")); add(xpos); add(new Label("y:")); add(ypos); add(message); add(new Button("Xray")); add(new Button("Restart")); add(new Button("New Xray")); add(new Button("Accept")); add(new Button("Reject")); }//End init void restart(){ current = 1; removeTestAtom(); highlighted = false; isTestAtom = false; newDiffPattern = false; for (int n = 1;n<=MAXATOMS;n++) { x[n] = 0; y[n] = 0; highlight[n] = 0; } for (int f=1;f<=nk;f++) { Intensity[f] = 0; testIntensity[f] = 0; } repaint(); } public boolean action(Event e, Object arg) { if (e.target instanceof Button) { String command = (String)arg; if (command.equals("Xray")) { diffract(); } if (command.equals("Restart")) { restart(); } if (command.equals("New Xray")) { newDiffract(); newDiffPattern = true; } if (command.equals("Accept")) { updateDiffractionPattern(); newDiffPattern = false; } if (command.equals("Reject")) { newDiffPattern = false; } repaint(); } return true; }//End action public boolean mouseDown (Event e, int xin, int yin) { if (current<=MAXATOMS) { if (xin>=X0&&xin<=450&&yin>=Y0-H&&yin<=Y0) { x[current] = -X0+xin; xpos.setText(String.valueOf(x[current])); y[current] = Y0-yin; ypos.setText(String.valueOf(y[current])); current++; message.setText(""); } else { message.setText("Click on the blue area"); } } else { message.setText("All spots entered - now highlighting"); if (highlighted == false && isTestAtom == false) { for (int i = 1;i<=MAXATOMS;i++) highlight[i] = 0; boolean found = locateAtom(xin,yin); if (found == true) { removeTestAtom(); highlighted = true; } } else if (highlighted == true && isTestAtom == false) { addTestAtom(xin,yin); isTestAtom = true; } else if (highlighted == true && isTestAtom == true) { int highAtom = 0; for (int i = 1;i<=MAXATOMS;i++) if (highlight[i] == 1) highAtom = i; removeTestAtom(); boolean found = locateAtom(xin,yin); if (found == true) { highlight[highAtom] = 0 ; highlighted = true; } else { addTestAtom(xin,yin); } } } repaint(); return true; }//End mouseDown void addTestAtom(int xin , int yin) { xtest=xin-X0; ytest=Y0-yin; } void removeTestAtom() { xtest = 0; ytest = 0; } boolean locateAtom(int xin, int yin){ int found = 0; int xspot = xin-X0; int yspot = Y0-yin; for (int i=1; i<=MAXATOMS; i++) { for (int ii=-5;ii<=5;ii++) for (int jj=-5;jj<=5;jj++) if(x[i]+ii==xspot&&y[i]+jj==yspot) { highlight[i] = 1; System.out.println("Spot #"+i+"x = "+xin+"y = "+yin); found = 1; } } if (found == 0) return false; else return true; } void diffract() { double arg; double xmid[] = new double[nk+1]; double ymid[] = new double[nk+1]; int n,f; for(n=1;n<=MAXATOMS;n++) { xmid[n] = (double)(x[n])/450; ymid[n] = (double)(y[n])/300; //System.out.println("xm = "+xmid[n]+"ym = "+ymid[n]); } for(f=1;f<=nk;f++) { Ire[f] = Iim[f] = 0; for(n=1;n<=MAXATOMS;n++) { arg = (double)( 2*3.14159*( h[f]*xmid[n]+k[f]*ymid[n] ) ); Ire[f] += (double)(Math.cos(arg)); Iim[f] += (double)(Math.sin(arg)); Intensity[f] = Ire[f]*Ire[f]+Iim[f]*Iim[f]; } } }//End diffract void newDiffract() { double argnew,argold;//,vol=0; double xold=0,yold=0,xnew=0,ynew=0; int n,f; for(n=1;n<=MAXATOMS;n++) { if (highlight[n] == 1) { xold = (double)(x[n])/450; yold = (double)(y[n])/300; xnew = (double)(xtest)/450; ynew = (double)(ytest)/300; //System.out.println("xm = "+xmid[n]+"ym = "+ymid[n]); } } for(f=1;f<=nk;f++) { argold = (double)( 2*3.14159*( h[f]*xold+k[f]*yold ) ); argnew = (double)( 2*3.14159*( h[f]*xnew+k[f]*ynew ) ); Itestre[f] = Ire[f]+(double)((Math.cos(argnew)-Math.cos(argold)) ); Itestim[f] = Iim[f]+(double)((Math.sin(argnew)-Math.sin(argold)) ); testIntensity[f] = Itestre[f]*Itestre[f]+Itestim[f]*Itestim[f]; } }//End newDiffract void updateDiffractionPattern() { for (int n=1;n<=MAXATOMS;n++) { if (highlight[n] == 1) { System.out.print("Atom # "+n+" updated from "+x[n]+", "+y[n]); x[n] = xtest; y[n] = ytest; removeTestAtom(); System.out.println(" to "+x[n]+", "+y[n]); } } for (int f=1;f<=nk;f++) { Ire[f] = Itestre[f]; Iim[f] = Itestim[f]; Intensity[f] = testIntensity[f]; Itestre[f] = Itestim[f] = testIntensity[f] = 0; } }//End updateDiffractionPattern public void paint(Graphics g) { int xdraw,ydraw; int kxdraw[] = new int[nk+1]; int kydraw[] = new int[nk+1]; int kxndraw[] = new int[nk+1]; int kyndraw[] = new int[nk+1]; //Set up Atomic box and Frequency domain g.setColor(Color.blue); g.fillRect(X0,Y0-H,W,H); g.setColor(Color.cyan); g.fillRect(10,400,450,50); //Add coordinate axes g.setColor(Color.black); g.drawLine(X0,Y0-H,X0,Y0); g.drawLine(X0,Y0,X0+W,Y0); g.drawLine(10,400,10,450); g.drawLine(10,450,460,450); //Add atoms to the field for (int n=1;n<=MAXATOMS;n++) { if(x[n]!=0&&y[n]!=0) { if (highlight[n] == 0) g.setColor(Color.red); else if (highlight[n] == 1) g.setColor(Color.orange); xdraw = X0+x[n]; ydraw = Y0-y[n]; g.fillOval(xdraw-5,ydraw-5,10,10); } } //Add the test spot, if necessary if(xtest!=0&&ytest!=0) { g.setColor(Color.white); xdraw = X0+xtest; ydraw = Y0-ytest; g.fillOval(xdraw-5,ydraw-5,10,10); } //Add the frequency components for (int f=1;f<=nk;f++) { kxdraw[f] = (int)(10+3.75*f); kydraw[f] = (int)((450-2.5*Intensity[f])); } g.setColor(Color.black); for (int f=1;f<=nk-1;f++) { g.drawLine(kxdraw[f],kydraw[f],kxdraw[f+1],kydraw[f+1]); } if(newDiffPattern == true) { for (int f=1;f<=nk;f++) { kxdraw[f] = (int)(10+3.75*f); kydraw[f] = (int)((450-2.5*testIntensity[f])); } g.setColor(Color.red); for (int f=1;f<=nk-1;f++) { g.drawLine(kxdraw[f],kydraw[f],kxdraw[f+1],kydraw[f+1]); } } }//End paint }