I‘m currently trying to render and use JComponents and it’s ancestors in a Custom Java Canvas. Right now, I‘m trying to create a Menu which should be clickable and it displays just fine inside my canvas when drawn with JComponent.draw(g). But the last piece I‘m trying to solve is that it should also react to the mouse-events the canvas generates (like hoovering over a JButton, clicking a JButton, etc etc). I only found listeners for events generated by the component…
So, is there any way I can tell a JComponent to not use its mouse events but instead pass through the events from somewhere else?
Related
I want to simulate basic input for a swing application. The whole thing runs headless and doesn't even have a JFrame. I just render all components from the JPanel using the draw() method on a Image (If you want to know why: The Image gets rendered on minecraft maps where users then should be able to interact). I want to execute click or scroll actions on the panel like it would come from regular user.
I know that I can simulate a Button click using doClick() but this doesn't work for components like checkboxes. I also tried using dispatchEvent() but that didn't fire the animations or on some components even threw a HeadlessException.
I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p
I'm making a point-and-click escape type of game. I'm wondering, if there's an easy way to make clickable images? I'm going to use photographs as background and also as items that the player has to collect. So, is there an easy way to make the items clickable and also disappear after clicked (player collects it).
Thanks for answers, and if my explanation was complicated, please say and I'll try to fix it.
For making a game I'd recommend bringing all the logic one level lower.
Create an data structure which will contain the state of your game "level". This data structure will be loaded from some kind of XML level configuration file, and I think it should contain:
A Image object containing the level background (photo).
An array of all items. Each item should have an Image, dimensions on the level screen (X,Y,Width,Height) and some kind of state (visible, highlighted, etc.).
Make a class which extends Canvas. This will be the component which will contain and render your whole game screen (with items, and background photo).
Override it's paint method. In this paint method use drawImage method go through your level data object (specified in step 1) and draw the background (room) and all the items in their respective coordinates. If the item has visible = false - don't draw it. If it has selected = true - draw some highlight around it or whatever you want.
Implement a MouseListener. This listener should check if click coordinates are inside the dimensions of one of your "objects" on the screen (loop through all clickable objects). If it is - do appropriate action (for example increase score and set visible = false for that item) and update your canvas with repaint. This will trigger the paint method again drawing all the changes on your canvas.
Register a MouseListener on your Canvas with addMouseListener to tie it all together.
If you're using Swing, simply set the icon of a JButton. This will create a "clickable image".
There are several ways, the most straightforward being using a JButton and setting an icon on it. But you can also add a MouseListener to any Component (like JPanel) and set an image as background (override paint).
You're going to probably do something like this: Draw a JPanel and then position a bunch of JLabels on it, and each label will draw it's own image too.
If you have a specific code question, we can be more help, but you're being very general. Try working through the Swing examples on the java web site and then ask more targeted questions.
You can set the image to a JButton object as background. The key point is that you should listen to the mouse click event and JButton is the first choice to satisfy this requirement.
A simple example on how you can get a clickable image. For more examples and explanations on java Swing and awt you can look at the official java tutorials here.
//a lable holding an image
JLabel label = new JLabel(new ImageIcon("MyImage));
//Add a mouse listener to get the click event
//
label.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}
});
i'm looking for this effect, but PulpCore doen't works with swing. Do you know what library use, or how to make a TileMap? Also, I'd like to move forever in it.
Thanks.
I'm guessing you don't want to use an actual world-map implementation like JXMapViewer...
The basic functionality appears to be: click on some coordinates, and that point becomes centered in the viewport. One fairly simple way to do this is to have the component(s) that you want to view all contained within a JPanel and have that panel inside a JScrollPane with its scrollbars turned off (setHorizontalScrollbarPolicy(HORIZONTAL_SCROLLBAR_NEVER) and so on). Then, set up a click or action listener for your elements that calculates the new center point, what the new viewport rectangle coordinates will be, and use scrollRectToVisible on the panel to shift the view. For animation, you can use a Swing Timer to set up a series of incremental scrolls in the required direction until you reach the target.
I've created an app with a small window (the size of a combo box). I need to create a floating panel that sits outside the window, next to the selected item in a JComboBox. (See attached image).
I've been reading about the JComboBox.setRenderer(customRenderer) etc. But was just wondering before I go down this path, whether it is at all possible to render something outside the window. I suspect it is, as the combobox itself manages to render it's popup list outside the window.
I'm very new to Swing, so any advice would be appreciated.
It's not possible with the custom renderer since Swing components are light weight. That is, Java is given a native window and all the component drawing takes place in that window. In your case, that is the JFrame containing the combo box.
What you can do though is create a new undecorated window and set it's location accordingly and draw whatever you want inside it.
EDIT: When Java needs to paint outside it's window bounds (like the case of pop up messages or combo boxes drop downs) if the component falls inside the bounds it uses the swing light weight mechanism. But if the component falls out side the bounds it is automatically substituted with a awt heavy weight component that has it's own native drawing surface outside the active window.
I've implemented similar idea using combobox renderers and tooltips on them. Content of every item's tooltip can be customized and rendered using HTML. Location of the tooltip can be set outside of the item itself thus creating design very similar to the one presented in your question.
Here is the starting point for you:
http://www.java2s.com/Code/Java/Swing-Components/ToolTipComboBoxExample.htm