Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-05-2009, 08:42 PM
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
Krooger is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-06-2009, 12:27 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,451
Rep Power: 8
Fubarable is on a distinguished road
Default
How about using a imageicons on jlabels as you're doing, and place them in a JLayeredPane?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-06-2009, 05:16 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
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:
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-08-2009, 07:12 PM
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
Krooger is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Images JavaWizz XML 1 10-17-2008 11:19 AM
images amith AWT / Swing 3 06-27-2008 09:38 PM
images amith AWT / Swing 1 05-20-2008 11:54 AM
Help with images... toby Java Applets 1 08-04-2007 06:25 AM
Images in JSP Daniel JavaServer Pages (JSP) and JSTL 1 06-05-2007 07:01 AM


All times are GMT +2. The time now is 03:32 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org