Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Swing / AWT / SWT
How do I set the opacity of a JLabel/JLabel's Image?
Dan Kao
Greenhorn
Posts: 18
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm trying to make a JLabel with an image fade in from transparent to fully visible.
Is this possible with the JLabel itself or would I have to change the Image?
If I'd have to change the Image's opacity how would I do this?
Thanks
Rob Camick
Rancher
Posts: 3265
30
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is some old code I found on the web that shows one way to do this:
import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import java.io.*; import java.net.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class TransparentImage extends JPanel { private BufferedImage backImage, frontImage; private float alpha = 1; public TransparentImage() { try { backImage = ImageIO.read(new File("mong.jpg") ); frontImage = ImageIO.read(new File("dukeWaveRed.gif") ); } catch(Exception e) { System.out.println(e); } } @Override public Dimension getPreferredSize() { return new Dimension(backImage.getWidth(), backImage.getHeight()); } public void setAlpha(float alpha) { this.alpha = alpha; repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // Paint background image Graphics2D g2 = (Graphics2D) g; int x = (getWidth() - backImage.getWidth())/2; int y = (getHeight()- backImage.getHeight())/2; g2.drawRenderedImage(backImage, AffineTransform.getTranslateInstance(x, y)); // Paint foreground image with appropriate alpha value Composite old = g2.getComposite(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); x = (getWidth() - frontImage.getWidth())/2; y = (getHeight()- frontImage.getHeight())/2; g2.drawRenderedImage(frontImage, AffineTransform.getTranslateInstance(x, y)); g2.setComposite(old); } private static void createAndShowUI() { final TransparentImage app = new TransparentImage(); JSlider slider = new JSlider(); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { JSlider source = (JSlider) e.getSource(); app.setAlpha(source.getValue()/100f); } }); slider.setValue(100); JFrame frame = new JFrame("Transparent Image"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( app ); frame.add(slider, BorderLayout.SOUTH); frame.setLocationByPlatform( true ); frame.pack(); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Use the slider to change the opacity of the foreground image.
Dan Kao
Greenhorn
Posts: 18
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks, works well.
Sorry to bother, but how would I make a loop to increase the value of
alpha
from 0 to 1 manually without a slider?
Rob Camick
Rancher
Posts: 3265
30
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Use a
Swing Timer
.
Dan Kao
Greenhorn
Posts: 18
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks, did that
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How do I place image in the background of JFrame ?
resize icon to fill jLabel surface
Code compiles but gives error when I run it
Image Opacity
Dynamically changing style opacity not reflecting in Safari
More...