0

hi!
I am designing a 2d game, in my game i want to add a feature e.g. in my jframe i have three images if a user clicks on a image that image will automatically get set as a background throughout the three laves (in my game they are three lavels) . but if a user dont choose one of three backgrounds then default background should be applied (defalt background is already they)..... does any one know ow to implement this feature

4
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by 117
0

you could also add a MouseListener to every component where an image is shown (maybe a JPanel or JButton) and whenever they are pressed set the background image to the frame depending on which image is clicked

Edited by zeroliken: added info

0

This could be done, as mentioned above with help from a JButton.
Or 3 JButtons.

Code-Snippet:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Class01 extends JFrame implements ActionListener 
{
    private JPanel contentPane;
    private JLabel bg;
    private Icon[] wallpaper;
    private JButton[] btn = new JButton[4];

    public Class01()
    {
        //
        // Creating the JFrame
        //
        setTitle("Java - Class01");
        setLocationRelativeTo(null);
        setSize(800,600);
        setResizable(true)
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);


        //
        // Creating the contentPane
        //
        contentPane = new JPanel();
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.setBackground(Color.BLUE); /* Example */
        contentPane.setOpaque(true);
        contentPane.setVisible(true);


       ///////////////////
       //    ARRAYS     //
       ///////////////////

       //
       // Initializing images
       //
       for (int i=0; i<wallpaper.length; i++)
       {
           try
           {
               wallpaper[i] = new ImageIcon(
               getClass().getResource("images/wallpaper"+i+".png");

               // Note: getClass().getResource(String);
               // -will access images inside the jar file.
           }
           catch (Exception ex) {}
       }

       //
       // Initializing Buttons
       //
       for (int i=1; i<btn.length; i++)
       {
           btn[i] = new JButton(); // Specify image inside the parameters, if any.
           btn[i].addActionListener(this);
           btn[i].setSize("100,100");
           btn[i].setOpaque(false);
           btn[i].setVisible(true);
       }

       JLabel bg = new JLabel();
       bg.setOpaque(true);
       bg.setVisible(true);

       contentPane.add(bg, BorderLayout.CENTER);
       repaint();

   }


   public void actionPerformed(ActionEvent e)
   {
       if (e.getSource() == btn[1])
       {
           bg = new JLabel(wallpaper[1]);
       }

       else if (e.getSource() == btn[2])
       {
           bg = new JLabel(wallpaper[2]);
       }

       else if (e.getSource() == btn[3])
       {
           bg = new JLabel(wallpaper[3]);
       }

   /** Main Method **/

   public static void main(String[] args)
   {
       java.awt.EventQueue.invokeLater(new Runnable()
       {
           public void run()
           {
               JFrame.setDefaultLookAndFeelDecorated(true);
               JDialog.setDefaultLookAndFeelDecorated(true);
               try
               {
                   UIManager.setLookAndFeel(
                   "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
               }
               catch (Exception ex)
               {
                   ex.printStackTrace();
               }

               new Class01();
           }
       });

   }
}

Hope that helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.