Adding JPEG or GIF image files into the GUI in Java using Eclipse IDE

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn how to add JPEG and GIF images into the JFrame.
  • You will learn the usage of the ImageIcon class in Java.
  • You will learn how to use SwingConstants interface in Java.
  • You will learn how to change the positions of heading and JPEG image.

Requirements

  • Any JPEG of GIF image file
  • Java SE Development Kit (Not necessarily the last version)
  • Eclipse IDE or any similar Interated Development Enviroment (IDE) that is designed for Java programming language

Difficulty

This program does not contains too complex Java codes. Therefore, difficulty level is:

  • Intermediate

Tutorial Contents

In this program, we are going to add the following JPEG image to the java frame with heading.
dog.jpg
This JPEG image and its heading positions can be changed in Java with simple command. So, in this program you will see the how to change the positions of heading and JPEG image.

Code.JPG

Frame

First of all, we need to create the JFrame with its name and we should also add the exit and other control buttons to the frame.

        JFrame frame = new JFrame("Dog Gif");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon Class

The usage purpose of the ImageIcon class is to represent an GIF or JPEG image which is included in label. To use this class, we need to know the name of the file of the image, because the constructor of this class takes the name of the file. In the code of this program, we used this class in the below code;

        ImageIcon dog_gif = new ImageIcon("dog.jpg");

With this code,basically we created a new image icon object that contains our JPEG image file dog.jpg.

SwingConstants Interface

A collection of constants generally used for positioning and orienting components on the screen.
Oracle
In order to use this method first of all, we need to import swing package with this following piece of code;

        import javax.swing.*;

Now, we can use SwingConstants interface with CENTER, BOTTOM, TOP ,RIGHT, LEFT or many other similar specific orientation commands to specify to position of label and its text positon.

setHorizontalTextPosition Method

The horizontal position of the text can be change with this command. We can use this command with directly SwingConstant inteface. For instance in order to locate the object to horizontally center, you need to use following command;

        third_label.setHorizontalTextPosition(SwingConstants.CENTER);
setVerticalTextPosition Method

The vertical position of the text can be change with this command. We can use this command with directly SwingConstant inteface. For instance in order to locate the object to vertically bottom, you need to use following command;

        third_label.setVerticalTextPosition(SwingConstants.BOTTOM);
Labels

With the help of the above methods and interface, we can define our first vertically right andhorizontally center label like following;

        first_label= new JLabel("Dog Gif",dog_gif, SwingConstants.CENTER);
        first_label.setHorizontalTextPosition(SwingConstants.RIGHT);
        first_label.setVerticalTextPosition(SwingConstants.CENTER);

The other labels are also constructed in same manner.

Panel

Lastly, we need to create a JPanel and add our labels into this JPanel. We can create it with following code;

        JPanel panel = new JPanel();

We can define its background colour with this code;

        panel.setBackground(Color.cyan);

We can adjust its size with this code;

        panel.setPreferredSize(new Dimension(300,800));

Then, we can add each label into the JPanel with following code;

        panel.add(first_label);
        panel.add(second_label);
        panel.add(third_label);

Lastly, we need to add the panel into the frame and frame must be visible;

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
Results of the Program

When you run this program you will see the following figure.
result.JPG
As it is expected, all of the labels are at the center and the text positions are right center, left bottom and center bottom respectively.

GitHub

You can download the code from following GitHub link.
GitHub
GitHub1.JPG

GitHub2.JPG

Code of the Program
import java.awt.*;
import javax.swing.*;

public class Dog_Gif_Label {

    public static void main(String[] args) 
    {
        JFrame frame = new JFrame("Dog Gif");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        ImageIcon dog_gif = new ImageIcon("dog.jpg");
    
        JLabel first_label, second_label, third_label;
    
        first_label= new JLabel("Dog Gif",dog_gif, SwingConstants.CENTER);
        //first_label.setHorizontalTextPosition(SwingConstants.RIGHT);
        //first_label.setVerticalTextPosition(SwingConstants.CENTER);
    
        second_label= new JLabel("Dog Gif",dog_gif, SwingConstants.CENTER);
        second_label.setHorizontalTextPosition(SwingConstants.LEFT);
        second_label.setVerticalTextPosition(SwingConstants.BOTTOM);
    
        third_label= new JLabel("Dog Gif",dog_gif, SwingConstants.CENTER);
        third_label.setHorizontalTextPosition(SwingConstants.CENTER);
        third_label.setVerticalTextPosition(SwingConstants.BOTTOM);
    
        JPanel panel = new JPanel();
        panel.setBackground(Color.cyan);
        panel.setPreferredSize(new Dimension(300,800));
        panel.add(first_label);
        panel.add(second_label);
        panel.add(third_label);
        
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }

}



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

helpfull for beginners as me

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @aromatheraphy I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x