//****************************************************************************** // test.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; import java.lang.Math; //============================================================================== // Main Class for applet test // //============================================================================== public class magball1 extends Applet implements Runnable { // THREAD SUPPORT: // m_test is the Thread object for the applet //-------------------------------------------------------------------------- Thread m_test = null; // ANIMATION SUPPORT: // m_Graphics used for storing the applet's Graphics context // m_Images[] the array of Image objects for the animation // m_nCurrImage the index of the next image to be displayed // m_ImgWidth width of each image // m_ImgHeight height of each image // m_fAllLoaded indicates whether all images have been loaded // NUM_IMAGES number of images used in the animation //-------------------------------------------------------------------------- private Graphics m_Graphics; private Image m_Images[]; private int m_nCurrImage; private int m_nImgWidth = 0; private int m_nImgHeight = 0; private int m_sizex = 50; private int m_sizey = 50; private int m_boxx = 580; private int m_boxy = 280; private int m_maxx = 0; private int m_maxy = 0; private int m_minx = 0; private int m_miny = 0; private double m_posx = 50; private double m_posy = 50; private int m_x = 0; private int m_y = 0; private double m_acc = 0; private double m_accx = 0; private double m_accy = 0; private double m_velx = 0; private double m_vely = 0; private double m_px = 50; private double m_py = 50; private boolean m_bounce = false; private boolean m_fAllLoaded = false; private final int NUM_IMAGES = 18; // test Class Constructor //-------------------------------------------------------------------------- public test() { // 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: test\r\n" + "Author: Digre, Per Sigurd\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. //---------------------------------------------------------------------- // Rectangle r = m_Graphics.getClipRect(); // m_boxx = r.width; // m_boxy = r.height; m_maxx = m_boxx - m_sizex / 2 - 1; m_maxy = m_boxy - m_sizey / 2 - 1; m_minx = m_sizex / 2; m_miny = m_sizey / 2; resize(m_boxx,m_boxy); // TODO: Place additional initialization code here } // 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 } // ANIMATION SUPPORT: // Draws the next image, if all images are currently loaded //-------------------------------------------------------------------------- private void displayImage(Graphics g) { if (!m_fAllLoaded) return; // Draw Image in center of applet //---------------------------------------------------------------------- m_px = m_posx; m_py = m_posy; m_bounce = false; m_acc = 500/( (m_posx - m_x)*(m_posx - m_x) + (m_posy - m_y)*(m_posy - m_y) + 10); m_acc = m_acc * m_acc; m_accx = m_acc * (m_posx - m_x); m_accy = m_acc * (m_posy - m_y) + 0.5; m_velx = m_velx + m_accx; if (m_velx > 9) m_velx = 9; if (m_velx < -9) m_velx = -9; m_posx = m_posx + m_velx; if (m_posx < m_minx ) { m_posx = 2 * m_minx - m_posx; m_velx = - m_velx / 1.1; m_bounce = true; } if (m_posx > m_maxx ) { m_posx = 2 * m_maxx - m_posx; m_velx = - m_velx / 1.1; m_bounce = true; } m_vely = m_vely + m_accy; if (m_vely > 9) m_vely = 9; if (m_vely < -9) m_vely = -9; m_posy = m_posy + m_vely; if (m_posy < m_miny ) { m_posy = 2 * m_miny - m_posy; m_vely = - m_vely / 1.1; m_bounce = true; } if (m_posy > m_maxy ) { m_posy = 2 * m_maxy - m_posy; m_vely = - m_vely / 1.1; m_bounce = true; } // BOKS i midten.. // oppe if ((m_posy > (m_boxy - m_sizey - 2) / 2) && (m_posy < (m_boxy - m_sizey - 2) / 2 + 15) && (m_posx > (m_maxx - m_sizex - 3) / 2) && (m_posx < (m_maxx / 2) + m_sizex)) { m_posy = (m_boxy - m_sizey - 2) - m_posy; m_vely = - m_vely / 1.2; if (m_velx * m_velx + m_vely * m_vely < 0.4 && ((m_maxx + m_minx) / 2 - m_posx ) < 10 && ((m_maxx + m_minx) / 2 - m_posx ) > -10 ) m_Graphics.drawString("Gratulerer...", 30, 30); m_bounce = true; } // venstre if ((m_posx > (m_maxx - m_sizex - 3) / 2) && (m_posx < (m_maxx - m_sizex - 3) / 2 + 15) && (m_posy > (m_boxy - m_sizey - 2) / 2)) { m_posx = (m_maxx - m_sizex - 3) - m_posx; m_velx = - m_velx / 1.2; m_bounce = true; } // høyre if ((m_posx < m_maxx / 2 + m_sizex) && (m_posx > m_maxx / 2 + m_sizex - 15) && (m_posy > (m_boxy - m_sizey - 2) / 2)) { m_posx = m_maxx + 2 * m_sizex - m_posx; m_velx = - m_velx / 1.2; m_bounce = true; } if (m_bounce) { if (m_velx > 0) m_velx = m_velx - 0.2; else m_velx = m_velx + 0.2; if (m_vely > 0) m_vely = m_vely - 0.2; else m_vely = m_vely + 0.2; } g.clearRect((int) m_px - m_sizex/2 + 2 ,(int) m_py - m_sizey/2 + 2, 50, 50); g.drawImage(m_Images[m_nCurrImage],(int) m_posx - m_sizex / 2 + 2 ,(int) m_posy - m_sizey / 2 + 2, null); } // test Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { // ANIMATION SUPPORT: // The following code displays a status message until all the // images are loaded. Then it calls displayImage to display the current // image. //---------------------------------------------------------------------- if (m_fAllLoaded) { Rectangle r = g.getClipRect(); g.clearRect(r.x, r.y, r.width, r.height); g.drawLine(1,1,m_boxx,1); g.drawLine(1,1,1,m_boxy); g.drawLine(m_boxx,1,m_boxx,m_boxy); g.drawLine(1,m_boxy,m_maxx / 2,m_boxy); g.drawLine(m_maxx / 2,m_boxy / 2,m_maxx / 2,m_boxy); g.drawLine(m_boxx - m_maxx / 2,m_boxy,m_boxx,m_boxy); g.drawLine(m_boxx - m_maxx / 2,m_boxy / 2,m_boxx - m_maxx / 2,m_boxy); g.drawLine(m_maxx / 2,m_boxy / 2,m_boxx - m_maxx / 2,m_boxy / 2); g.drawString("Få ballen oppå pålen! ",30,18); displayImage(g); } else g.drawString("Vent på lasting av objekter...", 10, 20); // TODO: Place additional applet Paint code here } // 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_test == null) { m_test = new Thread(this); m_test.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_test != null) { m_test.stop(); m_test = 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() { m_nCurrImage = 0; // If re-entering the page, then the images have already been loaded. // m_fAllLoaded == TRUE. //---------------------------------------------------------------------- if (!m_fAllLoaded) { repaint(); m_Graphics = getGraphics(); m_Images = new Image[NUM_IMAGES]; // Load in all the images //------------------------------------------------------------------ MediaTracker tracker = new MediaTracker(this); String strImage; // For each image in the animation, this method first constructs a // string containing the path to the image file; then it begins // loading the image into the m_Images array. Note that the call to // getImage will return before the image is completely loaded. //------------------------------------------------------------------ for (int i = 1; i <= NUM_IMAGES; i++) { // Build path to next image //-------------------------------------------------------------- strImage = "images/img00" + ((i < 10) ? "0" : "") + i + ".gif"; m_Images[i-1] = getImage(getDocumentBase(), strImage); tracker.addImage(m_Images[i-1], 0); } // Wait until all images are fully loaded //------------------------------------------------------------------ try { tracker.waitForAll(); m_fAllLoaded = !tracker.isErrorAny(); } 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 } if (!m_fAllLoaded) { stop(); m_Graphics.drawString("Error loading images!", 10, 40); return; } // Assuming all images are same width and height. //-------------------------------------------------------------- m_nImgWidth = m_Images[0].getWidth(this); m_nImgHeight = m_Images[0].getHeight(this); } repaint(); while (true) { try { // Draw next image in animation //-------------------------------------------------------------- displayImage(m_Graphics); m_nCurrImage++; if (m_nCurrImage == NUM_IMAGES) m_nCurrImage = 0; // TODO: Add additional thread-specific code here Thread.sleep(50); } 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(); } } } // MOUSE SUPPORT: // The mouseDrag() method is called if the mouse cursor moves over the // applet's portion of the screen while the mouse button is being held down. //-------------------------------------------------------------------------- public boolean mouseDrag(Event evt, int x, int y) { // TODO: Place applet mouseDrag code here return true; } // MOUSE SUPPORT: // The mouseMove() method is called if the mouse cursor moves over the // applet's portion of the screen and the mouse button isn't being held down. //-------------------------------------------------------------------------- public boolean mouseMove(Event evt, int x, int y) { // TODO: Place applet mouseMove code here m_x = x; m_y = y; return true; } // TODO: Place additional applet code here }