Problem in java.

Morningdot Hablu

Morningdot Hablu

@morningdot-6Xuj4M Oct 25, 2024
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.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Aug 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
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Aug 2, 2010

    Thanks goyal!!
    It's working now.
    Can you tell me why it is necessary to use all the methods??
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Aug 3, 2010

    Anybody have idea how to do this with Adapter classes.
  • learner

    learner

    @learner-SrHIlM Aug 6, 2010

    Can you please tell me how to compile two user defined classes in different packages on netbeans platform?
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Aug 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.