-1

I'm a third grade computer engineer student and I'm trying to do a game project. I added a background image to my JFrame. And I tried to make the other panels transparent which I added to frame. I use alpha value for this, Ex: new Color(0,0,0,125). I olso use cardLayout in my program and for every calling a new segment or new page at the center panel; alphavalue takes the whole panels transparency and implement it to the selected panel and it creates a problem. Example : I have 7 buttons at left panel and when I click crimes button, crime panel comes to the center panel and left panel comes inside of center panel with buttons again(transparently).

I have 16 classes, so I only added main class. Sorry for bad grammar. I hope you can understand me and help me.

    import java.util.*;

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

import java.awt.*;
import java.awt.event.*;

public class TheMafia {

    public static ImageIcon scale(ImageIcon i,int x,int y) {
        Image img = i.getImage();
        Image newimg = img.getScaledInstance(x,y,Image.SCALE_SMOOTH);
        i = new ImageIcon(newimg);
        return i;
    }

    public static void setButton (JButton b,int x,int y) {
        b.setPreferredSize(new Dimension(x,y));
        b.setBackground(Color.gray);
        b.setForeground(Color.white);
        b.setBorder(new LineBorder(Color.black,1));
        b.setFont(new Font("Serif",Font.BOLD,18));
    }

    public static void main(String[] args) {

        ImageIcon home2 = new ImageIcon("home.jpg");
        home2 = scale(home2,1366,768);
        JFrame theMafia = new JFrame();
        theMafia.setTitle("The Mafia Game - Best game in the world!");
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        theMafia.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theMafia.setContentPane(new JLabel(home2));
        theMafia.setLayout(new BorderLayout());
        //theMafia.setLayout(new FlowLayout());
        //theMafia.setExtendedState(JFrame.MAXIMIZED_BOTH);
        theMafia.setSize(800,700);
        theMafia.setLocationRelativeTo(null);
        theMafia.setVisible(true);


        p1.setBackground(new Color(0,0,0,35));
        p2.setBackground(new Color(0,0,0,65));
        p1.setPreferredSize(new Dimension(250,150));
        p2.setPreferredSize(new Dimension(250,150));
        //theMafia.add(p1);
        //theMafia.add(p2);







        // kullanıcı oluşturuldu
        User u1 = new User();

        // suçlar oluşturuldu
        Crime c1 = new Crime();
        c1.setName("Yaşlı Kadın");
        c1.setDifficulty(5);
        c1.setStrength(1);
        c1.setMoney(11);

        Crime c2 = new Crime();
        c2.setName("Dükkan Hırsızlığı");
        c2.setDifficulty(10);
        c2.setStrength(3);
        c2.setMoney(67);

        Crime c3 = new Crime();
        c3.setName("Araba Hırsızlığı");
        c3.setDifficulty(20);
        c3.setStrength(6);
        c3.setMoney(133);










        // suçun seçilmesi
        final JPanel crimes = new JPanel(new CardLayout());
        //crimes.setBackground(new Color(0,0,0,65));

        ImageIcon suçişle = new ImageIcon("suçişle.jpg");
        suçişle = scale(suçişle,50,50);
        JButton yap = new JButton("Suçu işle!",suçişle);
        setButton(yap,100,65);

        JPanel crime1 = new JPanel(new GridLayout(2,1));
        crime1.setBackground(new Color(0,0,0,35));
        crime1.setForeground(Color.white);

        JLabel crime1Info = new JLabel("Suç : "+c1.getName()+"\n Para : "+c1.getMoney()+"\n Yapabilme ihtimali : "+c1.getCapable()+"\n Güç : "+c1.getStrength());
        crime1Info.setFont(new Font("Serif",Font.BOLD,15));

        crime1.add(crime1Info);
        crime1.add(yap);

        JPanel crime2 = new JPanel(new GridLayout(2,1));
        crime2.setBackground(new Color(0,0,0,35));
        crime2.setForeground(Color.white);

        JLabel crime2Info = new JLabel("Suç : "+c2.getName()+"\n Para : "+c2.getMoney()+"\n Yapabilme ihtimali : "+c2.getCapable()+"\n Güç : "+c2.getStrength());
        crime2Info.setFont(new Font("Serif",Font.BOLD,15));

        crime2.add(crime2Info);
        crime2.add(yap);

        JPanel crime3 = new JPanel();
        crime3.setBackground(new Color(0,0,0,35));
        crime3.setForeground(Color.white);

        JLabel crime3Info = new JLabel("Suç : "+c3.getName()+"\n Para : "+c3.getMoney()+"\n Yapabilme ihtimali : "+c3.getCapable()+"\n Güç : "+c3.getStrength());
        crime2Info.setFont(new Font("Serif",Font.BOLD,15));

        crime3.add(crime3Info);
        crime3.add(yap);

        crimes.add(crime1,c1.getName());
        crimes.add(crime2,c2.getName());
        crimes.add(crime3,c3.getName());

        String crimesNames [] =   {c1.getName(),c2.getName(),c3.getName()};
        JComboBox crimesbox = new JComboBox(crimesNames);
        crimesbox.setEditable(false);
        crimesbox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt) {
                CardLayout cl = (CardLayout) (crimes.getLayout());
                cl.show(crimes,(String)evt.getItem());
            }
        });

        // menu
        final JPanel menus = new JPanel(new CardLayout());
        //menus.setBackground(new Color(0,0,0,35));
        // crime
        JPanel crime = new JPanel(new BorderLayout());
        crime.setBackground(new Color(0,0,0,35));
        crime.add(crimesbox,BorderLayout.PAGE_START);
        crime.add(crimes,BorderLayout.SOUTH);

        ImageIcon crimeimage = new ImageIcon("thief.png");
        crimeimage = scale(crimeimage,50,50);

        final JButton crimeButton = new JButton("Suçlar",crimeimage);
        setButton(crimeButton,178,76);

        crimeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) (menus.getLayout());
                if (e.getSource() == crimeButton) {
                    cl.show(menus,"suç");
                }
            }
        });

        // weapon shop
        JPanel weaponShop = new JPanel();
        //weaponShop.setBackground(new Color(0,0,0,125));

        final JButton weaponShopButton = new JButton("Silah Dükkanı");
        setButton(weaponShopButton,178,76);

        // building
        JPanel buildingPanel = new JPanel();
        //buildingPanel.setBackground(new Color(0,0,0,125));

        final JButton buildingButton = new JButton("Binalar");
        setButton(buildingButton,178,76);

        // nightlife
        JPanel nightLife = new JPanel();
        //nightLife.setBackground(new Color(0,0,0,35));

        final JButton nightLifeButton = new JButton("Gece Hayatı");
        setButton(nightLifeButton,178,76);

        // treatment center
        JPanel treatmentCenter = new JPanel();
        //treatmentCenter.setBackground(new Color(0,0,0,35));

        final JButton treatmentCenterButton = new JButton("Tedavi Merkezi");
        setButton(treatmentCenterButton,178,76);

        // casino
        JPanel casinoPanel = new JPanel();
        //casinoPanel.setBackground(new Color(0,0,0,35));

        final JButton casinoButton = new JButton("Gazino");
        setButton(casinoButton,178,76);

        // home page
        JPanel home = new JPanel();
        home.setBackground(new Color(0,0,0,35));

        ImageIcon homeimage = new ImageIcon("home.jpg");
        homeimage = scale(homeimage,1200,800);

        JLabel homelabel= new JLabel();
        home.add(homelabel);

        ImageIcon homeicon = new ImageIcon("home_icon.png");
        homeicon = scale(homeicon,50,50);

        final JButton homeButton = new JButton("Home",homeicon);
        setButton(homeButton,178,76);

        homeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) (menus.getLayout());
                if (e.getSource() == homeButton) {
                    cl.show(menus,"home");
                }
            }
        });

        menus.add(home,"home");
        menus.add(crime,"suç");
        menus.add(weaponShop,"silahDükkanı");
        menus.add(buildingPanel,"bina");
        menus.add(nightLife,"geceHayatı");
        menus.add(treatmentCenter,"TedaviMerkezi");
        menus.add(casinoPanel,"gazino");

        Color grisi=new Color(13,13,13);
        JPanel menusButton = new JPanel(new GridLayout(10,1));
        //menusButton.setBackground(grisi);
        menusButton.add(homeButton);
        menusButton.add(crimeButton);
        menusButton.add(weaponShopButton);
        menusButton.add(buildingButton);
        menusButton.add(nightLifeButton);
        menusButton.add(treatmentCenterButton);
        menusButton.add(casinoButton);
        menusButton.setOpaque(false);





        theMafia.add(menusButton,BorderLayout.WEST);
        theMafia.add(menus,BorderLayout.CENTER);
    }
}
StarsSky
  • 6,721
  • 6
  • 38
  • 63

1 Answers1

1

Swing does not handle transparent background properly. Swing expects components to be either opaque or non-opaque and the transparency causes a problem because the component is neither.

Check out Background With Transparency for more information and a couple of solutions to solve the problem.

camickr
  • 321,443
  • 19
  • 166
  • 288