首页 > 解决方案 > 在 JButton 上重叠 ImageIcons

问题描述

我正在用 Java Swing 开发一个游戏的 GUI:boardGame 的框架是一个 GridLayout,每个单元格中都有一个 JButton。该单元格具有从 0 到 3 的属性“高度”,如果我单击一个按钮,我想增加该高度。特别是我想将该JButton(使用.setIcon(“image.png”)设置)上的前一个imageIcon与下一层的另一个透明imageIcon(因此,下一个高度)重叠,以便在视觉上给出另一个高度。

像这样的东西:
类似的东西

在创建 boardGame 的类中:

 battlefieldPanel.setLayout(new GridLayout(5,5));
        for(int j=4; j>-1 ; j--){
            for(int i=0; i<5; i++){
                //here i create a button for every cell
                buttons[i][j] = new CellButton();               //a class that extends JButton
                buttons[i][j].setCell(new Cell(i,j));
                buttons[i][j].setSize(100,100);                 //imageIcons are already 100x100 pixels
                buttons[i][j].setBorderPainted(false);   
                buttons[i][j].setContentAreaFilled(false);      //give transparency to the button
                buttons[i][j].setIcon("level0.png");            //initial image of height 0
                buttons[i][j].getCell().setHeight(0);
                battlefieldPanel.add(buttons[i][j]);
                buttons[i][j].addActionListener(new ButtonHandler(buttons[i][j], pics));

在处理单击 CellButton 的类中:

public class ButtonHandler implements ActionListener {                                           
[...]                                   
@Override
public void actionPerformed(ActionEvent e) {
   incrementHeight();
}

public void incrementHeight() throws CellHeightException, ReachHeightLimitException, IOException {

      JLayeredPane jlp = new JLayeredPane();
      SpringLayout lpLayout = new SpringLayout();
      jlp.setLayout(lpLayout);

        if (button.getCell().getHeight() == 0) {

            ////////FIRST TRY//////////
            URL urlBG = new URL("url1");
            URL urlFG = new URL("url2");
            final BufferedImage imgBG = ImageIO.read(urlBG);
            final BufferedImage imgFG = ImageIO.read(urlFG);

            final BufferedImage combinedImage = new BufferedImage(
                    imgBG.getWidth(),
                    imgBG.getHeight(),
                    BufferedImage.TYPE_INT_ARGB );
            Graphics2D g = combinedImage.createGraphics();
            g.drawImage(imgBG,0,0,null);
            g.drawImage(imgFG,0,0,null);
            g.dispose();

             button.setIcon(new ImageIcon(combinedImage));

            /////////SECOND TRY//////////////
            BufferedImage bigIcon =  ImageIO.read(new File("level0.png"));
            BufferedImage smallIcon = ImageIO.read(new File("level1.png"));
            BufferedImage finalIcon = new BufferedImage(bigIcon.getWidth(), bigIcon.getWidth(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = finalIcon.getGraphics();
            g.drawImage(bigIcon,0,0,null);
            g.drawImage(smallIcon,0,0,null);
            button.add(new JLabel(new Imagecon(finalIcon)));
            g.dispose();

            //////THIRD TRY//////////
            JLayeredPane jlp = new JLayeredPane();
            SpringLayout lpLayout = new SpringLayout();
            jlp.setLayout(lpLayout);
            JLabel l0 = new JLabel(pics[0].getIcon()) ; //l0.setBounds(0,0,100,100); 
            l0.setOpaque(true);
            JLabel l1 = new JLabel(pics[1].getIcon()) ; //l1.setBounds(0,0,100,100); 
            l1.setOpaque(true);
            jlp.add(l0);
            jlp.add(l1);
            lpLayout.putConstraint(SpringLayout.NORTH, l0, 0, SpringLayout.NORTH, jlp);
            lpLayout.putConstraint(SpringLayout.SOUTH, l0, 0, SpringLayout.SOUTH, jlp);
            lpLayout.putConstraint(SpringLayout.WEST, l0, 0, SpringLayout.WEST, jlp);
            lpLayout.putConstraint(SpringLayout.EAST, l0, 0, SpringLayout.EAST, jlp);
            lpLayout.putConstraint(SpringLayout.NORTH, l1, 0, SpringLayout.NORTH, jlp);
            lpLayout.putConstraint(SpringLayout.SOUTH, l1, 0, SpringLayout.SOUTH, jlp);
            lpLayout.putConstraint(SpringLayout.WEST, l1, 0, SpringLayout.WEST, jlp);
            lpLayout.putConstraint(SpringLayout.EAST, l1, 0, SpringLayout.EAST, jlp);
            button.setOpaque(true);
            //jlp.add(button);
            button.add(jlp);
            jlp.setVisible(true);

            button.getCell().incrementHeight();   //ai the edn i call the real method that handles the backend
        }

标签: javaswingjbuttonoverlapimageicon

解决方案


推荐阅读