Tuesday, 30 September 2014

Java Swings : JPanel : JPanel with Image Background



                                                    Working with Jpanels

 JPanels with Image Background
Using Jpanels to add components are common , we set layout and add components according to our need but what if we want to add Background Image without changing actual behaviors of JPanel.
So this section will help you to create such a JPanel


This is the first Class JPanelExample which is a JFrame. With JPanel and JButton added to it , only difference is JPanel is the customized JPanel.
Background image path is passed as an argument to construct of this new JPanel Class.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JPanelExample extends JFrame {
           
            public JPanelExample() {
                        this.setTitle("JPanel Example");
                        this.setBounds(600, 200, 300, 300);
                        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        ImageIcon titleIcon = new ImageIcon("images/titleIcon.png");
                        this.setIconImage(titleIcon.getImage());
                        JPanel pnlOuter = new PnlImage("images/background.png");
                        pnlOuter.setBackground(Color.GRAY);
                        this.setLayout(new BorderLayout());

                        JButton btnTest = new JButton("button above image panel");
                        btnTest.setBounds(140, 20, 220, 40);
                        pnlOuter.add(btnTest);

                        this.add(pnlOuter, BorderLayout.CENTER);

            }

            public static void main(String[] args) {
                        JPanelExample testFrame = new JPanelExample();
                        testFrame.setVisible(true);

            }

}




This Class below is the customized JPanel which draws images to the background of JPanel. In this class has over ridden the paint method of JPanel and used g.drawImage(img,0,0,null) method to draw image in JPanel.
So by adding below class in your project . Image panel can be used by calling same as shown in above example.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class PnlImage extends JPanel
{
            private Image img;
   
    public PnlImage(String imageFile) {
            this(new ImageIcon(imageFile).getImage());
    }
       
    public PnlImage(Image image) {
            this.img=image;
            Dimension size=new Dimension(img.getWidth(null), img.getHeight(null));
            this.setPreferredSize(size);
            setLayout(null);
    }
    
    public void paintComponent(Graphics g) {
    g.drawImage(img,0,0,null);
    }   
}




Saturday, 6 September 2014

Java Swings - working with Jframe


     A JFrame is equivalent to a window and these features will make your JFrame look as you like, make sure you consider all these parameters before developing main window of any java swing application

1.     Shape of JFrame

To change the shape of Jframe use this code
JFrame testFrame=new JFrame();
testFrame.setUndecorated(true);   
// Frame is required to set undecorated for changing shape and transparency
Dimension arcSize = new Dimension(250,250);
RoundRectangle2D rectShape = new RoundRectangle2D.Double(0d, 0d,
testFrame.getWidth(), testFrame.getHeight(),
                        arcSize.width, arcSize.height);
testFrame.setShape(rectShape);

2.     Opacity of JFrame

Add line in bold to set opacity of JFrame
JFrame testFrame=new JFrame ();
testFrame.setUndecorated(true);    // Frame is required to set undecorated for changing shape and transparency
testFrame.setOpacity(0.3f);


This image describes how jframe will look after setting opacity

3.      Title of JFrame which will alsp appear in taskbar


JFrame testFrame=new JFrame ();
testFrame.setTitle("JFrame Example");

4.     Icon for taskbar and Header icon

JFrame testFrame=new JFrame ();
testFrame.setTitle("JFrame Example");
testFrame.setBounds(600, 200, 300, 300);
ImageIcon titleIcon=new ImageIcon("E:/titleIcon.png");
testFrame.setIconImage(titleIcon.getImage());

5.     Default close operation

We must include Default close operation for windows , If this JFrame is main window for any application then program must exit properly after its close operation.
It can done in two ways.
       1. By using function setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       2. By implementing WindowListener interface and override windowClosing method
Ex. We can write  Sytem.exit(0);
public void windowClosing(WindowEvent arg0) {
            System.out.println("Window closing");
            // Here we can write closure of all pending task or save data if required
          System.exit(0);  // this must be last line
           
      }


6.     Complete Code with all parameters

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.RoundRectangle2D;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameExample extends JFrame
{
      private static final long serialVersionUID = 1L;

      public JFrameExample() {
            this.setTitle("JFrame Example");
            this.setBounds(600, 200, 300, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ImageIcon titleIcon=new ImageIcon("E:/titleIcon.png");
            this.setIconImage(titleIcon.getImage());
            JPanel pnlOuter=new JPanel();
            pnlOuter.setBackground(Color.GRAY);
            this.setLayout(new BorderLayout());
            this.add(pnlOuter,BorderLayout.CENTER);
           
      }

      public static void main(String[] args) {
            JFrameExample testFrame=new JFrameExample();
// Comment Block 1 for Example C,D,E,F,G
      // Block 1 Start
            testFrame.setUndecorated(true);    // Frame is required to set undecorated for changing shape and transparency
            Dimension arcSize = new Dimension(250,250);
RoundRectangle2D rectShape = new RoundRectangle2D.Double(0d, 0d,
                        testFrame.getWidth(), testFrame.getHeight(),
                        arcSize.width, arcSize.height);
            testFrame.setShape(rectShape);
            testFrame.setOpacity(0.3f);
// Block 1 End
            testFrame.setVisible(true);
     
      }



}



In this code JPanel is also added, we can continue adding components to this panel for further development of application.

In next blogs I will continue with other components of java swing.