Implement mouseDragged and mouseMove Events

Implement mouseDragged and mouseMove Events

This tip shows you how to implement mouseDragged and mouseMove events for moving components into a frame:

import java.awt.*;import java.awt.event.*;class f extends Frame implements MouseMotionListener,MouseListener{Checkbox CB=new Checkbox("Label",true);Button BP=new Button("BUTTON_1");Button B=new Button("BUTTON");TextField TF=new TextField(10);Panel P=new Panel();List TA=new List();boolean b1=false;int c1=0;int c2=0;public f(String titlu)       {       super(titlu);       }void init()      {      setLayout(null);             TA.addMouseMotionListener(this);      TA.addMouseListener(this);      B.addMouseMotionListener(this);      B.addMouseListener(this);      P.addMouseMotionListener(this);      P.addMouseListener(this);      CB.addMouseMotionListener(this);      CB.addMouseListener(this);      TA.setLocation(50,50);      TA.setSize(100,100);TA.add("List");      B.setLocation(100,150);B.setSize(80,40);      CB.setLocation(120,370);CB.setSize(100,20);      P.setLayout(new BorderLayout());      P.setLocation(100,200);P.setSize(150,150);      P.setBackground(Color.yellow);      P.add(BP,BorderLayout.NORTH);      P.add(TF,BorderLayout.SOUTH);      add(TA);add(B);add(P);add(CB);     setLocation(0,0);	     setSize(400,400);     setVisible(true);     //show();     } //mouseDraggedpublic void mouseDragged(MouseEvent e)             {                         Component C=e.getComponent();             if(b1==false){b1=true;Point p=new                              Point(e.getPoint());c1=p.x;c2=p.y;}                     Point z=new Point(e.getPoint());             Point q=new Point(C.getLocation());                            C.setBounds(q.x+(z.x-c1),q.y+(z.y-c2),C.getSize().                                       width,C.getSize().height);               } //mouseReleasedpublic void mouseReleased(MouseEvent e)              {              b1=false;              }          //mouseEnteredpublic void mouseEntered(MouseEvent e)              {              Cursor c1=new Cursor(Cursor.HAND_CURSOR);                                                  setCursor(c1);              } //mouseExitedpublic void mouseExited(MouseEvent e)              {              Cursor c2=new Cursor(Cursor.DEFAULT_CURSOR);                                                  setCursor(c2);              }               //mouseClickedpublic void mouseClicked(MouseEvent e)              {              Component C=e.getComponent();              System.out.println(C);              }                          //mouseMoved public void mouseMoved(MouseEvent e)              {              Component C=((Component)e.getSource());              java.util.Random r=new java.util.Random();              java.util.Random g=new java.util.Random();              java.util.Random b=new java.util.Random();              int cr=r.nextInt(255);int cg=g.nextInt(255);              int cb=b.nextInt(255);              Color col=new Color(cr,cg,cb);              C.setBackground(col);              } //mousePressedpublic void mousePressed(MouseEvent e){}                              }public class mouse_events{       public static void main(String[] args)             {             f t=new f("MOUSE EVENT");             t.init();             }       }
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing