Jump to content

Rotating Cursor With Fading Mouse Trail


Swizzbeat

Recommended Posts

I have a lot of people ask me what the code is for my mouse cursor, so I mine as well post this.

 

MousePathPoint class:

import java.awt.Point;

//CREDITS TO ENFILADE

class MousePathPoint extends Point {

	private long finishTime;
	private double lastingTime;
	private int alpha = 255;

	public MousePathPoint(int x, int y, int lastingTime) {
		super(x, y);
		this.lastingTime = lastingTime;
		finishTime = System.currentTimeMillis() + lastingTime;
	}

        //added by Swizzbeat
	public int getAlpha() {
		int newAlpha = ((int) ((finishTime - System.currentTimeMillis()) / (lastingTime / alpha)));
		if (newAlpha > 255)
			newAlpha = 255;
		if (newAlpha < 0)
			newAlpha = 0;
		return newAlpha;
	}

	public boolean isUp() {
		return System.currentTimeMillis() >= finishTime;
	}

}

Class variables in main class:

private int mX, mY;
private long angle;
private BasicStroke cursorStroke = new BasicStroke(2);
private Color cursorColor = Color.WHITE;
private AffineTransform oldTransform;

Code to put in your onPaint:

Graphics2D g = (Graphics2D) g1;    //replace g1 with Graphics variable name
oldTransform = g.getTransform();
mX = client.getMousePosition().x;
mY = client.getMousePosition().y;

g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));

//MOUSE TRAIL
while (!mousePath.isEmpty() && mousePath.peek().isUp())
	mousePath.remove();
Point clientCursor = client.getMousePosition();
MousePathPoint mpp = new MousePathPoint(clientCursor.x, clientCursor.y, 300);
if (mousePath.isEmpty() || !mousePath.getLast().equals(mpp))
	mousePath.add(mpp);
MousePathPoint lastPoint = null;
for (MousePathPoint a : mousePath) {
	if (lastPoint != null) {
		g.setColor(new Color(255, 255, 255, a.getAlpha()));    //trail color
		g.drawLine(a.x, a.y, lastPoint.x, lastPoint.y);
	}
	lastPoint = a;
		}

if (mX != -1) {
	g.setStroke(cursorStroke);
	g.setColor(cursorColor);
	g.drawLine(mX-3, mY-3, mX+2, mY+2);
	g.drawLine(mX-3, mY+2, mX+2, mY-3);

	g.rotate(Math.toRadians(angle+=6), mX, mY);

	g.draw(new Arc2D.Double(mX-12, mY-12, 24, 24, 330, 60, Arc2D.OPEN));
	g.draw(new Arc2D.Double(mX-12, mY-12, 24, 24, 151, 60, Arc2D.OPEN));

        g.setTransform(oldTransform);
}
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...