I understand that 'fluid' isn't the best description, but that's how I imagine the green that lays inside a progress bar.
I was just wondering if it was possible to just see the green 'fluid' from the progress bar, and not the container that hold the 'fluid'.
The purpose of this is to make artwork under the progress bar that would hold the green 'fluid', rather then the system's default UI holding this fluid.
EDIT:
This is a photo of the current progress bar in question:
The JFrame this is on is INVISIBLE, so all you can see is this bar.
I would like to know if it is possible to remove the GREY from around the JProgressBar and just display the green.
By fluid, you may mean the paint used by a BasicProgressBarUI in its implementation of paintDeterminate(). The UI delegate fills all of boxRect with ProgressBar.background and some fraction of boxRect with ProgressBar.foreground. You can
Change the color via the UIManager, as discussed here, but the delegate is not obligated to use your setting.
Implementnt your own ProgressBarUI, as suggested here.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JProgressBar.html#paintBorder
you should use setBorderPainted to remove the border, I guess that's what you want.
I know this is an old question, but found it using google and I'm trying to do exactly the same thing. I found setting the background to transparent works perfectly, at least with the Nimbus L&F, haven't tested this with others.
progress.setBackground (new Color (0, 0, 0, 0));
Related
Here a simple question I'd like to ask.
I'm making a videogame (with NO graphics) and for that I'm using SceneBuilder. So here is the state of my HP bar :
HP BAR
Now I want to reshape it in order to obtain a heartshape :
HP BAR wanted
Thus, anyone know if it's possible to do it using SceneBuilder with the settings?
Thank you everyone, have a wonderful day.
One simple way, providing the background of the panel is simple, would be to overlay an ImageView over the top of the progress bar. If you use a PNG with a "hole" of transparency, you'd effectively hide the areas of the square that you're not interested in seeing.
In this example, the black pixels would be the same colour as the background colour (in your case grey) and the white pixels would be fully transparent.
The downside to this is that if the panel changes colour then you'll need to recreate your PNG in the new colour.
My boss has put me in charge of building a production dashboard to track the efficiency of the workers on a production line. He wants a progress bar to fill up in relation to the time and change colors if the worker is running on time or if they're behind, etc. I need to know if you can have multiple colors for one JProgressBar that changes as the bar fills up to show those changes. I'm referring to
Is this even possible? If not, what's my best course of action to go about doing this? I've thought about putting multiple JProgressBars together and doing some calculations to find out which section needs to be which color, unless there's a better way to go about it.
Looking at the link I posted above, you might be able to create multiple progressbars, setting the width of them to something predetermined (length of a task), placing them side-by-side, then setting those colors for each piece individually. Looking around I don't see anything for setting colors to from n to m to a certain value.
Make a progress bar whose foreground colour is "transparent". Put a graphic behind it with all the colours you want. As the progress bar fills up, the transparent part of it will expose the graphic behind it.
Been building up a little game in java, already asked a few questions about the usage of JPanel in it, and used them to paint the graphics and the main part of the screens.
Now i have a little doubt about other components. My intention is to add on the corner a pair of bars to show health and mana of an entity (like in a rpg game), and wondered which was the best approach for it.
Thought about making a new JPnale with a pair of JProgressBar to set the ammount of it, but then i wondered if it would be better to paint it completely and fill a pair of rectangles.
I mean, doing a pair of new JProgressBar() for it, or a pair of g.fillRect() and then paint the ammounts.
I guess that easiest is to set the JProgress, as i can set values and text if i want, maybe, but not sure about it and if it would run smoother without overwhelming it with JComponents.
Also, if want to add buttons would be better the JButton, or paint a rectangle and check for containment of the mouse pointer with an event (I have this approach at some points where there is not KeyBinding). Should i change that?
Thank you beforehand :)
I'm writing an answer because i haven't got enough reputation to comment :(
I think is better if you paint it in your graphics engine. Use Rectangles is of course a better idea than use a new JPanel with JProgressBar.
But i think that it's even better if you use Images to build your own Progress Bar.
You can create them or find them on internet.
For example you can take an Image for the Progress Bar Background, and another Image for the Foreground (the part that will fill the Bar). Then you can set their X and Y position and then just change the Foreground Width in relation with the entity health to fill or empty the Progress Bar.
I know how to draw a rectangle onto a JPanel, but how can I paint a rectangle to the screen so that the rectangle appears to be floating? More specifically, a non-filled rectangle. My thought is to use a transparent JFrame with a rectangle drawn on it; however, this makes all of the content in the JFrame transparent.
My Solution
So I think there are many ways of going about this, some more complex than others, some more practical than others.
I tried the splash screen. The problem with that is you need to pass VM parameters "-splash " when you run. I created a manifest file to automate this/put the parameters into eclipse; but then the code is dependent on the .gif file and I can't change the size/position of the rectangle easily. Similar problems occur while faking it via screen screenshot. Good suggestions though, I learned some pretty cool stuff.
So, back to what I did. I used a JFrame, got the content pane and set the background to red (what ever color you want), then set the frame undecorated which removes the titlebar and border of the window. This created a floating rectangle which I could easily change the size and location of (frame.setSize, .setLocation). I have yet to make this a non filled rectangle, I tried internal frames and layeredpanes, but no success.
JFrame is a heavyweight component, and those were always opaque for the longest time. However, since Java 6u10, there is an inofficial API for transparent windows in the class com.sun.awt.AWTUtilities, which will most likely become official in Java 7. In earlier versions, the only way to simulate this kind of thing was to fake it via screenshots taken with java.awt.Robot
You would probably have to have parts of the window transparent while the actual drawn rectangle is not. I doubt there is a platform-agnostic solution for this so you would need to resort to JNI for this. Depending on what you need to do it might also be nice to make the window invisible for clicks which would need other tricks as well.
https://github.com/twall/jna/
That project offers a library and has examples of a clock and info balloons that are semi-transparent and transcend even what you're trying to do. The demos work well on Windows but I couldn't speak to how cross platform they are.
You might want to look at JLayeredPane. The demo on this page shows partially what you want, however when painting your rectangle you'll need to set your paint to transparent:
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
g2d.setComposite(ac);
g2d.drawImage(image, x, y, this);
I have an application that uses disabled JTextFields in several places which are intended to be transparent - allowing the background to show through instead of the text field's normal background.
When running the new Nimbus LAF these fields are opaque (despite setting setOpaque(false)), and my UI is broken. It's as if the LAF is ignoring the opaque property. Setting a background color explicitly is both difficult in several places, and less than optimal due to background images actually doesn't work - it still paints it's LAF default background over the top, leaving a border-like appearance (the splash screen below has the background explicitly set to match the image).
Any ideas on how I can get Nimbus to not paint the background for a JTextField?
Note: I need a JTextField, rather than a JLabel, because I need the thread-safe setText(), and wrapping capability.
Note: My fallback position is to continue using the system LAF, but Nimbus does look substantially better.
See example images below.
Conclusions
The surprise at this behavior is due to a misinterpretation of what setOpaque() is meant to do - from the Nimbus bug report:
This is a problem the the orginal design of Swing and how it has been confusing for years. The issue is setOpaque(false) has had a side effect in exiting LAFs which is that of hiding the background which is not really what it is ment for. It is ment to say that the component my have transparent parts and swing should paint the parent component behind it.
It's unfortunate that the Nimbus components also appear not to honor setBackground(null) which would otherwise be the recommended way to stop the background painting. Setting a fully transparent background seems unintuitive to me.
In my opinion, setOpaque()/isOpaque() is a faulty public API choice which should have been only:
public boolean isFullyOpaque();
I say this, because isOpaque()==true is a contract with Swing that the component subclass will take responsibility for painting it's entire background - which means the parent can skip painting that region if it wants (which is an important performance enhancement). Something external cannot directly change this contract (legitimately), whose fulfillment may be coded into the component.
So the opacity of the component should not have been settable using setOpaque(). Instead something like setBackground(null) should cause many components to "not have a background" and therefore become not fully opaque. By way of example, in an ideal world most components should have an isOpaque() that looks like this:
public boolean isOpaque() { return (background!=null); }
I ran into this same issue last week using JTextPane. The setOpaque() method works as expected when using any look and feel other than nimbus. Apparently, the nimbus look and feel changes the behaviour we have come to expect with setOpaque() for many Components. Depending on how you look at it, it can be considered a bug. Check the comments on this sun bugid:
nimbus opaque bug
The workaround that worked for me was:
myPane.setOpaque(false); // added by OP
myPane.setBorder(BorderFactory.createEmptyBorder());
myPane.setBackground(new Color(0,0,0,0));
Note from OP: I also had to ensure setOpaque(false) for JTextField so that the parent background was painted - just wanted to mention this for others who follow in case they had experimented with setOpaque(true), as I had.
Hey there Software Monkey.
mmhh what about installing UI's subclasses replacement that actually respect the setOpaque behavior.
I think it is something like setUI or something like that.
You could grab the source code of the nimbus and see what's broken there ( if it is there ) , subclass it and install the "fixed" one.
Yours sound quite intersting, do you have any screenshot we can see?
From the javadoc
public void setBackground(Color bg)
Sets the background color of this component. The background color is
used only if the component is opaque,
and only by subclasses of JComponent
or ComponentUI implementations. Direct
subclasses of JComponent must override
paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose
to ignore it.
I think the question is how to interpret "opaque" and "background".
For a JTextfield there is the question: "what visible parts are the background?". I'd define "background" as the parts of the bounding rectangle, that are not drawn by the component.
For a "round" button, e.g., this will be the corners outside the circle.
Therefor I'd say a JTextfield has no visible background! It has a rectangular shape and what you are the taking as background is not the field's background but the field's canvas.
Rebuttal from OP
This is an interesting enough idea to be worth responding to in the answer for future viewers (as opposed to in comments).
I have to disagree. I would argue that the part of the component outside the border is not part of the component - it's outside the component. A field with rounded corners is, of necessity, non-opaque, in that it cannot be responsible for painting it's entire rectangular region - this is a side-effect of all components being rectangular in dimensions.
I think this consideration makes the argument for the existing (and misunderstood) meaning of isOpaque(). It also makes my argument that setOpaque() should not exist and that setBackground(null) should cause the component to not paint a background.
I would put forth that the background of a text field is indeed the color of the region inside it's borders, and I don't think you will find very many people to dispute that as an intuitive conclusion - therefore having background apply to that region obeys the rule of least surprise for the API user.