Painting a custom Mouse over your preset mouse - java

I'm making a game and when I add my sprites to my screen, naturally, they are behind my mouse. But I want to add a custom mouse image to my screen, over the mouse.
I already have sprites for the mouse, and I know to to make the image appear wherever my mouse is. The only thing is that I already know its going to appear underneath my mouse. Any suggestions?

Set the image as a custom Cursor. See Toolkit.createCustomCursor(Image,Point,String) for turning the image into a cursor.

Related

how can i make part of a JLabel Visible under mouse movement?

how do i make a part of the JLabel, relative to the position of the mouse, visible only under the location of the mouse. the JLabel has an image of a room. i want the mouse to act as a flashlight, and only display, in a large circumference around the mouse the part of the JLabel/image that the mouse is hovering over.
Use a JLayer.
Read the section from the Swing tutorial on How to Decorate Components With the JLayer Class for various examples.
The section on Responding to Events has an example that does what you want.

Java - Getting screen position of buffered image

I'm working with some code where I have a few buffered images in a scrollbar. When I click one of these images, I want to draw a rectangle around the image to indicate that the image is selected.
My issue is that because my panel is scrollable, the image position set when drawing is not necessarily the actual screen position. So when the mouse is clicked, the point of the cursor's position doesn't intersect any of the position attributes (x, y, height, & width) I have for the object holding the actual image.
Does anybody know how to get the actual position of an image relative to the screen instead of the panel?
So far I haven't found any solutions to this online.
Thanks.
You can use the SwingUtilities.convertPoint(...) method.
I would guess you need to convert the mouse point the coordinates of the viewport of the scroll pane.
Maybe an easier approach is to use a JList with a custom renderer. You can set the JList to wrap components horizontally. Then in you custom renderer you just add a Border to the selected item. Read the section from the Swing tutorial on How to Use Lists for more information and examples.

Canvas: refresh screen on touch

I'm trying to make something like Battleships.
By this time I've managed to random spawn and draw them on canvas.
But there's another problem: in the game user will touch the cell and that cell will be painted as yellow (if hit) or draw a dot (if miss).
As far as I see, everything that displays on canvas must be writted in onDraw() method.
The question is: how can I make screen refresh with new data when user touches the screen?
Now the screen looks like this:
This time the ships are visible for debug

Java. Swing. JComponent's clickable area

I have a custom component displaying *.png image. The image has transparent and non-transparent area. If I add ActionListener(or MouseClickListener) to component, it will raise events even if I click on transparent area of component. Visually it looks like clicking outside the component.
How can I declare which area should react on clicks and which should not?
I've read about getting pixel from image your coordinates from event object and check its transparency. It seems difficult and ineffective.
Maybe define custom-border of this component or something else?
You answered your own question.
Within the mousePressed() event handler, you're going to have to check if you're within the JComponent, and then check the pixel at the x and y coordinate of the mouse click for transparency.
How can I declare which area should react on clicks and which should not?
This is done at the JComponent level by overriding the contains(...) method. So for example you extend JLabel to create a TransparentLabel which contain your image in the form of an icon.
Then whenever this method is invoked you only need to check this one location to determine if the pixel is transparent or not.
If your main issue about "overhead" is that you only want to make it opaque when the mouse enters a non-transparent part of the image, I'd consider pre-computing an image "mask".
On image load, make another image (or a 2d array, or something similar) that will be binary (i.e. black-and-white only, or 1 and 0 values only in an array). 0/white = transparent, 1/black = non-transparent.
Then, on mouse events, you can just check the exact pixel in the mask if it is set (value = black or 1), and trigger if it is.
Did you try to bunk two same pictures file and just for the second picture give it a short width ? Like that I think you will can to add differents listerners for the both pictures.

Is it possible to move an Icon?

In Java, is it possible to use paintIcon to put an icon into a canvas, and then move that icon around using keyboard events like you would a game character?
Once you paint something on the canvas, it stays there, and what was there beneath that location is lost. To "move" the icon, you need to repaint the canvas (or at least what was originally at the location of the icon) and draw the icon at its new position.
This would work better with an applet by registering keylisteners.
and overridding the paint method

Categories