IT_Programming/Java

사용자 정의 마우스 커서 만들기

JJun ™ 2010. 9. 27. 21:32

-----------------------------------------------------------------------------------

 

   걍.. 쓸 일도 있고.. 심심하기도 해서 끄적여 만들어본 예제 코드...

 

 

 

-----------------------------------------------------------------------------------

 

// Ver. Frame


import java.awt.*;
import javax.swing.*;

 

public class Main extends JFrame
{
    private Cursor cursor = null;

    public Main()
    {
        super("사용자 정의 커서");
        init();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 300);
        setVisible(true);
    }
 
    private void init()
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        MediaTracker tracker = new MediaTracker(this);

        Image cursorImage = toolkit.getImage("./bin/images/i_mouse.png");
        tracker.addImage(cursorImage, 0);

      

        try { tracker.waitForID(0); }
        catch (InterruptedException ie) { ie.printStackTrace(); }

 

        try
        {
            Point hotSpot = new Point(1, 1);  
            cursor = toolkit.createCustomCursor(cursorImage, hotSpot, "i_mouse Cursor");
            if(cursor != null)
            {
                setCursor(cursor);
            }
        }
        catch (IndexOutOfBoundsException e)
        {
            e.printStackTrace();
        }
    }

 

    public static void main(String[] args)
    {
        new Main();
    }
}

 

// Ver. Applet


import java.awt.*;
import java.applet.*;

 

public class Main extends Applet!!
{
    private Cursor cursor = null;
 
    public void init()
    {
        // load the image with the Media Tracker
        MediaTracker tracker = new MediaTracker(this);
        Image cursorImage = getImage(getCodeBase(), "images/i_mouse.png");
        tracker.addImage(cursorImage, 0);

   

        try { tracker.waitForID(0); }
        catch (InterruptedException ie)
        {
            ie.printStackTrace();
        }

 

        // get the toolkit for this environment
        Toolkit tookit = getToolkit();

        try
        {
            // this is the x,y coordinates of the image which will actually do the "clicking"
            Point hotSpot = new Point(1, 1);

         

            // create the custom cursor
            cursor = tookit.createCustomCursor(cursorImage, hotSpot, "i_mouse Cursor");
        }
        catch (IndexOutOfBoundsException e)
        {
            e.printStackTrace();
        }

        // set the cursor for this applet!! component
        setCursor(cursor);
    }
}


-----------------------------------------------------------------------------------