Ajp Prac 11
Ajp Prac 11
11
Program code: Debug the following code and write the output.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Label l;
setLayout(null);
l.setBounds(50,150,200,100);
add(l);
l.setText("Mouse Entered");
l.setText("Mouse Exited");
Output:
Exercise:
1) Write a program to change the background color of Applet when user performs events
using Mouse.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Label l;
setLayout(null);
l=new Label();
l.setBounds(50,150,200,100);
add(l);
addMouseListener(this);
l.setText("MOUSE PRESSED");
setBackground(Color.red);
repaint();
}
public void mouseReleased(MouseEvent e)
l.setText("MOUSE RELEASED");
setBackground(Color.pink);
repaint();
l.setText("MOUSE ENTERED");
setBackground(Color.yellow);
repaint();
l.setText("MOUSE EXITED");
setBackground(Color.green);
repaint();
l.setText("MOUSE CLICKED");
setBackground(Color.blue);
repaint();
}
}
Output:
2) Write a program to count the number of clicks performed by the user in a
Frame Window.
import java.awt.*;
import java.awt.event.*;
Label l;
saee_11_3()
f.setVisible(true);
f.setSize(500,500);
f.setLayout(new FlowLayout());
l=new Label();
f.add(l);
f.addMouseListener(this);
{}
{}
{
l.setText("Number of clicks:"+ me.getClickCount());
{}
{}
Output:
3) Write a program to demonstrate the use of mouseDragged and
mouseMoved method of MouseMotionlListener.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
Label l;
l=new Label();
add(l);
addMouseMotionListener(this);
l.setText("X:"+me.getX()+","+"Y:"+me.getY());
l.setText("X:"+me.getX()+","+"Y:"+me.getY());
Graphics g=getGraphics();
g.setColor(Color.blue);
g.fillOval(me.getX(),me.getY(),5,5);
Output: