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.
Related
I was trying to add a mouseListener and a mouseMotionListener to my game and noticed that I can add them to the Canvas or to the JFrame. Do I add it to both or one of them?
What I recommend is using the Canvas. When you are using a listener of any kind, think of where the actions will occur. Do all of your updates happen on the frame or on the canvas? If it is the latter, use the canvas to handle all of your action listener objects.
Another way to think of it is that the JFrame is just a window holding the implementation of your game. Your graphic updates, keyboard inputs, mouse inputs, and any other functionality is done through the canvas.
For example, compare the JFrame and canvas to this image of Skyrim. The window on the outside (A JFrame Object) has a close/minimize feature and the window holds the game screen (A Canvas Object).
Attach it to the Canvas
You should add the mouse listener to the canvas, there is one reason for it:
Coordinates.
If you attach mouse listener to the frame the 0-point of coordinates will be on the left-up corner of JFrame border. It will be hard to calculate the coordinates relative to canvas.
Instead of you can attach mouse listener to Canvas. The coordinates will be better with this. But do not forget to gain focus on canvas after adding listener:
canvas.addMouseMotionListener(motionListener);
canvas.requestFocus();
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.
I'm working on a domino game and it's going pretty well, now I want to drag a domino tile from one JPanel to another, my dragging implementation works, it's just that I can't find how to drag shapes between two jpanels.
Here's how it looks:
It is called Drop Location Rendering.
I'm building a domino game in java and I am using modified rectangle2d's to draw my tiles. To drag a tile I use mouse events to change the tiles coordinates and redraw the JPanel.
This all works great and very smooth, until I start using the frames glassPane, I use the glassPane to be able to drag a tile from one JPanel to another.
It works, but rendering is quite slow when I paint on the glassPane. I've tried to use clipping when repainting, but it makes no difference.
Does anyone have an idea?
thnx.
It seems when a glassPane is visible on your RootPaneContainer, all repaint events behind the GlassPane have their clip set to fill the entire RootPaneContainer. This may be overriding your manually specified clip rect.
I am working on a project in which I have a background image with specific points of interest. Each of these specific points will have a custom button class overlaid on it so that when I click the point, I'm actually clicking the button. However, I would like to be able to rotate the background image and have the buttons rotate with the image so that the custom buttons are still overlaid on the specific points. Any tips as to how I should go about doing this?
Are you actually wanting to rotate 4 different images and move them around the square, but always keeping them upright? Or are you rotating a single image so that after one button click the single image is on its side? If the former, then that can be easily done by using a container (a JPanel) that uses BorderLayout, and having four JPanels with background images and JButtons held in the container JPanel at the four compass points of the BorderLayout: BorderLayout.EAST, BorderLayout.WEST, BorderLayout.NORTH, and BorderLayout.SOUTH (although Java gurus prefer you use the newer constants, i.e., BorderLayout.PAGE_START). Then when a button is pressed, remove components and re-add but in a rotated order.
If you want to do the latter, then things get a bit trickier in that you'll likely need to use AffineTransforms, rotate instance to rotate the container, and you'll need to perform the same transformation on the point of the mouse press/click/release, so that the rotated buttons receive correct clicks. If the container is not square, things get even trickier still.