Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default set resize for clicked card image in Memory Matching Learning Game

    When I run my memory matching learning game, 2 cards that match stay face up, until the rest of the cards on the game screen are flipped and matched. But when I'm doing that, the images stretch, crunch, and resize out of control. I want to set a specific size for each clicked-on card. I suspect that I'm supposed to do it in the method that populates the game screen with face-down cards. Here is the method:

    ***I NEED TO DYNAMICALLY SET A PERMANENT RESIZE FOR ALL CARD IMAGES, SO EACH CARD STAYS THE SAME SIZE UNTIL THE GAME IS OVER. I BELIEVE IT SHOULD BE DONE SOMEWHERE WITHIN LINES 67 - 74.

    Java Code:
    private void createGameScreen(ArrayList <Card> cards) {
          pnlGameScreen.removeAll();
          
          pnlGameScreen.setBackground(Color.CYAN);
          allMatch = 0;
                
          pnlGameScreen.setLayout(new GridBagLayout());
          
          
          Insets cardPadding = new Insets(3,3,3,3);
          int a = 0;
          
          int rows = cards.size() / 3;
          int cols = cards.size() / rows;
          for(int row = 0; row < rows; row++){
                
                for(int col = 0; col < cols; col++){              
                Card card = cards.get(a);
                if (cardsMap == null) {
                    cardsMap = new HashMap<>();
                }
                
                JPanel pnlCard = new JPanel();
                    
                cardsMap.put(pnlCard, card);
                    
                pnlCard.addMouseListener(new MouseListener() {
    
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            
                        }
    
                        @Override
                        public void mousePressed(MouseEvent e) {
                            
                        }
    
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            flipCardEvent(e);
                        }
    
                        @Override
                        public void mouseEntered(MouseEvent e) {
                            
                        }
    
                        @Override
                        public void mouseExited(MouseEvent e) {
                            
                        }
                    });
                    
                    pnlCard.setLayout(new GridBagLayout());
                    JLabel lblImage = new JLabel();        
                                    
                    if (card.isShowing()) {
                        lblImage.setIcon(card.getfrontImage());
                    } else {
                        lblImage.setIcon(card.getbackImage());
                        
                    }
                    lblImage.addComponentListener(new ComponentListener() {
    
                      /*what am I missing here?*/
                        @Override
                        public void componentResized(ComponentEvent e) {
                            JLabel lbl = ((JLabel) e.getSource());
                            ImageIcon imgIcon = (ImageIcon) lbl.getIcon();
                            Image dimg = imgIcon.getImage().getScaledInstance(lbl.getWidth(), lbl.getHeight(),
                                Image.SCALE_SMOOTH);
                            lbl.setIcon(new ImageIcon(dimg));
                            lbl.revalidate();
                        }
    
                        @Override
                        public void componentMoved(ComponentEvent e) {}
    
                        @Override
                        public void componentShown(ComponentEvent e) {}
    
                        @Override
                        public void componentHidden(ComponentEvent e) {}
                    });
                    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridy = 0;
                    gbc.gridx = 0;           
                    gbc.fill = GridBagConstraints.BOTH;
                    gbc.weightx = 1;
                    gbc.weighty = 1;
                    
                    pnlCard.add(lblImage, gbc);
                    
                    pnlCard.setBackground(Color.RED);
                    
                    gbc = new GridBagConstraints();
                    gbc.gridy = row;
                    gbc.gridx = col;         
                    gbc.fill = GridBagConstraints.BOTH;
                    gbc.weightx = .1;
                    gbc.weighty = .1;
                    gbc.insets = cardPadding;
                    pnlGameScreen.add(pnlCard, gbc);
                    a++;
              }
            }
          
           pnlGameScreen.revalidate();
           }
    Note that a card shows as an image inside of a JLabel, which is on a JPanel. The JPanel has a red background.
    Last edited by SamJava_the_Hut; 12-15-2013 at 01:05 AM. Reason: I just learned that a you shouldn't set a size for a JLabel, since the image will squeeze into it anyway.

  2. #2
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: set resize for clicked card image in Memory Matching Learning Game

    I got my code to work! When I click on the cards, they stay the same size. I think this did it:

    Java Code:
    pnlCard.setLayout(new GridBagLayout());
                    JLabel lblImage = new JLabel();      
                    
                    lblImage.setPreferredSize(new Dimension(144, 216));
                    lblImage.setMinimumSize(new Dimension(144, 216));
                    lblImage.setMaximumSize(new Dimension(144, 216));                
                                    
                    if (card.isShowing()) {
                        lblImage.setIcon(card.getfrontImage());
                    } else {
                        lblImage.setIcon(card.getbackImage());                    
                    }

Similar Threads

  1. Problem with Memory Matching Game
    By thesonyuh in forum New To Java
    Replies: 6
    Last Post: 09-24-2011, 05:59 AM
  2. Card memory game applet.
    By Soufly in forum AWT / Swing
    Replies: 2
    Last Post: 04-21-2011, 05:02 AM
  3. Need help with matching game
    By soundymcsoundsound in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 12:09 AM
  4. Replies: 1
    Last Post: 12-02-2009, 04:39 PM
  5. Replies: 44
    Last Post: 10-19-2009, 09:07 PM

Posting Permissions

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