//****************************************************************************** // fractal.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; //============================================================================== // Main Class for applet fractal // //============================================================================== public class fractal extends Applet implements Runnable { double left[] = {0, -1, -0, -1}; double bottom[] = {0, 0, 0, 0}; double right[] = {10, 1, 0.5, 1}; double top[] = {10, 1, 0.5, 2}; double a[][] = { {0, 0.85, 0.2, -0.15, 0, 0}, {0.5, 0.5, 0.5, 0, 0, 0}, {0, 0.1, 0.42, 0.42, 0, 0}, {0.05, 0.05, 0.46, 0.47, 0.43, 0.42} }; double b[][] = { {0, 0.04, -0.26, 0.28, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, -0.42, 0.42, 0, 0}, {0, 0, -0.32, -0.15, 0.28, 0.26} }; double c[][] = { {0, -0.04, 0.23, 0.26, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0.42, -0.42, 0, 0}, {0, 0, 0.39, 0.17, -0.25, -0.35} }; double d[][] = { {0.16, 0.85, 0.22, 0.24, 0, 0}, {0.5, 0.5, 0.5, 0, 0, 0}, {0.5, 0.1, 0.42, 0.42, 0, 0}, {0.6, -0.5, 0.38, 0.42, 0.45, 0.31} }; double e[][] = { {0, 0, 0, 0, 0, 0}, {0, 1, 0.5, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0} }; double f[][] = { {0, 1.6, 1.6, 0.44, 0, 0}, {0, 0, 0.5, 0, 0, 0}, {0, 0.2, 0.2, 0.2, 0, 0}, {0, 1, 0.6, 1.1, 1, 0.7} }; int n[] = {4, 3, 4, 6}; int m; int i, j, k, r; int ip[] = new int[6]; //int ip[] = new int[n]; int table[] = new int[1000]; //int table[] = new int[m]; double x, y, s, t; double p[] = new double[6]; //double p[] = new double[n]; boolean running = true; boolean cleaning = false; // ÄÁÆ®·Ñ in ÄÞÆ÷³ÍÆ® //-------------------------------------------------------------------------- Button btn_reset; // ¸®¼Â ¹öư Button btn_run; // ½ÇÇà ¹öư int sel = 0; // ÃÊÀ̽º - µ¥ÀÌÅÍ Choice c_sel; // ÀÌÁß ¹öÆÛ¸µÀ¸·Î ±ôºýÀÓ ÁÙÀ̱â //-------------------------------------------------------------------------- Image img_off; Graphics g_off; // THREAD SUPPORT: // m_fractal is the Thread object for the applet //-------------------------------------------------------------------------- Thread m_fractal = null; // fractal Class Constructor //-------------------------------------------------------------------------- public fractal() { // TODO: Add constructor code here } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: fractal\r\n" + "Author: DongJoon. Lee\r\n" + "Created with Microsoft Visual J++ Version 1.0"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- public void init() { // If you use a ResourceWizard-generated "control creator" class to // arrange controls in your applet, you may want to call its // CreateControls() method from within this method. Remove the following // call to resize() before adding the call to CreateControls(); // CreateControls() does its own resizing. //---------------------------------------------------------------------- resize(600, 330); setBackground(Color.white); setForeground(Color.black); setLayout(null); // Á¦ÀÛÀÚ Ç¥½Ã Label lbl_copyright = new Label("Java Designed by LeeDongjoon", Label.RIGHT); add(lbl_copyright); lbl_copyright.reshape(size().width-200, size().height-20, 200, 20); // ¸®¼Â ¹öư btn_reset = new Button("¸®¼Â (Reset)"); add(btn_reset); btn_reset.reshape(0, size().height-20, 100, 20); // ½ÇÇà ¹öư btn_run = new Button("½ÇÇà/ÀϽÃÁ¤Áö (Start/Pause)"); add(btn_run); btn_run.reshape(110, size().height-20, 190, 20); // ÃÊÀ̽º - ÇÁ·¢Å» µ¥ÀÌÅÍ Label lbl_sel = new Label("ÇÁ·¢Å» µ¥ÀÌÅÍ (fractal data)", Label.LEFT); add(lbl_sel); lbl_sel.reshape(0, size().height-70, 170, 20); c_sel = new Choice(); c_sel.addItem("°í»ç¸® (fern)"); c_sel.addItem("»ï°¢Çü (triangle)"); c_sel.addItem("³ª¹«1 (tree1)"); c_sel.addItem("³ª¹«2 (tree2)"); add(c_sel); c_sel.select(sel); c_sel.reshape(0, size().height-50, 170, 20); // º¯¼ö ¸®¼Â reset_vars(); // ÀÌÁß ¹öÆÛ¸µÀ¸·Î ±ôºýÀÓ ÁÙÀ̱â img_off = createImage(size().width, size().height); g_off = img_off.getGraphics(); } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { // TODO: Place applet cleanup code here } // fractal Paint Handler //-------------------------------------------------------------------------- public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g_off.setColor(Color.black); // º¸ÀÌÁö ¾Ê´Â ±×·¡ÇÈ ÄÜÅØ½ºÆ®¸¦ ±ú²ýÀÌ Áö¿î´Ù. if(cleaning) { g_off.clearRect(0, 0, size().width, size().height); cleaning = false; } else for(i = 0; i < 50; i++) { //j = table[(int)(Math.random() / (1. / m + 1))]; j = table[(int)(Math.random() * m)]; t = a[sel][j] * x + b[sel][j] * y + e[sel][j]; y = c[sel][j] * x + d[sel][j] * y + f[sel][j]; x = t; //if(i >= 10) g_off.fillRect(200+(int)(x*400./(right[sel]-left[sel])), 300-(int)(y*300./(top[sel]-bottom[sel])), 1, 1); } // ½ÇÁ¦ µð½ºÇ÷¹ÀÌ¿¡ Àü¼Û g.drawImage(img_off, 0, 0, this); } // The start() method is called when the page containing the applet // first appears on the screen. The AppletWizard's initial implementation // of this method starts execution of the applet's thread. //-------------------------------------------------------------------------- public void start() { if (m_fractal == null) { m_fractal = new Thread(this); m_fractal.start(); } // TODO: Place additional applet start code here } // The stop() method is called when the page containing the applet is // no longer on the screen. The AppletWizard's initial implementation of // this method stops execution of the applet's thread. //-------------------------------------------------------------------------- public void stop() { if (m_fractal != null) { m_fractal.stop(); m_fractal = null; } // TODO: Place additional applet stop code here } // THREAD SUPPORT // The run() method is called when the applet's thread is started. If // your applet performs any ongoing activities without waiting for user // input, the code for implementing that behavior typically goes here. For // example, for an applet that performs animation, the run() method controls // the display of images. //-------------------------------------------------------------------------- public void run() { while (true) { try { if(running) repaint(); // TODO: Add additional thread-specific code here Thread.sleep(1); } catch (InterruptedException e) { // TODO: Place exception-handling code here in case an // InterruptedException is thrown by Thread.sleep(), // meaning that another thread has interrupted this one stop(); } } } // ÇÚµé À̺¥Æ® //-------------------------------------------------------------------------- public boolean action(Event evt, Object what) { // ÃÊÀ̽º if(evt.target == c_sel && evt.id == Event.ACTION_EVENT) { sel = c_sel.getSelectedIndex(); reset_vars(); running = true; cleaning = true; repaint(); return true; } // ¸®¼Â ¹öư if(evt.target == btn_reset && evt.id == Event.ACTION_EVENT) { reset_vars(); running = true; cleaning = true; repaint(); return true; } // ½ÇÇà / ÀÏÁöÁ¤Áö ¹öư if(evt.target == btn_run && evt.id == Event.ACTION_EVENT) { // ½ÇÇà / ÀÏÁöÁ¤Áö running = running? false: true; return true; } return false; } // º¯¼ö ¸®¼Â //-------------------------------------------------------------------------- public void reset_vars() { m = 25 * n[sel]; s = 0; for(i = 0; i < n[sel]; i++) { p[i] = Math.abs(a[sel][i] * d[sel][i] - b[sel][i] * c[sel][i]); s += p[i]; ip[i] = i; } for(i = 0; i < (n[sel]-1); i++) { k = i; for(j = (i+1); j < n[sel]; j++) if(p[j] < p[k]) k = j; t = p[i]; p[i] = p[k]; p[k] = t; r = ip[i]; ip[i] = ip[k]; ip[k] = r; } r = m; for(i = 0; i < n[sel]; i++) { k = (int)(r * p[i] / s + 0.5); s -= p[i]; do { table[--r] = ip[i]; } while(--k > 0); } } }