[go: up one dir, main page]

0% found this document useful (0 votes)
43 views2 pages

Display Image

The document is a Java program that creates a simple GUI application to display an image using Swing components. It defines a class 'DisplayImage' that initializes the user interface, loads an image from a specified path, and sets up the layout. The main method runs the application on the Event Dispatch Thread, making the window visible.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Display Image

The document is a Java program that creates a simple GUI application to display an image using Swing components. It defines a class 'DisplayImage' that initializes the user interface, loads an image from a specified path, and sets up the layout. The main method runs the application on the Event Dispatch Thread, making the window visible.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.awt.

Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class DisplayImage extends JFrame {

public DisplayImage() {

initUI();
}

private void initUI() {

ImageIcon ii = loadImage();

JLabel label = new JLabel(ii);

createLayout(label);

setTitle("Image");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private ImageIcon loadImage() {

ImageIcon ii = new ImageIcon("src/images/snake.jpg");


return ii;
}

private void createLayout(JComponent... arg) {

Container pane = getContentPane();


GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);

gl.setAutoCreateContainerGaps(true);

gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
);

gl.setVerticalGroup(gl.createParallelGroup()
.addComponent(arg[0])
);

pack();
}

public static void main(String[] args) {

EventQueue.invokeLater(() -> {
DisplayImage ex = new DisplayImage();
ex.setVisible(true);
});
}
}

You might also like