JAVA_PRACTICAL11
JAVA_PRACTICAL11
JAVA_PRACTICAL11
Exercise:
Q1.Write a java program to change the background color of frame when user performs
events using Mouse.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true); } @Override
getContentPane().setBackground(Color.YELLOW); }
getContentPane().setBackground(Color.GREEN); }
@Override
getContentPane().setBackground(Color.BLUE);
System.out.println("Blue"); }
@Override
@Override
getContentPane().setBackground(Color.LIGHT_GRAY); }
SwingUtilities.invokeLater(MouseEventA::new); }}
Output:
Q2. write java program to count the number of clickd performed by the user in a frame
window
import javax.swing.*;
import java.awt.event.*;
public class ClickCounter extends JFrame implements MouseListener {private int clickCount = 0;
public ClickCounter() {
setTitle("Click Counter");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label.setFont(label.getFont().deriveFont(18.0f));
add(label);
addMouseListener(this);
setVisible(true); }
@Override
clickCount++;
@Override
@Override
@Override
@Override
SwingUtilities.invokeLater(ClickCounter::new);
}}
Output:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addMouseMotionListener(this);
}@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
} @Override
mouseX = e.getX();
mouseY = e.getY();
repaint(); } @Override
super.paint(g);
g.setColor(Color.BLACK);
g.drawString(eventType + " at (" + mouseX + ", " + mouseY + ")", 50, 50);
new MouseMotionDemo(); }}
Output: