Results 1 to 7 of 7
  1. #1
    newbie106 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default How to change the mouse pointer ?

    Hi, I've been writing a class that is supposed to display a gun aim instead of the mouse and a bullet hole every place clicked. I used the following code:
    Java Code:
    import javax.swing.*;
    public class Shooting {
    
    	public static void main(String[] args) {
    		JFrame frame= new JFrame("Shooting");
    		frame.add(new MovingLabel());
    		frame.setSize(1360, 730);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    
    	}
    
    }
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class MovingLabel extends JComponent implements MouseListener, MouseMotionListener {
    	public MovingLabel(){
    		addMouseListener(this);
    		addMouseMotionListener(this);
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    
    		img.paintIcon(this, g, xx, yy);
    	}
    	public void mouseClicked(MouseEvent e){
    		ImageIcon image= new ImageIcon(getClass().getResource("bullet1.png"));
    		image.paintIcon(getComponentPopupMenu(), getGraphics(), e.getX(), e.getY());
    	}
    	private int xx; 
    	private int yy;
    	private ImageIcon img= new ImageIcon(getClass().getResource("aim.png"));
    	public void mouseEntered(MouseEvent e) {
    	}
    	public void mouseExited(MouseEvent e) {
    	}
    	public void mousePressed(MouseEvent e) {
    	}
    	public void mouseReleased(MouseEvent e) {
    	}
    	public void mouseMoved(MouseEvent e){
    		xx=e.getX();
    		yy=e.getY();
    		repaint();
    	}
    	public void mouseDragged(MouseEvent e){
    	}
    
    }
    However it has many problems. The first problem is that the bullet image comes infront of the aim image which is not what I intended. The other problem is when I move the mouse the bullet hole image goes away. The third problem is the mouse pointer is visible along with the gun aim picture. The fourth problem is it is slow and when I click multiple times quickly it does not work as well so please help.
    If you want to use the images they are here:How to change the mouse pointer ?-aim.png How to change the mouse pointer ?-bullet1.png
    Last edited by newbie106; 08-31-2012 at 04:19 AM.

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

    Default Re: How to change the mouse pointer ?

    I don't see how you're using the bullet hole image. I see that you create an ImageIcon called "image", but don't see anywhere in your code above where this icon is used or drawn. Is there more code that hasn't been shown yet? Also please wrap your code in [code] [/code] tags so that it retains its formatting and is easier to read.

  3. #3
    newbie106 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to change the mouse pointer ?

    there is no more to the code and just for clarification bullet1.png is the bullet hole picture and it is used and painted in this method:
    Java Code:
    public void mouseClicked(MouseEvent e){
    		ImageIcon image= new ImageIcon(getClass().getResource("bullet1.png"));
    		image.paintIcon(getComponentPopupMenu(), getGraphics(), e.getX(), e.getY());
    	}
    Last edited by newbie106; 08-31-2012 at 04:45 AM.

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

    Default Re: How to change the mouse pointer ?

    Quote Originally Posted by newbie106 View Post
    there is no more to the code and just for clarification bullet1.png is the bullet hole picture and it is used and painted in this method:
    public void mouseClicked(MouseEvent e){
    ImageIcon image= new ImageIcon(getClass().getResource("bullet1.png"));
    image.paintIcon(getComponentPopupMenu(), getGraphics(), e.getX(), e.getY());
    }
    Again, please use code tags as I've described above when posting code. It's hard enough trying to understand someone else's code, please don't make it harder than it has to be.

    I see that you've now added the code for drawing your bullet hole, and I see your problem -- you're trying to draw it in the popup menu layer using getGraphics, a very bad way to do this.

    Instead I've drawn images like this in one of two ways:
    1) create an ArrayList of Point and in mousePressed add the mouse Point into this ArrayList and call repaint(). Then in paintComponent iterate through this list drawing your bullet hole. Or
    2) create a BufferedImage that is the size of your JComponent at the start of your program and draw this image in your JComponent's paintComponent method using Graphics#drawImage(...). In mousePressed get the Graphics object from this BufferedImage by calling getGraphics() on it, use this Graphics object to draw the bullet hole, dispose of the Graphics object obtained, and call repaint().

    Also note that you should read in your images only once, not on each key press.

  5. #5
    newbie106 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to change the mouse pointer ?

    Please bear with me, I'm not that good with java. Can you explain how to create BufferedImage and use it. I tried to create one but it asked for 3 parameters: width, height and imageType. I don't know what to put for imageType because its an int.
    and thanks for your quick reply
    Last edited by newbie106; 08-31-2012 at 07:04 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: How to change the mouse pointer ?

    For the mouse cursor you want to create your own Cursor, which allows you to define the hotspot (the point in the image which is considered to be the mouse coordinates).
    Toolkit has a createCursor method (or something like that) which takes the image and the hotspot.

    That'll give you a better cursor, which may well solve your bullt-hole problem as well.
    Please do not ask for code as refusal often offends.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to change the mouse pointer ?

    Quote Originally Posted by newbie106 View Post
    Can you explain how to create BufferedImage and use it.
    Have you read the API for the class? Do that first, and then go through this Tutorial Lesson: Working with Images (The Java™ Tutorials > 2D Graphics) (and the subtopics listed in the left sidebar).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 8
    Last Post: 04-04-2012, 01:45 AM
  2. change color on mouse click
    By hannerz06 in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 09:46 PM
  3. change the mouse cursor - crosshair cursor
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 09-28-2009, 01:57 AM
  4. change object color on mouse click
    By gotenks05 in forum Java Applets
    Replies: 1
    Last Post: 04-05-2009, 07:14 PM
  5. Replies: 6
    Last Post: 03-17-2009, 02:23 AM

Posting Permissions

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