View Single Post
  #2 (permalink)  
Old 08-14-2007, 01:04 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
image format that can have like transparent/ non-visible pixels on it
png works okay for this.
There may be some options depending on what you are trying to do. If loading two images and drawing them one-over-the-other/overlayed doesn't give you what you want you can draw them both into a BufferedImage to use in your app. Here's an example of how you can do it. I made up two images since you didn't sppecify any to work with.
Code:
import java.awt.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import javax.swing.*; public class OverlayImages { private ImageIcon getContent() { return new ImageIcon(getImage()); } private BufferedImage getImage() { BufferedImage bg = getOpaqueImage(); BufferedImage fg = getTranslucentImage(); int w = bg.getWidth(); int h = bg.getHeight(); int type = BufferedImage.TYPE_INT_RGB; BufferedImage image = new BufferedImage(w, h, type); Graphics2D g2 = image.createGraphics(); g2.drawImage(bg, 0, 0, null); int x = (w - fg.getWidth())/2; int y = (h - fg.getHeight())/2; g2.drawImage(fg, x, y, null); g2.dispose(); return image; } private BufferedImage getOpaqueImage() { int w = 200; int h = 140; int type = BufferedImage.TYPE_INT_RGB; BufferedImage image = new BufferedImage(w, h, type); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setBackground(Color.orange); g2.clearRect(0,0,w,h); g2.setPaint(Color.green.darker()); g2.draw(new Line2D.Double(w/16.0,h/16.0,w*15/16.0,h*15/16.0-1)); g2.draw(new Line2D.Double(w*15/16.0,h/16.0,w/16.0,h*15/16.0-1)); g2.setPaint(Color.blue); g2.draw(new Rectangle2D.Double(w/16, h/16, w*7/8, h*7/8)); g2.dispose(); return image; } private BufferedImage getTranslucentImage() { int w = 90; int h = 90; int type = BufferedImage.TYPE_INT_ARGB_PRE; BufferedImage image = new BufferedImage(w, h, type); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Ellipse2D e = new Ellipse2D.Double(10,10,w-20,h-20); g2.setPaint(Color.red); g2.fill(e); g2.setPaint(Color.blue); g2.draw(e); g2.dispose(); return image; } public static void main(String[] args) { OverlayImages test = new OverlayImages(); ImageIcon icon = test.getContent(); JOptionPane.showMessageDialog(null, icon, "", JOptionPane.PLAIN_MESSAGE); } }
Reply With Quote