Results 1 to 4 of 4
  1. #1
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Question [SOLVED] Best way to overlap Images?

    Hello, I'm writting a program that involves playing cards.
    With more players and more cards delt the card dispaly area begins to grow too large so I want to have each new card delt to overlap that persons previous card only slightly to the right so you can see both.
    Right now I just have a panel for each palyers area and when they draw a card it simply adds the Card image as a JLabel.

    Anyone have advice on the best way to go about this?
    Thank You

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default

    How about using a imageicons on jlabels as you're doing, and place them in a JLayeredPane?

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

    Default

    advice on the best way to go about this
    Depends on how you want to design things.
    Two other options are drawing the cards in a graphic component and using zOrder to overlap components.
    Here's a quick example of each:
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    
    public class StackingCards {
        private JPanel getContent(BufferedImage[] images) {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(new GraphicComponent(images), gbc);
            panel.add(getZOrderComponent(images), gbc);
            return panel;
        }
    
        private JPanel getZOrderComponent(BufferedImage[] images) {
            JPanel panel = new JPanel(null);
            Dimension d = new Dimension(300,100);
            panel.setPreferredSize(d);
            int iw = images[0].getWidth();
            int ih = images[0].getHeight();
            int overlap = iw/4;
            int x0 = (d.width - iw - (images.length-1)*overlap)/2;
            int y = (d.height - ih)/2;
            for(int i = 0; i < images.length; i++) {
                JLabel label = new JLabel(new ImageIcon(images[i]));
                panel.add(label);
                int x = x0 + i*overlap;
                label.setBounds(x, y, iw, ih);
                panel.setComponentZOrder(label, 0);
            }
            Component[] c = panel.getComponents();
            for(int i = 0; i < c.length; i++) {
                System.out.printf("c[%d] zOrder = %d%n",
                                   i, panel.getComponentZOrder(c[i]));
            }
            return panel;
        }
    
        public static void main(String[] args) throws IOException {
            String prefix = "images/geek/geek";
            String[] ids = {
                "-----", "-c---", "--g--", "---h-", "----t"
            };
            String ext = ".gif";
            BufferedImage[] images = new BufferedImage[ids.length];
            for(int i = 0; i < images.length; i++) {
                String path = prefix + ids[i] + ext;
                images[i] = javax.imageio.ImageIO.read(new File(path));
            }
            StackingCards test = new StackingCards();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent(images));
            f.pack();
            f.setVisible(true);
        }
    }
    
    class GraphicComponent extends JPanel {
        BufferedImage[] images;
    
        public GraphicComponent(BufferedImage[] images) {
            this.images = images;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            int iw = images[0].getWidth();
            int ih = images[0].getHeight();
            int overlap = iw/4;
            int x0 = (w - iw - (images.length-1)*overlap)/2;
            int y = (h - ih)/2;
            for(int i = 0; i < images.length; i++) {
                int x = x0 + i*overlap;
                g.drawImage(images[i], x, y, this);
            }
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(300,100);
        }
    }
    geek images

  4. #4
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default

    Ahh thank you! This is a side project I'm working on at work, so I haven't had time to try this out but it looks great!

Similar Threads

  1. XML Images
    By JavaWizz in forum XML
    Replies: 1
    Last Post: 10-17-2008, 10:19 AM
  2. images
    By amith in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2008, 08:38 PM
  3. images
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 10:54 AM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 06:01 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
  •