CrazyEngineers
  • Problem in java.

    Updated: Oct 25, 2024
    Views: 1.1K
    Hello guy's,
    Look at this coding in java.
    This will run.
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Mouseapp extends Applet implements MouseListener,MouseMotionListener
    {
    int x,y;
    String msg="";
    public void init()
    {
    addMouseListener(this);
    addMouseMotionListener(this);
    }
    public void mouseEntered(MouseEvent m)
    {
    x=m.getX();
    y=m.getY();
    msg="Mouse Entered";
    repaint();
    }
    public void mouseExited(MouseEvent m)
    {
    x=10;
    y=10;
    msg="Mouse Exited";
    repaint();
    }
    public void mousePressed(MouseEvent m)
    {
    x=m.getX();
    y=m.getY();
    msg="Mouse is pressed";
    repaint();
    }
    public void mouseReleased(MouseEvent m)
    {
    x=m.getX();
    y=m.getY();
    msg="Mouse is Released";
    repaint();
    }
    public void mouseClicked(MouseEvent m)
    {
    x=m.getX();
    y=m.getY();
    showStatus("Mouse is clicked at" + x + "," +y);
    }
    public void mouseDragged(MouseEvent m)
    {
    x=m.getX();
    y=m.getY();
    msg="IMS";
    repaint();
    }
    public void mouseMoved(MouseEvent m)
    {
    x=m.getX();
    y=m.getY();
    showStatus("Mouse is moved at" + x + "," +y);
    }
    public void paint(Graphics g)
    {
    g.drawLine(10,20,x,y);
    }
    }
    But look at this code.
    Here i am not using all mouse listener modules.
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class hp extends Applet implements MouseListener,MouseMotionListener
    {
        int x,y;
        public void init()
        {
            addMouseListener(this);
            addMouseMotionListener(this);
        }
        public void mouseEntered(MouseEvent m)
        {
            x=m.getX();
            y=m.getY();
        }
        public void paint(Graphics g)
            {
                g.drawLine(10,20,x,y);
            }
    }
    Can any one tell me where i am wrong.
    This code will display an error.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberAug 2, 2010

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    /*
     <APPLET CODE="Hp" WIDTH=400 HEIGHT=300>
     </APPLET>
    */
    public class Hp extends Applet implements MouseListener,MouseMotionListener
    {
        int x,y;
        public void init()
        {
            addMouseListener(this);
            addMouseMotionListener(this);
        }
        public void mouseEntered(MouseEvent m)
        {
            x=m.getX();
            y=m.getY();
        }
    public void mouseExited(MouseEvent m)
        {}
    public void mouseReleased(MouseEvent m)
        {}
    public void mousePressed(MouseEvent m)
        {}
    public void mouseDragged(MouseEvent m)
        {}
    
    public void mouseMoved(MouseEvent m)
        {}
    
    public void mouseClicked(MouseEvent m){}
        public void paint(Graphics g)
            {
                g.drawLine(10,20,x,y);
            }
    }
    
    
    
    
    Try this

    save it as "Hp.java"

    and run it using appletviewer Hp.java
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberAug 2, 2010

    Thanks goyal!!
    It's working now.
    Can you tell me why it is necessary to use all the methods??
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberAug 3, 2010

    Anybody have idea how to do this with Adapter classes.
    Are you sure? This action cannot be undone.
    Cancel
  • learner

    MemberAug 6, 2010

    Can you please tell me how to compile two user defined classes in different packages on netbeans platform?
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberAug 7, 2010

    learner
    Can you please tell me how to compile two user defined classes in different packages on netbeans platform?
    Didn't understand what you want to say.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register