Results 1 to 7 of 7
- 08-31-2012, 04:04 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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:
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.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){ } }
If you want to use the images they are here:
Last edited by newbie106; 08-31-2012 at 04:19 AM.
-
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.
- 08-31-2012, 04:12 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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.
-
Re: How to change the mouse pointer ?
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.
- 08-31-2012, 06:26 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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 replyLast edited by newbie106; 08-31-2012 at 07:04 AM.
- 08-31-2012, 09:56 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 08-31-2012, 10:21 AM #7
Re: How to change the mouse pointer ?
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).
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Making an invisible JButton that appears under the mouse pointer
By kjkrum in forum AWT / SwingReplies: 8Last Post: 04-04-2012, 01:45 AM -
change color on mouse click
By hannerz06 in forum New To JavaReplies: 3Last Post: 03-31-2010, 09:46 PM -
change the mouse cursor - crosshair cursor
By cassysumandak in forum New To JavaReplies: 1Last Post: 09-28-2009, 01:57 AM -
change object color on mouse click
By gotenks05 in forum Java AppletsReplies: 1Last Post: 04-05-2009, 07:14 PM -
change the delay between double click in mouse listener
By itaipee in forum AWT / SwingReplies: 6Last Post: 03-17-2009, 02:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks