This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 29th, 2011  |  syntax: Java  |  size: 2.30 KB  |  views: 109  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.AlphaComposite;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.IOException;
  8. import java.net.URL;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15.  
  16. public class ChangeColourTest extends JPanel {
  17.  
  18.     // Main
  19.     public static void main(String[] args) {
  20.         JFrame jf = new JFrame("Test");
  21.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         jf.setPreferredSize(new Dimension(150, 150));
  23.        
  24.         JPanel under = new JPanel();
  25.         under.setBackground(Color.green);
  26.         under.setPreferredSize(new Dimension(150, 150));      
  27. //        jf.getLayeredPane().add(under, -1);
  28.        
  29.         ChangeColourTest cct = new ChangeColourTest( new Color(0xFFFF0000) );
  30.         cct.setOpaque(false);
  31.         jf.add(cct);
  32.        
  33.         jf.pack();
  34.         jf.setVisible(true);
  35.     }
  36.    
  37.     // Holders
  38.     private BufferedImage image;
  39.     private Color color;
  40.    
  41.     // Constructor
  42.     public ChangeColourTest( Color c )
  43.     {
  44.         setBackground(Color.pink);
  45.         // Args
  46.         color = c;
  47.        
  48.         setPreferredSize(new Dimension(150,150));
  49.        
  50.         // Load the image
  51.         try {
  52.             image = ImageIO.read(new URL("http://i.imgur.com/Pu6iq.png")); /* A black 'S' on a transparent background. */            
  53.         } catch (IOException ex) {
  54.             System.err.println("IOException");
  55.         }      
  56.        
  57.         // create red image
  58.         BufferedImage redVersion = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
  59.         Graphics2D g = (Graphics2D) redVersion.getGraphics();
  60.         g.setColor(Color.red);
  61.         g.fillRect(0, 0, image.getWidth(), image.getHeight());
  62.  
  63.         // paint original with composite
  64.         g.setComposite(AlphaComposite.DstIn);
  65.         g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
  66.  
  67.         JLabel label = new JLabel();
  68.         label.setPreferredSize(new Dimension(150, 150));
  69.         label.setIcon(new ImageIcon(redVersion));
  70.         add(label);
  71.     }    
  72.    
  73. }
clone this paste RAW Paste Data