public class One {
  public static void main(String[] args) {
    javax.swing.JFrame a = new javax.swing.JFrame(); // source of events
    a.setVisible(true);
    a.setSize(400, 400); 
    a.addMouseMotionListener(new Helper()); // create and attach a listener
  }
}

public class Helper implements java.awt.event.MouseMotionListener {
  public void mouseMoved(java.awt.event.MouseEvent e) {
    System.out.println("The mouse moves, x = " + e.getX()); 
  }
  public void mouseDragged(java.awt.event.MouseEvent e) {
    System.out.println("The mouse is being dragged."); 
  }
}

Next, a timer:

https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

public class Six {
  public static void main(String[] args) {
    javax.swing.Timer t = new javax.swing.Timer(333, new Helper()); // source of events 
    // ... (we can even set an initial delay)
    t.start(); // start the source 
  }
}

public class Helper implements java.awt.event.ActionListener {
  int i = 0; 
  public void actionPerformed(java.awt.event.ActionEvent e) {
    System.out.println( i++ );  
  }
}

The most general of sorting: 

public class Seven {
  public static void main(String[] args) {
    java.util.ArrayList<Student> a = new java.util.ArrayList<Student>(); 
    a.add(new Student("Michael", 17, 3.14)); 
    a.add(new Student("Chris", 16, 3.04)); 
    a.add(new Student("Brian", 15, 3.24)); 
    a.add(new Student("Alex", 14, 3.84)); 
    a.add(new Student("David", 13, 3.64)); 
    a.add(new Student("Patrick", 12, 3.54));
    System.out.println( a ); 
    java.util.Collections.sort( a, new GPAJudge() );
    System.out.println( a ); 
    
  }
}

public class Student {
  String name; 
  int age;
  double gpa; 
  public Student(String name, int age, double gpa) {
    this.name = name;
    this.age = age;
    this.gpa = gpa;
  }
  public String toString() {
    return this.name + "(" + this.age + ", " + this.gpa + ")" ;  
  }
}

public class GPAJudge implements java.util.Comparator<Student> {
  public int compare(Student a, Student b) { 
    if (a.gpa > b.gpa) return -1; 
    else if (a.gpa == b.gpa) return 0;
    else return 1; 
  }
}

Let's say there is a Player (name, toString(), points) class and you have
several such objects in an array list of players with random number of points
(integers) between 12 and 28 points. I want to sort these players. Make the
players with <= 21 points come first. In this category the players with more
points should be listed first. In the the > 21 category the players with less
points come first. How do we do it? 

Wenhao, Nick R, Jiawei T, Daniel daSilva, Yongchao, Mingming, Austin C, Alex N,
Joe P., Carlie B, Xiaohang L, Steven R,. Tyler R, Alan R, Abigail, Patrick G, Kyle L, 
Shayan K, *Ethan L, *Peyton R, Jonah, Jake P, Caleb G, Hongjian, Alex T, Jarod, Akash, 
Robert W, Mateus, Tyler D, Ben L, Clint S, Lucas K, Sam Maginot, Yaxin, Jiawei Z, 
Brent H, Jeremy deF, Likang, Liyuan, Sam Madden, Tim M, Kyle E, Caige W. 

Another type of sorting:

import java.util.*; 

public class Seven {
  public static void main(String[] args) {
    ArrayList<Player> a = new ArrayList<Player>(); 
    a.add(new Player("Laura", 23)); 
    a.add(new Player("Larry", 23)); 
    a.add(new Player("Jay", 16)); 
    a.add(new Player("John", 22)); 
    a.add(new Player("Brian", 21)); 
    a.add(new Player("Alex", 13)); 
    Collections.sort(a ); 
    System.out.println( a ); 
  }
}

import java.util.*; 

public class Player implements Comparable<Player> {
  String name;
  int points; 
  public Player(String name, int points) {
    this.name = name;
    this.points = points; 
  }
  public int compareTo(Player other) {
     if (this.points <= 21 && other.points > 21) return -1; 
     else if (this.points > 21 && other.points <= 21) return 1;
     else if (this.points <= 21 && other.points <= 21) {
       if (this.points > other.points) return -1; 
       else if (this.points == other.points) return this.name.compareTo(other.name); 
       else return 1; 
     } else {
       if (this.points < other.points) return -1; 
       else if (this.points == other.points) return this.name.compareTo(other.name); 
       else return 1;        
     }
  }
  public String toString() {
    return this.name + ":" + this.points;  
  }
}

--