General discussion

Locked

Java - Applets

By senbe ·
How to change the mouse pointer in an applet?

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Java - Applets

by senbe In reply to Java - Applets

Point value changed by question poster.

Collapse -

Java - Applets

by bbuscher In reply to Java - Applets

The mouse pointer, or cursor is treated as a part of a java Component class. Component is in the java.awt package, and the superclass of most of the invisible components. So, the change the cursor you use the setCursor() method of the component class. See the java doc online at http://www.javasoft.com/products/jdk/1.2/docs/api/java/awt/Cursor.html

Code as follows:
java.awt.Cursor MyCursor = new Cursor(HAND_CURSOR);
Mycomponent.setCursor(Mycursor);
//now your cursor is a hand :-)

Note: this only has effect when the cursor is over the component, so choose which component you invoke this on accordingly.

Collapse -

Java - Applets

by senbe In reply to Java - Applets

The question was auto-closed by TechRepublic

Collapse -

Java - Applets

by imamuddin In reply to Java - Applets

There are several implementation to change the mouse pointer in Java Applet.

1. You can change the mouse pointer directly by calling method setCursor.

2. You can create your own classes, implement the MouseListener and change the mouse pointerfrom the selected method.

Here is the sample

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

public class MousePointerTest extends Applet {
private myComponent myComp;
private Cursor curAppletArea; public void init() {
setLayout(new FlowLayout());
myComp=new myComponent();
add (myComp);

// change mouse pointer over here

setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}

public void paint(Graphics g) {
g.drawString ("Mouse Pointer Demo",10,10);
}
}

class myComponent extends Canvas implements MouseListener{
private Cursor curInArea;

public myComponent() {
super();
curInArea=new Cursor(Cursor.HAND_CURSOR);
setBounds(5,10,100,100);
addMouseListener(this);
}

public void paint (Graphics g) {
g.drawRect (0,0,getBounds().width-1,getBounds().height-1);
}

// or you can change mouse pointer with the selected event
public void mouseEntered(MouseEvent e) {
this.setCursor (curInArea);
}
public void mouseClicked(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

Collapse -

Java - Applets

by senbe In reply to Java - Applets

The question was auto-closed by TechRepublic

Collapse -

Java - Applets

by senbe In reply to Java - Applets

This question was auto closed due to inactivity

Back to Web Development Forum
6 total posts (Page 1 of 1)  

Related Discussions

Related Forums