【问题标题】:Jbutton over Jbutton background image?Jbutton 超过 Jbutton 背景图像?
【发布时间】:2012-07-15 08:49:44
【问题描述】:

假设我用 jbuttons 创建了一个 2d 瓦片地图,然后在地图顶部创建了单位,当单位(也是一个 jbutton)位于瓦片顶部时,有没有办法显示地图的背景,因为如何现在是单位的背景只是被染成红色,那么是否可以使用 jbuttons 而不是 jbuttons 来做到这一点?

【问题讨论】:

标签: java swing jpanel jbutton sprite


【解决方案1】:

如果最上面的 JButton 是 Translucent 可以解决您的目的,这里有一个示例代码,您可以如何做到这一点。只需将 AlphaComposite 值(在我的例子中使用的 0.7f)更改为适合您的代码实例的任何值:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.FlowLayout;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.URL;

import javax.swing.*;

public class TransparentButton
{       
    private CustomButton button;
    private ImageIcon backgroundImage;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Transparent Button");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBackground(Color.BLUE);

        try
        {
            backgroundImage = new ImageIcon(
                    new URL("http://gagandeepbali.uk.to/" + 
                            "gaganisonline/images/404error.jpg"));
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }

        JButton baseButton = new JButton(backgroundImage);
        baseButton.setOpaque(true);
        baseButton.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        button = new CustomButton("Transparent Button");
        baseButton.add(button);

        contentPane.add(baseButton);
        frame.setContentPane(contentPane);
        frame.setSize(300, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TransparentButton().displayGUI();
            }
        });
    }
}

class CustomButton extends JButton
{
    private BufferedImage buttonImage = null;

    public CustomButton(String title)
    {
        super(title);
        setOpaque(false);
    }

    @Override
    public void paint(Graphics g)
    {
        if (buttonImage == null ||
                buttonImage.getWidth() != getWidth() ||
                    buttonImage.getHeight() != getHeight())
        {
            buttonImage = (BufferedImage) createImage(
                                getWidth(), getHeight());                               
        }   

        Graphics gButton = buttonImage.getGraphics();
        gButton.setClip(g.getClip());
        super.paint(gButton);
        /*
         * Make the graphics object sent to 
         * this paint() method translucent.
         */     
        Graphics2D g2 = (Graphics2D) g;
        AlphaComposite newComposite = 
            AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, 0.7f);
        g2.setComposite(newComposite);      
        /*
         * Copy the JButton's image to the destination
         * graphics, translucently.      
         */         
        g2.drawImage(buttonImage, 0, 0, null);          
    }
}

这是相同的输出:

【讨论】:

  • 不是我要找的东西,它更像是背景上的一个单元,并且显示背景为空的单元图像,所以基本上就像另一个图像顶部的叠加图像,但感谢您的帮助。
  • 似乎对于您想要的场景,JButton 确实不是正确的选择,正如@MadProgrammer 所描述的那样,我想答案很重要:-)
  • @GagandeepBali 很棒,例如 +1
  • @joeyrohan:谢谢你,保持微笑:-)
【解决方案2】:

可能的,是的,可取的,啊,可能不会。

我相信您需要将标题按钮的布局更改为您可以控制的内容(这将取决于您的视觉要求)。

就我个人而言,我可能会选择一个带有标签的面板,使用鼠标侦听器来监视鼠标操作,并可能使用输入/操作映射来进行键盘交互。

Jbuttons 只是 jcomponent,所以它们获得了 jcomponents 的所有功能

【讨论】:

  • 是的,我知道,但我的其他队友使用 jbuttons 而不是 jbuttons,我想看看是否有一种类似于 jpanels 的简单方法
  • 看,没关系。 jbuttin 只是一个 jcomponent,这意味着您可以向其中添加其他组件并调整布局以满足您的需求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
相关资源
最近更新 更多