Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 28th, 2011  |  syntax: Java  |  size: 1.90 KB  |  hits: 72  |  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. import javax.imageio.ImageIO;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13. public class ChangeColourTest extends JPanel {
  14.  
  15.     // Main
  16.     public static void main(String[] args) {
  17.         JFrame jf = new JFrame("Test");
  18.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.         jf.setPreferredSize(new Dimension(150, 150));
  20.        
  21.         ChangeColourTest cct = new ChangeColourTest( new Color(0xFFFF0000) );
  22.         jf.getContentPane().add(cct, "Center");
  23.        
  24.         jf.pack();
  25.         jf.setVisible(true);
  26.     }
  27.    
  28.     // Holders
  29.     private BufferedImage image;
  30.     private Color color;
  31.    
  32.     // Constructor
  33.     public ChangeColourTest( Color c )
  34.     {
  35.         // Args
  36.         color = c;
  37.        
  38.         setPreferredSize(new Dimension(150,150));
  39.        
  40.         // Load the image
  41.         try {
  42.             image = ImageIO.read(new URL("http://i.imgur.com/Pu6iq.png")); /* A black 'S' on a transparent background. */            
  43.            
  44.         } catch (IOException ex) {
  45.             System.err.println("IOException");
  46.         }      
  47.     }    
  48.    
  49.     // Override paint compoenent
  50.     @Override
  51.     public void paintComponent(Graphics g)
  52.     {
  53.         Graphics2D g2D = (Graphics2D)g;
  54.        
  55.         // Paint the test image to the component and change it's colour
  56.        
  57.         // Fill red
  58.         g2D.setColor(color);
  59.         g2D.fillRect(0, 0, getSize().width, getSize().height);
  60.        
  61.         // Paint with composite
  62.         g2D.setComposite(AlphaComposite.DstIn);
  63.         g2D.drawImage( image, 0, 0, image.getWidth(), image.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
  64.        
  65.     }
  66. }