2011-10-11 48 views
1

有關:How to make a dynamic image at run time?卡片清單?

我有頁面圖像後,我想將它們顯示在列表中,以便用戶可以選擇要播放的頁面。我知道JList支持圖像,但會顯示整張圖像失去卡組的感覺。可能我只會用名字顯示圖像的邊緣,並以某種方式突出顯示它。

有什麼想法?

回答

2

我能夠拉出來。你可以從這個代碼在這裏的圖片:http://leepoint.net/notes-java/examples/graphics/cardDemo/cards20.zip

Card.java

package deck.displayer; 

import javax.swing.ImageIcon; 

/** 
* 
* @author Javier A. Ortiz <[email protected]> 
*/ 
public class Card { 

private String text; 
private ImageIcon icon; 

public Card(String text, ImageIcon icon) { 
    this.text = text; 
    this.icon = icon; 
} 

/** 
* @return the text 
*/ 
public String getText() { 
    return text; 
} 

/** 
* @return the icon 
*/ 
public ImageIcon getIcon() { 
    return icon; 
} 
} 

CardCellRenderer.java

package deck.displayer; 

import java.awt.Component; 
import java.awt.Font; 
import java.awt.image.CropImageFilter; 
import java.awt.image.FilteredImageSource; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.ListCellRenderer; 

/** 
* 
* @author Javier A. Ortiz <[email protected]> 
*/ 
public class CardCellRenderer extends JLabel implements ListCellRenderer { 

    private Font uhOhFont; 

    @Override 
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
    if (isSelected) { 
     setBackground(list.getSelectionBackground()); 
     setForeground(list.getSelectionForeground()); 
    } else { 
     setBackground(list.getBackground()); 
     setForeground(list.getForeground()); 
    } 
    Card card = (Card) value; 
    setIcon(card.getIcon()); 
    if (getIcon() != null) { 
     if (index != list.getModel().getSize() - 1) { 
      setIcon(new ImageIcon(createImage(new FilteredImageSource(((ImageIcon) getIcon()).getImage().getSource(), 
        new CropImageFilter(0, 0, getIcon().getIconWidth(), 20))))); 
     } 
     setFont(list.getFont()); 
    } else { 
     setUhOhText(card.getText() + " (no image available)", 
       list.getFont()); 
    } 
    return this; 
} 
//Set the font and text when no image was found. 

protected void setUhOhText(String uhOhText, Font normalFont) { 
    if (uhOhFont == null) { //lazily create this font 
     uhOhFont = normalFont.deriveFont(Font.ITALIC); 
    } 
    setFont(uhOhFont); 
    setText(uhOhText); 
} 
} 

Test.java

package deck.displayer; 

import java.net.URL; 
import java.util.ArrayList; 
import javax.swing.ImageIcon; 

/** 
* 
* @author Javier A. Ortiz <[email protected]> 
*/ 
public class Test extends javax.swing.JFrame { 
ArrayList<Card> cards = new ArrayList<Card>(); 

/** 
* Creates new form Test 
*/ 
public Test() { 
    try { 
     initComponents(); 
     String suits = "shdc"; 
     String faces = "a23456789tjqk"; 
     for (int suit = 0; suit < suits.length(); suit++) { 
      for (int face = 0; face < faces.length(); face++) { 
       //... Get the image from the images subdirectory. 
       String imagePath = "cards/" + faces.charAt(face) 
         + suits.charAt(suit) + ".gif"; 
       URL imageURL = this.getClass().getResource(imagePath); 
       ImageIcon img = new ImageIcon(imageURL); 

       //... Create a card and add it to the deck. 
       System.out.println("Adding: "+String.valueOf(faces.charAt(face)) 
         + String.valueOf(suits.charAt(suit))); 
       cards.add(new Card(String.valueOf(faces.charAt(face)) 
         + String.valueOf(suits.charAt(suit)), img)); 
      } 
     } 
     pageList.setCellRenderer(new CardCellRenderer()); 
     pageList.setModel(new javax.swing.AbstractListModel() { 

      @Override 
      public int getSize() { 
       return cards.size(); 
      } 

      @Override 
      public Object getElementAt(int i) { 
       return cards.get(i); 
      } 
     }); 
    } catch (Exception e) { 
     System.exit(1); 
    } 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    jScrollPane1 = new javax.swing.JScrollPane(); 
    pageList = new javax.swing.JList(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    pageList.setModel(new javax.swing.AbstractListModel() { 
     String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; 
     public int getSize() { return strings.length; } 
     public Object getElementAt(int i) { return strings[i]; } 
    }); 
    jScrollPane1.setViewportView(pageList); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE) 
      .addContainerGap()) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE) 
      .addContainerGap()) 
    ); 

    pack(); 
}// </editor-fold> 

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* 
    * Set the Nimbus look and feel 
    */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* 
    * If Nimbus (introduced in Java SE 6) is not available, stay with the 
    * default look and feel. For details see 
    * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* 
    * Create and display the form 
    */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      new Test().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JList pageList; 
// End of variables declaration 
} 

下面是輸出:

example

+0

+1 cool :-)順便說一句,你可以接受你自己的答案 – kleopatra

2

指針:您可以使用Layerd Pane

您可能要打破你的思維中,JList和再想想不同的組件,可根據您的需要的用戶界面。您不能在JList內部使用LayeredPane(是的,但是不會出現10K以上的複雜性和錯誤)。

替代指針 - 如果你那麼必須使用JList中,考慮this SO post

+0

我之前去過那條路線,但在JList中無法做到這一點。請記住,在某些時候可能有數百頁的頁面,所以Layered Pane +拖放是沒有問題的。我需要像JList那樣組織起來,看起來像甲板一樣(就像你可以用LayeredPane做的那樣) – javydreamercsw

+0

你能上傳你想要實現的圖形表示嗎?這是否像你正在尋找的'手風琴'控制? –

+0

然後,您需要使用上面提到的(1+)但沒有JList的分層窗格。你必須自己組織它。 –