Announcement

Collapse
No announcement yet.

Farbe des Cursors ändern

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Farbe des Cursors ändern

    Hallo,

    gibt es in Java-AWT eine Möglichkeit, die Farbe eines Cursors zu ändern? Ich soll das Fadenkreuz, also CROSSHAIR, rot färben.

    Schon mal vielen Dank.

    Christopher

  • #2
    Hallo Christopher,

    unten ein Beispiel...

    Geht auch mit reinen AWT-Komponenten.
    Das Image für den Cursor kannst du auch aus einer Datei laden via Toolkit.

    Gruß
    Frank

    <code>
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;

    public class CursorDemo {
    &nbsp;&nbsp;&nbsp;public static void main(String[] args) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Toolkit toolkit = Toolkit.getDefaultToolkit();

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dimension size = toolkit.getBestCursorSize(10, 10);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Point hotSpot = new Point(size.width / 2, size.height / 2);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedIma ge image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Graphics g = image.getGraphics();
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.setColor(Col or.red);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.drawLine(siz e.width / 2, 0, size.width / 2, size.height);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.drawLine( 0, size.height / 2, size.width, size.height / 2);

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "red crosshair cursor");

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JLabel cursorLabel = new JLabel("--- cursorLabel ---");
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cursorLabel.se tHorizontalAlignment(SwingConstants.CENTER);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cursorLabel.se tVerticalAlignment(SwingConstants.CENTER);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cursorLabel.se tPreferredSize(new Dimension(200,200));
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cursorLabel.se tCursor(cursor);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JLabel defaultLabel = new JLabel("--- defaultLabel ---");
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultLabel.s etHorizontalAlignment(SwingConstants.CENTER);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultLabel.s etVerticalAlignment(SwingConstants.CENTER);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaultLabel.s etPreferredSize(new Dimension(200,200));
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JFrame frame = new JFrame("CursorDemo");
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.setDefau ltCloseOperation(JFrame.EXIT_ON_CLOSE);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.getConte ntPane().add(cursorLabel, BorderLayout.NORTH);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.getConte ntPane().add(defaultLabel, BorderLayout.CENTER);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.pack( );
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.setVisib le(true);
    &nbsp;&nbsp;&nbsp;}
    }
    </code&gt

    Comment

    Working...
    X