背景图片帮助,JPanel,JFrame,什么?

时间:2014-11-28 09:33:03

标签: java image swing background jlabel

我想为我正在设计的游戏制作背景图片,但我无法找到正确的方法来获得我想要的效果,我想要一个可以在文本后面看到的背景等基本上有背景覆盖整个JFrame / JPanel而不仅仅是布局的一个部分(例如BorderLayout.Center)我认为无论如何它都会这样做但是如果确实如此,我如何为透明的背景制作背景以查看背后的背景......

让我感到困惑,但我希望有人能够理解我想要做的事情并且可以提供帮助......这是我现在的代码。我一直在玩背景,所以我没有读过如何写它。

import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;

public class GamePanel extends JPanel {

private JTextPane playertext;
private JTextField wealthstring, currentwealth;

public GamePanel() {

    super();
    setLayout(new BorderLayout());
    setBackground(Game.getBackgroundColor());
    Border raised = BorderFactory.createRaisedBevelBorder();
    Border lowered = BorderFactory.createLoweredBevelBorder();
    setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(4, 4, 4, 4), (BorderFactory.createCompoundBorder(raised, lowered))));
    add(northpanel(), BorderLayout.NORTH);
    add(eastpanel(), BorderLayout.EAST);


}


private JLabel northpanel() {

    Font northfont = new Font("Engravers MT", Font.BOLD, 12);
    ImageIcon banner = new ImageIcon("images/banner.png", "North Background");

    playertext = new JTextPane();
    playertext.setFont(northfont);
    playertext.setEditable(false);
    playertext.setText("Player: \n" + Game.getName());
    playertext.setBackground(Game.getBackgroundColor());
    playertext.setBorder(new EmptyBorder(10,10,10,10));

    wealthstring = new JTextField("Money: ");
    wealthstring.setFont(northfont);
    wealthstring.setEditable(false);
    wealthstring.setHorizontalAlignment(wealthstring.RIGHT);
    wealthstring.setBorder(null);
    wealthstring.setBackground(Game.getBackgroundColor());

    currentwealth = new JTextField();
    currentwealth.setFont(northfont);
    currentwealth.setEditable(false);
    currentwealth.setHorizontalAlignment(wealthstring.RIGHT);
    currentwealth.setBackground(Game.getBackgroundColor());
    currentwealth.setBorder(null);
    String wealthrounded = String.format("%.2f", Game.getMoney());
    currentwealth.setText(wealthrounded);

    JPanel wealthtext = new JPanel();
    wealthtext.setLayout(new GridLayout(2, 1));
    wealthtext.setBackground(Game.getBackgroundColor());
    wealthtext.setBorder(new EmptyBorder(10,10,10,10));
    wealthtext.add(wealthstring);
    wealthtext.add(currentwealth);

    JLabel northpanel = new JLabel();
    northpanel.setLayout(new BorderLayout());
    northpanel.setIcon(banner);
    northpanel.add(new JSeparator(), BorderLayout.PAGE_END);
    northpanel.add(playertext, BorderLayout.WEST);
    northpanel.add(wealthtext, BorderLayout.EAST);
    return northpanel;
}

private JPanel eastpanel() {


    JButton tab1 = new JButton("Tab 1");
    JButton tab2 = new JButton("Tab 2");
    JButton tab3 = new JButton("Tab 3");

    JPanel easttabs = new JPanel();
    easttabs.setLayout(new GridLayout(1, 3));
    easttabs.add(tab1);
    easttabs.add(tab2);
    easttabs.add(tab3);

    JPanel eastpanels = new JPanel();
    eastpanels.setLayout(new BorderLayout());
    eastpanels.setBackground(Game.getBackgroundColor());
    eastpanels.add(easttabs, BorderLayout.NORTH);

    return eastpanels;
}

}

2 个答案:

答案 0 :(得分:0)

创建一个从JPanel扩展的类,并在其paint方法中绘制一个图像。 现在创建一个从JFrame扩展的类,并在其构造函数中将其contentpane设置为该JPanel类的对象,如下所示。现在您可以将组件添加到jframe。

class SimpleTest extends JFrame {

    JButton btn = new JButton("A Game");

    public SimpleTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setContentPane(new CPane());
        getContentPane().setBackground(Color.red);
        getContentPane().add(btn, BorderLayout.NORTH);
        setSize(new Dimension(300, 300));
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SimpleTest();
            }
        });
    }
}

class CPane extends JPanel {

    Image back;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        try {
            back = ImageIO.read(getClass().getResource("back.jpg"));
        } catch (Exception ex) {
        }
        g.drawImage(back, 0, 0, this);
    }
}

如果你想让jpanel透明,那么

panel.setOpaque(false);

答案 1 :(得分:0)

因此,如果您已经有一个将图像作为背景的JPanel,并且您想在其上添加另一个面板,则可以使用不透明度来实现此目的。仅显示添加到此面板的组件。这是代码:

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

import javax.swing.JPanel;

public class TransparentPane extends JPanel {

    public TransparentPane() {

        setOpaque(false);

    }

    @Override
    protected void paintComponent(Graphics g) {


        super.paintComponent(g);


        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0f));

        g2d.setColor(getBackground());
        g2d.fill(getBounds());

        g2d.dispose();

    }

}