/** * poly.java * * Title: string - NMM * Description: * @author akilian * @version */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.lang.Math.*; import java.util.Vector; import java.io.*; import java.net.*; public class simplex extends java.applet.Applet implements Runnable{ // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated // every time the form is saved. All manual changes will be overwritten. // BEGIN GENERATED CODE // member declarations // END GENERATED CODE boolean isStandalone = false; Dimension size; Image buffer; // The off-screen image for double-buffering Graphics bufferGraphics; // A Graphics object for the buffer Thread animator; // Thread that performs the animation boolean please_stop; int stepper = 0; int timer=0; int maxstepper = 10000; int order = (int)Math.pow(2,12); int last_x=80; int last_y=80; int last_mx; int last_my; int line =0; int xpos=0; int ypos=250; float[][] pt = new float[3][2]; Color[][] colorgrid = new Color[100][100]; private Font font; int div; public simplex() { } // Retrieve the value of an applet parameter public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } // Get info on the applet parameters public String[][] getParameterInfo() { return null; } // Get applet information public String getAppletInfo() { return "Applet Information"; } // Initialize the applet public void init() { try { initComponents(); } catch (Exception e) { e.printStackTrace(); } // double buffering size = this.size(); buffer = this.createImage(size.width, size.height); bufferGraphics = buffer.getGraphics(); please_stop = false; font = new Font("SansSerif", Font.BOLD,10); float[][] tempval=new float[100][100]; float max = -100000000f; float min = 100000000f; for(int j=0;j<100;j++) { for(int i=0;i<100;i++) { tempval[j][i] = func((float)i/5.0f-10,(float)j/5.0f-10); //System.out.println((int)tempval%255); if (maxtempval[j][i]) min=tempval[j][i]; //colorgrid[j][i] = new Color((int)tempval%255,(int)tempval%255,(int)tempval%255); } } float range = max-min; double adjust; for(int j=0;j<100;j++) { for(int i=0;i<100;i++) { //System.out.print(tempval[j][i]+" " ); adjust = (double)tempval[j][i]; adjust= adjust - min; //System.out.print(adjust +" -min "); adjust = adjust /range; //System.out.print(adjust +" -range "); tempval[j][i] = (float)adjust; //System.out.println(tempval[j][i]+" " + range + " " + min); colorgrid[j][i] = new Color(tempval[j][i],tempval[j][i],tempval[j][i]); } } pt[0][0] = -10f; pt[0][1]= -10f; pt[1][0] = -10f; pt[1][1] = -9.1f; pt[2][0] = -9f; pt[2][1] = -10f; this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { last_mx = e.getX(); last_my = e.getY(); } }); // event model 1.1 mouse listener this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { int x = e.getX(), y = e.getY(); } }); } public void initComponents() throws Exception { // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated // every time the form is saved. All manual changes will be overwritten. // BEGIN GENERATED CODE // the following code sets the frame's initial state setLocation(new java.awt.Point(0, 0)); setLayout(null); setSize(new java.awt.Dimension(1000, 500)); // event handling // END GENERATED CODE } public void simplex() { float[] temp=new float[3]; int res=0; float[] trypt = new float[2]; float[] vect = new float[2]; float[] newpt = new float [2]; float curmax=1000f; for(int i=0;i<3;i++) { temp[i] = func(pt[i][0],pt[i][1]); if (temp[i]>=temp[res]) res=i; System.out.println("testloop "+res +" " + temp[i] + " "+ i); } curmax = temp[res]; int right = (res+1)%3; int left = (res-1); if(left<0) left=2; System.out.println(left+" "+right+ " "+ res); float rightval = temp[right]; float leftval = temp[left]; newpt[0] = (pt[right][0]+pt[left][0])/2.0f; newpt[1] = (pt[right][1]+pt[left][1])/2.0f; vect[0] = newpt[0]-pt[res][0]; vect[1] = newpt[1]-pt[res][1]; trypt[0] = newpt[0]+vect[0]; trypt[1] = newpt[1]+vect[1]; boolean notfound = true; //while (notfound) { float tmpval = func(trypt[0],trypt[1]); float tmpval2; System.out.println(" vals " +rightval+" " + leftval + " " + tmpval+ " "); //if (tmpval=leftval) { System.out.println("test6"); pt[right][0] = newpt[0]; pt[right][1] = newpt[1]; pt[res][0] = (pt[res][0]+pt[left][0])/2.0f; pt[res][1] = (pt[res][1]+pt[left][1])/2.0f; notfound = false; } } } } } public float func(float x, float y){ float ret; ret =(float)( 0.2f*((x*x)+(y*y))-2 * Math.exp(-((x*x)+(y*y))) - Math.exp(-((x*x)+((y+4)*(y+4)))/2.0f)); return ret; } public void animate() { simplex(); } public void paint(Graphics screen) { // if(stepper ==0) { int xoff=50; int yoff=50; int scalegr = 4; int scale = 20; int xoffpt = xoff + scale*10; int yoffpt = yoff + scale*10; bufferGraphics.setColor(Color.white); if (stepper == 0) { bufferGraphics.fillRect(0, 0, size.width, size.height); // clear the buffer //} for(int j=0;j<100;j++) { for(int i=0;i<100;i++) { bufferGraphics.setColor(colorgrid[j][i]); bufferGraphics.fillRect(i*scalegr+xoff,j*scalegr+yoff,scalegr,scalegr); } } bufferGraphics.setColor(Color.red); //bufferGraphics.drawRect(xoff,yoff,400,400); bufferGraphics.drawLine(xoff+200,yoff-5,xoff+200,yoff+400+5); bufferGraphics.drawLine(xoff-5,yoff+200,xoff+400+5,yoff+200); } bufferGraphics.setColor(Color.green); bufferGraphics.drawLine((int)(pt[0][0]*scale)+xoffpt,(int)(pt[0][1]*scale)+yoffpt,(int)(pt[1][0]*scale)+xoffpt,(int)(pt[1][1]*scale)+yoffpt); bufferGraphics.drawLine((int)(pt[1][0]*scale)+xoffpt,(int)(pt[1][1]*scale)+yoffpt,(int)(pt[2][0]*scale)+xoffpt,(int)(pt[2][1]*scale)+yoffpt); bufferGraphics.drawLine((int)(pt[2][0]*scale)+xoffpt,(int)(pt[2][1]*scale)+yoffpt,(int)(pt[0][0]*scale)+xoffpt,(int)(pt[0][1]*scale)+yoffpt); bufferGraphics.setColor(Color.black); screen.drawImage(buffer, 0, 0, this); } // this one is for updating during runtime Graphics screeng; public void update() { if (screeng == null) screeng = this.getGraphics(); paint(screeng); } public void run() { while(!please_stop) { while (true){ update(); animate(); stepper +=1f; System.out.println(stepper+" " + pt[0][0]); } } animator = null; } public void start() { if (animator == null) { please_stop = false; animator = new Thread(this); animator.start(); } } // Standard method to stop the applet public void stop() { please_stop = true; } // Standard method to destroy the applet public void destroy() { } // Main entry point when running standalone public static void main(String[] args) { simplex applet = new simplex(); applet.isStandalone = true; Frame frame = new Frame(); frame.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { Frame f = (Frame) e.getSource(); f.setVisible(false); f.dispose(); System.exit(0); } }); frame.setTitle("simplex"); frame.add( applet, BorderLayout.CENTER ); applet.init(); applet.start(); frame.setSize( 1000, 500 ); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation( (d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible( true ); } }