Source Code : Change Cursor in a thread for animation

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Change Cursor in a thread for animation

   

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main implements Runnable, ActionListener {
  private boolean animate;

  private Cursor[] cursors = new Cursor[] { Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR),
      Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR) };

  private JFrame frame;

  public Main(JFrame frame) {
    animate = false;
    this.frame = frame;
  }

  public void run() {
    int count = 0;
    while (animate) {
      try {
        Thread.currentThread().sleep(200);
      } catch (Exception ex) {
      }
      frame.setCursor(cursors[count % cursors.length]);
      count++;
    }
    frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  }

  public void actionPerformed(ActionEvent evt) {
    JButton button = (JButton) evt.getSource();
    if (animate) {
      button.setText("Start Animation");
      animate = false;
    } else {
      animate = true;
      button.setText("Stop Animation");
      new Thread(this).start();
    }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Start Animation");
    button.addActionListener(new Main(frame));
    frame.getContentPane().add(button);
    frame.setSize(300, 300);
    frame.setVisible(true);
  }

  public static void p(String str) {
    System.out.println(str);
  }
}

   
    
    
  

Thank with us