Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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);
    }
}