Results 1 to 4 of 4
  1. #1
    marco is offline Member
    Join Date
    Jul 2007
    Posts
    23
    Rep Power
    0

    Default Help Loading Up Pictures

    i need an image format that can have like transparent/ non-visible pixels on it, reason being is to import the sprite of a character onto the template drawn by a background picture also imported
    a *.jpg doesnt work coz it solidifies the non-visible colors behind it :/ and a *.gif completely screws up the background template for some reason even if it only has 1 frame :mad:
    please help

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.
    Java 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);
        }
    }

  3. #3
    marco is offline Member
    Join Date
    Jul 2007
    Posts
    23
    Rep Power
    0

    Default

    sweet - it all works.. is there anyway to do it with the image.getToolKit syntax for it?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    is there anyway to do it with the image.getToolKit syntax for it?
    For making the BufferedImages? The closest thing is the Component createImage method. Check the Method Detail section for this method in the Component api for details about its use (more restricted than BufferedImage way shown above).
    Java Code:
    Component c ...
    // after c is realized and visible
    int w = ..., h = ...
    (BufferedImage)c.createImage(w, h);

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 10:51 PM
  2. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 10:33 AM
  3. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 09:14 AM
  4. Help with pictures en Java
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:36 AM
  5. Loading of JSP file failed
    By Heather in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-06-2007, 02:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •