0% found this document useful (0 votes)
4 views

Practical 11

The document contains two Java programs that demonstrate mouse event handling using AWT. The first program, MouseDemo, draws a pink oval at the mouse click position, while the second program, MouseColor, changes the background color of the window to red when the mouse is dragged and to green when the mouse is moved. Both programs create a window of size 300x300 and implement their respective mouse listeners.

Uploaded by

Vedant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Practical 11

The document contains two Java programs that demonstrate mouse event handling using AWT. The first program, MouseDemo, draws a pink oval at the mouse click position, while the second program, MouseColor, changes the background color of the window to red when the mouse is dragged and to green when the mouse is moved. Both programs create a window of size 300x300 and implement their respective mouse listeners.

Uploaded by

Vedant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical 11

import java.awt.*;
import java.awt.event.*;
public class MouseDemo extends Frame implements MouseListener
{
MouseDemo()
{
addMouseListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
Graphics g=getGraphics();
g.setColor(Color.PINK);
g.fillOval(e.getX(),e.getY(),30,30);
}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public static void main(String [] args)
{
new MouseDemo();
}
}
Output:-
import java.awt.*;
import java.awt.event.*;
public class MouseColor extends Frame implements MouseMotionListener
{
MouseColor()
{
addMouseMotionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
setBackground(Color.RED);
}
public void mouseMoved(MouseEvent e)
{
setBackground(Color.GREEN);
}
public static void main(String [] args)
{
new MouseColor();
}
}
Output:-

You might also like