Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ImagePanel("yourImage.JPG"));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class ImagePanel extends JPanel {

    BufferedImage img;

    ImagePanel(String name) {
        super(true);
        try {
            img = ImageIO.read(new File(name));
            this.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    }
}