Note that there are some explanatory texts on larger screens.

plurals
  1. POJlabel with Image Fade Out Strange Effect
    primarykey
    data
    text
    <p>Hope this question could emphasize more about the fading out effect of Jlabel (swing).</p> <p>Certainly, yes... I already follow some guide and some answers given from <a href="https://stackoverflow.com/questions/3270997/making-a-jlabel-fade-away">This Link Thread</a>, but mine is quite a bit different. It's not just only A text inside the JLabel, there's an image i put on.</p> <p>I proceed to follow on the <a href="http://fr.w3support.net/index.php?db=so&amp;id=1531494" rel="nofollow noreferrer">Thread Located out of stackoverflow</a>. And yet, it gives me a fade out effect. But there's horrible thing occured; <strong>the white background.</strong></p> <p><strong>How to solve this out?</strong></p> <p>I share the interface here... The <a href="http://img171.imageshack.us/img171/5964/jlabelfadingout.jpg" rel="nofollow noreferrer">First screenshot taken here</a> is the earlier phase when the fade out have not occured yet. While, The <a href="http://img824.imageshack.us/img824/5761/jlabelfadingout2.jpg" rel="nofollow noreferrer">Second screenshot taken here</a> is the unwanted result i mentioned.</p> <p>Tobe honest, I used the <strong>Trident Library</strong> to do animatiing; So, whenever the user click over the image it will execute this code;</p> <pre><code>Timeline tm = new Timeline(jll_btnProceed); tm.addPropertyToInterpolate("intensity", 1.0f, 0.0f); tm.setDuration(1000); tm.play(); </code></pre> <p>and... the JLabel itself, I used to override it using this source code;</p> <pre><code>/** * * @author Gumuruh */ public class JLabelFader extends JLabel { private float intensity = 1.0f; public void setIntensity(float intensity) { this.intensity = intensity; this.setOpaque(false); repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; final Composite oldComposite = g2.getComposite(); g2.setComposite(AlphaComposite.SrcOver); final Color c = getBackground(); final Color color = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255 * (1.0f - intensity))); g2.setColor(color); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setComposite(oldComposite); } } </code></pre> <p>My hand and my head can't stop for making this trully solved. Thus I tried to follow up some example from the <strong>Java Filthy Rich Client ebook</strong> and then applying the source code given below, but first I need to COMMENT the <strong>protected void paint(Graphic g)</strong> method written above, and simply adding this source code;</p> <pre><code>private BufferedImage buttonImage = null; public void paint(Graphics g) { // Create an image for the button graphics if necessary if (buttonImage == null || buttonImage.getWidth() != getWidth() || buttonImage.getHeight() != getHeight()) { buttonImage = getGraphicsConfiguration(). createCompatibleImage(getWidth(), getHeight()); } Graphics gButton = buttonImage.getGraphics(); gButton.setClip(g.getClip()); // Have the superclass render the button for us super.paint(gButton); // Make the graphics object sent to this paint() method translucent Graphics2D g2d = (Graphics2D) g; AlphaComposite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity); g2d.setComposite(newComposite); // Copy the button's image to the destination graphics, translucently g2d.drawImage(buttonImage, 0, 0, null); } </code></pre> <p>in which at the end... giving me nice fade out effect. But At first, it gave me the <strong>2nd horrible effect</strong> which is <strong>BLACK BACKGROUND</strong> rendered first. Can't believe me?? Okay, Here is <a href="http://img577.imageshack.us/img577/5951/jlabelfadingoutaftertak.jpg" rel="nofollow noreferrer">First screen shot AFTER applying code from ebook</a>. and here is <a href="http://img19.imageshack.us/img19/5951/jlabelfadingoutaftertak.jpg" rel="nofollow noreferrer">the nice fade out effect result</a>.</p> <p>If there's somebody telling me; "<strong>YOUR PNG IS NOT TRANSPARENT</strong>!". Please, dont say like that. Because, I followed the <a href="http://www.axialis.com/tutorials/tutorial-misc001.html" rel="nofollow noreferrer">creation of PNG inside the Photoshop nicely using this Tut</a>.</p> <p>Now, My head's spinned, but my heart laughed and can't handle it over. OMG. Geeezzz....!</p> <p>And the New Stories begun... <strong>* UPDATED FROM HERE AND BELOW *</strong></p> <p>Ehm, depply thank you very much to our friend called... <strong>MKorbel,</strong> from <a href="https://stackoverflow.com/questions/6992154/java-transparent-panels-custom-effect-on-panels/6992259#6992259">his thread given at this link</a>. Providing a clear example of fading out effect the Swing JButton and I tried to implement it into my JLabel, and violaaa...!! IT works. Let's give a big clap for <strong>MKorbel</strong>. :D</p> <p>SO anyway, how could I fix the earlier code? Pretty simple, just <strong>COMMENT the Paint()</strong> method, and <strong>use again the paintComponent() method</strong> and it should be <strong>overriden</strong> with the new source code below;</p> <pre><code>@Override public void paintComponent(java.awt.Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity)); if (rectangularLAF &amp;&amp; isBackgroundSet()) { Color c = getBackground(); g2.setColor(c); g.fillRect(0, 0, getWidth(), getHeight()); } super.paintComponent(g2); g2.dispose(); } </code></pre> <p>Now the JLabel become easy to be changed with its <strong>intensity -variable</strong>. Fading out and Fading in is accomplished.</p> <p>Okay. <strong>Now everything</strong> seems "<strong>OKAY</strong>". BUt hold a moment, there's something strangely occured again here. Have you noticed it? Hmmm.... I tried to give a <strong>Mouse Event (Hover On)</strong> into the <strong>JLabel</strong> that we override the <strong>paintComponent()</strong> method discussed earlier.With this line of code; </p> <pre><code>myJLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); </code></pre> <p>Logically, it should change the Cursor when Hover ON. But, Here comes <strong>another strange effect</strong>. (Really sorry, but this is stil the continue of the main case). The strange effect now is the <strong>Mouse Cursor can't be changed</strong> when we Hover On the Jlabel. It still can't change the Cursor. <strong>Seems the paintComponent() method effecting</strong> the way Cursor react. Is that true?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload