//This program was part of coursework to //implement and understand and relates to mouse //events and drawing rectangles and lines from one point //to another import java.awt.*;//Point,Graphics import java.awt.event.*;//interface import javax.swing.*;//JPanel,JFrame import java.util.*; public class MyMouseEvents extends JPanel implements MouseMotionListener , MouseListener, ActionListener { final static int max=1000; Point[] p= new Point[max]; int ncount=0; JButton b1 = new JButton("button1"); JButton b2 = new JButton("button2"); public MyMouseEvents(){ addMouseListener(this); addMouseMotionListener(this); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e){ System.out.println("Button pressed"); if(e.getSource()==b1){ System.out.println("Button 1 pressed"); sortx(); } if(e.getSource()==b2){ System.out.println("Button2 pressed"); } } //HERE'S THE SORT CODE public void sortx(){ MyComparatorX com= new MyComparatorX(); Arrays.sort(p,0,ncount,com); repaint(); } private class MyComparatorX implements Comparator{ public final int compare(Point a, Point b){ int x1=a.x; int x2=b.x; return x1-x2; //sorts from right to left } } public void mouseClicked(MouseEvent event){} public void mouseEntered(MouseEvent event){} public void mouseExited(MouseEvent event){} private int buttonpressed; private int index=-1; private int foundIndex=-1; public void mousePressed(MouseEvent e){ buttonpressed=e.getButton(); if(e.getButton()==e.BUTTON3){ncount=0;} else if(e.getButton()==e.BUTTON1){ //add to array store if(ncountminx && p[i].xminy && p[i].y=2){//we must have two points to draw a line int i=0; do{ int j=i+1; do{//note add1 to i g.drawLine(p[i].x,p[i].y, p[j].x,p[j].y); j++; }while(j