Results 1 to 6 of 6

Thread: Applet question....code posted

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts

    Applet question....code posted

    Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    /*
     * DistanceApplet.java
     *
     * Created on July 7, 2005, 11:49 AM
     *
     * To change this template, choose Tools | Options and locate the template under
     * the Source Creation and Management node. Right-click the template and choose
     * Open. You can then make changes to the template in the Source Editor.
     */
    
    /**
     *
     * @author Neil
     */
    public class DistanceApplet extends javax.swing.JApplet implements MouseListener, MouseMotionListener
    {
        
        int x1, y1;   // Coords of mouse press
        int x2, y2;   // Coords of mouse release    
        double dist;    // The distance between the 2 points
        boolean dragging;      // Set to true when a drag is in progress.
        
        
        /*
         * Initialize the applet with certain parameters
         */
        public void init()
        {
            setBackground(Color.lightGray);
            addMouseListener(this);
            addMouseMotionListener(this);
        }
        
        /*
         * Paint function for applet
         */
        public void paint(Graphics g)
        {
            // Draw a black frame around the applet.
            g.setColor(Color.black);
            g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
        }
        
        /*
         * This code deals with retrieving the coordinates where the user first pressed the mouse button
         */
        public void mousePressed(MouseEvent evt)
        {
            if (dragging)  // Exit if a drag is already in progress.
                return;
            
            // Location where user clicked.
            x1 = evt.getX();
            y1 = evt.getY();
            
            // Dragging starts when user presses the mouse button
            dragging = true;
            
        }
        
        /*
         * This code deals with retrieving coordinates where the user let go of the mouse and calls the function to calculate the distance
         */
        public void mouseReleased(MouseEvent evt)
        {
            // Dragging stops when user releases the mouse button.
            dragging = false;
            
            // Location where user clicked.
            x2 = evt.getX();
            y2 = evt.getY();
            
            // Call private function that will calculate the distance between the two points and display it to the user
            calculateDistance(x1, y1, x2, y2);
        }
        
        private void calculateDistance(int a1, int b1, int a2, int b2)
        {
            dist = Math.sqrt(Math.pow((a1 - a2), 2) + Math.pow((b1 - b2), 2));
            
        }
        
        public void mouseDragged(MouseEvent evt)
        {
            repaint();
        }
    
        public void mouseClicked(MouseEvent mouseEvent)
        {
        }
    
        public void mouseEntered(MouseEvent mouseEvent)
        {
        }
    
        public void mouseExited(MouseEvent mouseEvent)
        {
        }
    
        public void mouseMoved(MouseEvent mouseEvent)
        {
        }
    }
    So a little background. Been learning how to write an applet, want this one to calculate and display the distance between 2 points which the user selects anywhere on the applet...all the user does is press...and that registers one point and then the user drags the mouse along to any old point int the applet and then when he/she lets go of the mouse button, thats the second point that is registered and the applet will calculate and spit back the result at you. Now my problem is how do I display the result in an applet? I used JApplet to extend my applet class so tha means I can use Swing API along with the applet. Any pointers would be greatly appreciated.

    Thanks in advance.

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    Oh another thing is that I just tried running the program and I used:
    Code:
    System.out.println(dist)
    and though it prints out the values fine..its printed out in the command prompt, or in Netbeans it is printed out in the IDE itself...but in the applet, since it is running in the browser, I cant think of any other way...to display the results.

  3. #3
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    g.drawString


    Seriously, dosn't your IDE have code completion? Just type in "g" and you'll have a list of attributable fields and methods, along with return and parameter types.
    Last edited by yamangman; 07-07-2005 at 09:23 PM.
    To err is human. To really foul things up ... you need a computer.

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    Nice thanks a lot!

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    Alright another question is then once I have the string drawn onto the applet itself, how do I go about erasing it and writing a fresh new string because if the user tries to computer another distance between 2 points then it overwrites on the string that is drawn on the screen and becomes illegible. I dont know much about applets but I tried to call the paint method in the code once again to clear out the screen but I dont think it worked. Do I have to call init all over again?

  6. #6
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    http://java.sun.com/j2se/1.4.2/docs/.../Graphics.html

    Specifically:

    Quote Originally Posted by The Java API
    If you draw a figure that covers a given rectangle, that figure occupies one extra row of pixels on the right and bottom edges as compared to filling a figure that is bounded by that same rectangle.
    Try giving your drawString method specific coordinates. A further suggestion would be to bookmark:

    http://java.sun.com/j2se/1.4.2/docs/api/
    To err is human. To really foul things up ... you need a computer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sony CDX-M650 Car Stereo Code
    By ikonia in forum Consumer Electronics
    Replies: 4
    Last Post: 16-10-2005, 06:19 PM
  2. Java constructor question
    By suryad in forum Software
    Replies: 9
    Last Post: 03-05-2005, 08:23 AM
  3. Linux code help!
    By nvening in forum Software
    Replies: 18
    Last Post: 26-02-2005, 12:30 AM
  4. lxdirect code question
    By zixiazhang in forum Retail Therapy and Bargains
    Replies: 3
    Last Post: 31-10-2004, 07:50 PM
  5. Stepping code question
    By Knoxville in forum PC Hardware and Components
    Replies: 6
    Last Post: 04-09-2003, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •