Is it not possible to rotate a Label? It seems the API has that function but it doesn't seems to work? Are there anymore ways to rotate text?
Label nameLabel = new Label( "Test", skin);
nameLabel.setRotation( 90 );
stage.addActor( nameLabel );
You can wrap your label inside another Actor and rotate the parent Actor. So you will indirectly rotate the label, but the visible result is the same.
So you could create a parent actor for example like this:
public class LetterActor extends Group { //..
then for example in the constructor you add a Label to it:
this.addActor(someLabel);
then add a rotate action (or any other action!) to it:
this.addAction(Actions.rotateBy(90));
you may also need to set a height/width & origin for this parent actor
I've found it's not possible to rotate Labels, or Buttons or anything with text in libGDX. You can make an image and rotate it as a workaround.
Related
I create buttons using jlabels, so I can make a image into sort of a button. The only problem is, is that jlabels are square, therfore if i click somewhere within the square where the picture is not contained, it still runs the jlabel.MouseClickEvent. Is there any fix for this, or another component that i could use?
Ex. If i click this on the corner where the circle is not showing, but the square is still there, then the event fires.
Any fixes/different components to use? Thanks!
If you are just using simple Shapes for the images then you might be able to use the Shape Component found in Playing With Shapes.
The ShapeComponent will only respond to mouse events within the bounds of the Shape.
Otherwise the solution is to override the contains(...) method of your JLabel to check if the mouse point is in the bounds of your image, or in your case if the pixel at that location is not transparent.
I have a series of column labels that scrolls independently from the data that is displayed in a matrix below. I can make the whole scrollbar transparent except on hover. The labels are right up against the data, which I like, however, upon hover, unless I shift the vertical scroll (which I'd rather not do), the scrollbar obscures the beginning of all the labels.
I would like to set the background of the scrollbar as transparent so that only the "grabber" (or whatever it's called) is the only thing that is drawn. (It will obscure the beginning of the labels it is over, but would be a lot less so.)
Is there any way to do that? Here is what I tried:
Color bg = new Color(255,255,255,0);
colLabelScroll.setBackground(bg);
This does not seem to make the background of the scrollbar transparent.
What I'm shooting for is like how the iPhone's scrollbar grabber hovers over info in some apps. Is that even possible with JScrollBars?
Transparent JScrollBar can do it, but consider this: if column labels are related to the data and you can scroll them independently, beginner users may not understand what is going on and associate column labels with whatever is visually aligned beneath it. Either you will need some sort of visual indicator that makes it clear that the labels are disconnected from the data, or you should change the way labels are scrolled that never leaves them statically in 1 place.
Here's how I ended up making the relationship between the labels and the data clearer:
Instead of allowing the user to independently and intentionally scroll the labels, I decided to control the label scroll position via mouse hover. This eliminates the need for the obtrusive scrollbar.
I created a scroll-bar-like indicator that shows the portion of the data the labels represent.
I highlighted the currently hovered label that corresponds to the data below it, i.e. the only label that is ever correctly aligned with the data is the one that is under (or directly above) the cursor.
When the mouse is not hovered over (or dragging from) the column labels, do not display any labels. This helps prevent invalid label/data associations by the user.
A few nuanced notes: Implementing your own scrollbar-like indicator is somewhat involved, especially if your labels are painted and then rotated, because the paint position of 0 is at the bottom of the pane, yet the vertical scroll position of the pane is at the top. You will have to track the vertical scroll position to be able to recover it again when the cursor returns since you are blanking the labels on mouse out.
When developing a plugin for IntelliJ, I accomplished it with:
scrollPane.getVerticalScrollBar().setUI(ButtonlessScrollBarUI.createTransparent());
It takes advantage of the the:
ButtonlessScrollBarUI.createTransparent()
method, which is an IntelliJ specific method. However, if you can find a ScrollBarUI which has a transparent background, you can use the same trick.
Since I got a bit lost myself at first after reading #hepcat72's answer I'm posting a little explanation about the BasicScrollBarUI class:
JScrollBar scrollbar = scrollPaneConversation.getVerticalScrollBar();
scrollbar.setUI(new BasicScrollBarUI(){
// This function returns a JButton to be used as the increase button
// You could create your own customized button or return an empty(invisible) button
#Override
protected JButton createIncreaseButton(int orientation){
}
// Same as above for decrease button
#Override
protected JButton createDecreaseButton(int orientation){
}
// This function paints the "track" a.k.a the background of the scrollbar
// If you want no background just return from this function without doing anything
// If you want a custom background you can paint the 'Graphics g' object as you like
#Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
{
}
// This function paints the "thumb" a.k.a the thingy that you drag up and down
// You can override this function to paint it as you like
#Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
{
}
});
Refer to the Transparent JScrollBar link posted by #hepcat72 for hints about what to do exactly in these functions.
I'm trying to make a chessboard (8x8 grid) and fill it with background squares, but it always appears as a grid of empty squares. The image is called emptysquare.jpg but it does have a background color.
How to I correctly add an image to a JButton on the grid I have?
The reason that your chessboard is empty is due to the fact that you call JFrame#setVisible before adding your ChessSquare buttons to your panel. Ensure all component have been added before making this call.
Also set the Icon like this in ChessSquare
ImageIcon empty = ...
setIcon(empty);
You need to call setIcon( empty ) in your constructor.
It's allowed-but-awkward to call setVisible( ) before the squares are added, but if you do you may need to repack to get the layout right.
The call to setLayout( ) must be moved before the loop.
With JavaFX, I want to create some "post-it" which are composed by a rectangle for the shape and a TextArea to write something in this rectangle. I'm a beginner with JavaFX and I want to know if my rectangle could contain the TextArea to only have to move the rectangle when I want to move the entire object.
Thank you with advance, sry for my poor English.
Rectangle cannot have children, but there are many components that can be styled to look like a post-it that can.
You could for example use a Group, a VBox, a StackPane or a BorderPane for your post-it, and put a label or text area inside that. They will all move as one when you move the parent.
How can I create in a Swing interface a toggle image button?
I have two images, imageon.jpg and imageoff.jpg,and I basically want a clickable element that toggles the images and fires an event.
Update: Is there a way to override the usual 'chrome' around the image? I would prefer a simple image to a button with an image inside.
Load the images with ImageIcon. Create a JToggleButton. Then apply the icons with AbstractButton.setIcon/setPressedIcon/setSelectedIcon. Remove the border with AbstractButton.setBorderPainted(false).
How about JToggleButton? You can subclass it and override paint() to paint the correct image based on whether it is selected or not.
Another way would be to subclass JPanel and catch mouse clicks, overriding paintComponent() to draw the correct image. This way the only thing that gets drawn is the actual image (unlike the JToggleButton option).
I had the same problem with JButtons. Try this:
result = new JButton( icon );
result.setBorderPainted( false );
result.setContentAreaFilled( false );
width = icon.getIconWidth();
height = icon.getIconHeight();
result.setPreferredSize( new Dimension( width, height ) );
It is necessary to set the preferred size to get rid of additional space around the button. This worked for me on Windows 7 and Mac OS X 10.6.
Your best bet is to subclass AbstractButton, and set properties like border and background (in your constructor).
MyButton() {
setBorder(null);
setBackground(null);
}