I am using several horizontally positioned JSlider objects in my Java application and would like to align their left and right track ends vertically. That means whenever two knobs are positioned on the left end of their track the upper knob should be exactly above the lower one without any displacement in horizontal direction.
The problem I currently have is that long labels at ticks on the left or right end of a track decrease the length of the track and do not allow a nice alignment. As an example, see the following image:
JSlider alignment problem example
Does anyone know how to vertically align the tracks of two horizontally positioned JSlider objects independent of their tick labels?
A solution for my problem might be to left-align/right-align labels at the end of the track (i.e. the label on the far left tick is left-aligned to its tick), but I did not find out how I could do that. Maybe there is also a better solution I did not think about.
Thanks, Sandreal
I do not think that the JSlider labels are accessible in such a way that you can edit their location, so instead you should create separate labels and position then yourself relative to the JSlider, then you can be sure that they will not effect the JSlider (depending on your layout manager).
Related
I’m writing code for drawing in a JPanel as if it’s a canvas, where I can create shapes by clicking inside the panel. When I click, it places the dots where I want them to go, however if I scroll and then click, it places the dots where the mouse would be if I hadn’t scrolled. I assume this is because the mouse coordinates don’t change, so I’m guessing I need to add the number of pixels I’ve scrolled (horizontally and vertically). Is there a command I can use to refer to these values?
It's been a while since I've worked with Swing, but I believe you want to call:
JScrollPane sp = ...
Point viewPosition = sp.getViewport().getViewPosition();
The viewPosition.x and .y will return the offset in pixels from the top left corner.
I have made a player that can display few steps with different images.
Under this player I have added a JSlider, to jump to every step. It should have labels for the first and last steps.
I managed by setting my own LabelTable for the JSlider.
It works very well for numbers under 100. But numbers with three or more digits get are cut off.
See the bottom-right corner of this image. Step 438 is not completely visible:
Is it possible to get the last (step)label completely visible?
I have already tried:
set the (max, min, preferred) size of the slider
get the JLabel over getLabelTable and set the location (a few pixels to the left) of the JLabel, but it is always (0,0).
The JSlider uses the SynthSliderUI.
Thank you! ;)
I have a GridLayout I'm making which is populated with a bunch of pictures. The GridLayout itself is set to SizeFull(), as is each individual image in the grid.
The grid with the pictures is within another grid, and that grid has relative sizes set.
With this set up, the grid of pictures stays within the spot I want it to, properly resizing to fit within the space they should, but the pictures do not retain their proper square proportions. They squish fat or skinny however they want. I want them to retain the original proportions, though, so they expand to fill their available area as much as possible while retaining those proportions.
If I set the width to 100%, or the height to 100%, and leave the other undefined, then it retains the proportions, and properly expands to fit the one that is set to 100%, but the other spills outside the nested grid layout's spot in the upper grid layout.
Anyone know how to do this?
Have you tried setting the width and height as percentage?
So on an image of 80 by 120 pixels:
setWidth(66, Unit.PERCENTAGE)
setHeight(100, Unit.PERCENTAGE)
I want to have something like this five buttons, one button surrounded by four other buttons. Just like this:
I know that with Android that we can only have square type views so how would it be possible to do this? OpenGl or something? Anyone have any links to something related? Basically I want curved buttons that are close together.
My guess is that at the end of the day it will be easier to do this in a custom view.
But if you want to use stock views, I'd suggest the following. First, use a RelativeLayout container to arrange the four outside buttons in a 2x2 grid. Then position the center button so it overlaps the grid in the center. Put the center button at a higher Z (closer to the user) than the four surrounding buttons. Then use transparency as part of the button images to get them to look like you want. Then (hopefully) try it out. If the Z order is right, the center button will capture taps that would otherwise have gone to one of the other four buttons.
This actually won't work as is, because the center button square will intrude into the surrounding squares. I don't know if it would work, but you can then try replacing the center button with another grid of button "pieces". The grid would have empty positions except where the center button image overlaps the grid cell. You'll have to make this fine enough to avoid intruding into the outer button images.
EDIT:
It occurred to me that perhaps you could do this with a group of TouchDelegate objects. You would arrange the buttons as I described at first, but make only the parent container clickable. It would use five TouchDelegates to find out which button (if any) was under the tap coordinates. Unfortunately, TouchDelegate only work with Rect hit regions, which leaves us where we started. However, you could cannibalize the source of TouchDelegate and define your own version that accepts some sort of general shape class instead of just a Rect. (The shape class would have to have the equivalent of Rect.contains() to test for a hit. There's nothing built into Android, but you could easily write your own classes for the specific shapes you have.)
You could simplify the code a bit by putting the hit and delegation logic directly in the parent container view, but it would be cleaner, I think, to have a reusable delegate class that separates out the event handling from the container itself.
You can use a mask color for example:
a completely red button,like the image that you posted,the just do pixel color check against the red button,something like:
bool insideMyAwsomeShapeButton(int mouse_x,int mouse_y) {
if get_image_pixel_color_at_pos(mouse_x,mouse_y) == rgbcolor(1,0,0)
{
return true;
}
return false; }
again,this is just a thought,you have to find out specific api on android to do the task,like check pixel color.
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.