Image Click event in Java LibGdx - java

I am working on a Street Fighter Game. I need to have a character selection screen for the user. How can I do this character selection in Java libGDX? This is my image code. Can I do this using ImageClick event?
splashTexture1 = new Texture(Gdx.files.internal("assets/gui/Character1.PNG"));
splashTexture1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite1 = new Sprite(splashTexture1);
splashSprite1.setX(100);
splashSprite1.setY(180);

My suggestiong is using scene2d for menu or HUD stuff.
Check this out : https://github.com/libgdx/libgdx/wiki/Scene2d
scene2d is a 2D scene graph for building applications and UIs using a hierarchy of actors:
buttons
labels
sliders
text buttons
scrollable views (lists)
tables
your custom widgets
group of actors
You can add ClickListener to an actor. (In your case the actor is a Button)

You can do it in two ways. I'd recommend using both.
Common stuff
You can list images of all characters in a screen. And let the user select any one using one of the following.
Click event
Each image will have a separate ClickListener (If you have problem regarding that, you should search 'How to add clicklisteners to libgdx actors).
On clicking any image, the variable storing selected character (for the rest of the game) will be set with the image that has been clicked. And the screen is changed.
Keyboard event
One of the image will have focus drawn around it. The user can move focus around to other character images using arrow keys. When user presses enter, the character that currently has focus gets selected and the screen is changed.
Hope this helps.

I would recomend to use the Table-Layout from libgdx for it. Add an ImageButton to a Table and add an ClickListener to the ImageButton. Add the same Texture as up and down Texture and you are done. Maybe change it later so the user see that he has clicked a figure.
The Table gives you the ability to arrange those Buttons in the way you like and it's structured. Moreover it is easy to create a dynamic view, that does have new ImageButton for every new Texture you add to a folder for example. Just create a ScrollPane with a the Table of "figures" (which are the ImageButton with an listener) and you can add a bunch of it and let the user scroll down and up to check which he likes.

Related

I want to build an Android app with overlay over the map, What layout should I use to achieve the result as the image?

enter image description here
Should I use fragments? but I want an overlay over the map activity. How can I achieve this?
So far, I have been trying, to add two buttons and on click of a button opens a floating map, but it doesn't give me this result, it resembles a bookmark bar on chrome with three dots on the sides which aren't what I want.
Not sure what you are going to do when user will tap on go back or any button or back press
but here i assume it that you are going to remove that white overlay and only map will be shown
if yes then you can simply use map in your activity/fragment whatever you like and white overlay should be created and shown using BottomSheetDialogFragment
to understand how you will create BottomSheetDialogFragment , you can refer this example
https://medium.com/#kosta.palash/using-bottomsheetdialogfragment-with-material-design-guideline-f9814c39b9fc
I think the best bet is using coordinatorLayout with a child layout that has BottomsheetBehaviour for the overlay. or simply BottomSheetDialogFragment if user isn't going to interact with map when the overlay is drawn up.
a layout with app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" fits the picture. more on that here

Gwt - Showing a pop up panel when user's touch more than one widget or link

I am creating a panel showing many different kind of widgets such as button. The panel allows to zoom in and zoom out. It is required to show whole panel in the beginning. However, some users may touch more than one button when the panel is too small.
I want to handler the situation like chrome in Android. When the user touches more than one link, a pop up panel will be showing.
What library or APIs may I use?
Thanks!
You could place your Buttons in a FocusPanel implementing a ClickHandler to open your desired popup- thus when your user clicked between two buttons the click is registered and you can handle it.
Note, you will have to place a FlowPanel in that FocusPanel to place more than one button inside.
If you want to react on hover instead of on click, use HoverHandler instead.

How to select multiple checkboxes in a Drag event

We have multiple checkboxes, as shown below:
We want the user to be able to select first checkbox for A,B,C, by him dragging over the checkboxes.
Is this possible in android?
Yes, you could do something like that. You'd have to first start a drag operation and drag a view. If you tried to do it with just your finger, it would be an exercise in gesture detection. But dragging a view would be simple.
Using this tutorial as a guide - http://developer.android.com/guide/topics/ui/drag-drop.html - you should be able to start a drag operation using a click listener on the view you want to drag. Then, as the dragged view enters the bounding boxes (you set up draglisteners on each) of a checkbox, you can set it to checked.

Getting graphical buttons to line up in xml

I have a custom popup window with a custom image background in which I need to place three custom buttons with their own background images. I have only one set of images for all the components, so I want all the parts to scale appropriately. My question now is how do I make sure that the buttons line up with the appropriate parts of the background image of the popup window. I have been experimenting with all sorts of widths and heights and I can not make it look consistent on all my test devices. Can anyone give me some pointers?
EDIT: Just to make it as clear as possible, the image below illustrates the kind of thing I am talking about:
I want the graphical button to line up with the arrows which are a part of the popup window background image.
arrange buttons in an xml layout and set this view to your custom popup and you can set images to buttons from code.

Clickable images in Java

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);
}
});

Categories