Thursday, 14 May 2015

Cursor Control in Java

For some strange reason, the capability to control the cursor is in the Frame class, so when you can control the cursor, the user sees when a frame has focus. The applet in Listing 17.38 enables you to select the cursor from a pop-up menu.

Listing 17.38. Selecting the cursor from a pop-up menu.
import java.awt.*;
import java.applet.Applet;


public class cursor_test extends Applet
{
    a_frame a_window;
    Button b_open, b_close;
    boolean window_visible;

    public void init()
    {
        b_open = new Button("Open Window");
        b_close = new Button("Close Window");
        add(b_open);
        add(b_close); 
        create_dialog("Pick a cursor");
    }

    public a_frame create_dialog(String the_title) {
        //create a new Frame
        a_window = new a_frame(the_title,this);
        //set its size to 300 by 80 pixels
        a_window.resize(300,80); 
        return a_window; 
    }

    public boolean action(Event the_event, Object the_arg) {
        String item_name; 
        //handle mouse clicks on the two buttons
        if(the_event.target instanceof Button) {
            item_name = (String)the_arg;
            if(item_name.equals("Open Window")) {
                if(!window_visible) {
                    a_window.show(); 
                    window_visible = true;
                } 
            } 
            if(item_name.equals("Close Window")) {
                if(window_visible) {
                    a_window.hide(); 
                    window_visible = false;
                } 
            } 
        }
        return true;
    }


}

class a_frame extends Frame {
    Button close_window;
    Choice a_menu;
    //this has to be of type cursor_test not Applet. If you 
    //use Applet instead of cursor_test it 
    //will still compile but you won't be able to use my_
    //parent to access the instance
    //variables of the dialogs applet such as window_visible.
    cursor_test my_parent;

    a_frame(String the_title, cursor_test host) {
        super(the_title); 
        FlowLayout fl; 

        my_parent = host; 
        //have to define a layout manager for the window
        fl = new FlowLayout(); 
        setLayout(fl); 
        close_window = new Button("Close Window");
        add(close_window); 
        a_menu = new Choice(); 
        a_menu.addItem("Crosshair cursor");
        a_menu.addItem("Default Arrow cursor");
        a_menu.addItem("Resize Window to the Right cursor");
        a_menu.addItem("Hand cursor");
        a_menu.addItem("Move Window cursor");
        a_menu.addItem("Resize Window Upwards cursor");
        a_menu.addItem("Resize Window using NorthEast corner cursor");
        a_menu.addItem("Resize Window using NorthWest corner cursor");
        a_menu.addItem("Resize Window Downwards cursor");
        a_menu.addItem("Resize Window using SouthEast corner cursor");
        a_menu.addItem("Resize Window using SouthWest corner cursor");
        a_menu.addItem("Text Editing cursor");
        a_menu.addItem("Resizing Window to the left cursor");
        a_menu.addItem("Hourglass cursor");
        add(a_menu);
    }
    public boolean action(Event an_event, Object arg) {
        String button_name; 
        String the_input; 
        String item_name; 

        if (an_event.target instanceof Button) {
            button_name = (String)arg;
            if(button_name.equals("Close Window")) {
              my_parent.window_visible = false;
                hide(); 
            } 
        }
        if(an_event.target instanceof Choice) {
            item_name = (String)arg;
            if(item_name.equals("Crosshair cursor")){
                setCursor(Frame.CROSSHAIR_CURSOR); 
            } 
            if(item_name.equals("Default Arrow cursor")){
                setCursor(Frame.DEFAULT_CURSOR); 
            } 
            if(item_name.equals("Resize Window to the Right cursor")){
                setCursor(Frame.E_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Hand cursor")){
                setCursor(Frame.HAND_CURSOR); 
            } 
            if(item_name.equals("Move Window cursor")){
                setCursor(Frame.MOVE_CURSOR); 
            } 
            if(item_name.equals("Resize Window Upwards cursor")){
                setCursor(Frame.N_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Resize Window using NorthEast corner cursor")){
                setCursor(Frame.NE_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Resize Window using NorthWest corner cursor")){
                setCursor(Frame.NW_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Resize Window Downwards cursor")){
                setCursor(Frame.S_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Resize Window using SouthEast corner cursor")){
                setCursor(Frame.SE_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Resize Window using SouthWest corner cursor")){
                setCursor(Frame.SW_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Text Editing cursor")){
                setCursor(Frame.TEXT_CURSOR); 
            } 
            if(item_name.equals("Resizing Window to the left cursor")){
                setCursor(Frame.W_RESIZE_CURSOR); 
            } 
            if(item_name.equals("Hourglass cursor")){
                setCursor(Frame.WAIT_CURSOR); 
            } 

        }
        return true;
    }
}

No comments:

Post a Comment