Jbutton在Jbutton背景图片上?

时间:2012-07-15 08:49:44

标签: java swing jpanel jbutton sprite

让我们说我用jbuttons创建了一个2d平铺地图,然后在地图上创建了单位,当单位(也是一个jbutton)位于平铺的顶部时,有一种方法可以显示地图的背景,因为如何它现在是单位的背景只是红色,所以可以用jbuttons jbuttons做到这一点吗?

2 个答案:

答案 0 :(得分:3)

如果最顶层的JButton Translucent可以解决您的目的,这里有一个示例代码,您将如何做到这一点。只需将我的案例中使用的AlphaComposite0.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);          
    }
}

以下是相同的输出:

TRANSPARENT BUTTON

答案 1 :(得分:2)

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

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

我个人可能会选择一个带有标签的面板,使用鼠标监听器来监控鼠标操作。可能是键盘交互的输入/动作映射。

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