Results 1 to 5 of 5
  1. #1
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default [SOLVED] Custom Cursors: Thick Lines

    I wanted to create my own custom cursor so I wrote some code to create a BufferedImage then I use this to create a cursor. I wanted to avoid having an image file because this is something I then have to distribute with my software.

    The code below creates this cursor, it works, and also draws a rectangle by comparison.

    My problem is that the lines in the cursor are incredibly thick. Is there any way around this? I'd like them to be thinner.

    Java Code:
    public class Main {
        public static Cursor GetMyCursor() {
            Image image = GetMyImage();
            Point hotSpot = new Point(0,0);
            return Toolkit.getDefaultToolkit().createCustomCursor(image, hotSpot, "my cursor");
        }
    
        public static Image GetMyImage() {
            int width = 10;
            int height = 10;
            BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D graphics = image.createGraphics();
            graphics.setColor(Color.BLACK);
            graphics.drawLine(0, 0, 0, 6);
            graphics.drawLine(0, 0, 6, 0);
            graphics.drawLine(3, 2, 3, 4);
            graphics.drawLine(2, 3, 4, 3);
    
            return image;
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JFrame test = new JFrame();
            test.add(new MyFrame());
            test.setPreferredSize(new Dimension(200,200));
            test.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            test.pack();
            test.setCursor(GetMyCursor());
            test.setVisible(true);
        }
    
        public static class MyFrame extends JPanel {
            protected @Override void paintComponent(Graphics g) {
                g.drawRect(10,10,20,20);
            }
        }
    
    }

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Graphics2D.setStroke() should sort it.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The stroke may not help here as your image is being magnified more than two fold. I think the problem is that your image size is less then half the size of a cursor, and so Swing has to magnify the image to get it to fit a cursor's size. Instead, try this:

    Java Code:
      public static Image GetMyImage()
      {
        int width = 32;
        int height = 32;
    and I'll bet things will work better.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Ah, good spot
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

Similar Threads

  1. SWT List w/ colored lines?
    By p!lle in forum SWT / JFace
    Replies: 1
    Last Post: 03-23-2009, 04:23 PM
  2. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 11:26 AM
  3. Random Lines
    By Urgle in forum New To Java
    Replies: 29
    Last Post: 11-12-2008, 04:42 PM
  4. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:41 AM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •