0

Fist of all, I knew that this question has been asked before (JFrame to image without showing the JFrame). However, due to the poor coding of mine, I would like to ask help from others about how to remove the JFrame out from my coding as shown below:

package com.example.ImageScreen;

import java.awt.Color;
import java.awt.Dimension;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  

import javax.imageio.ImageIO;  
import javax.swing.*;  

//public class ImageScreen extends JPanel{  
public class ImageScreen extends JPanel{  
    private static final long serialVersionUID = 1L;  
    private BufferedImage image;  

    public ImageScreen() {  
        setSize(600,600);
   //     setMinimumSize(new Dimension(250,250));  

        try {  
                                                //Load the image  
            image = ImageIO.read(new File("C:/Users/User/Downloads/Geoffs_Picture_Overlay_App/crosshair.gif"));  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  

    @Override  
    public void paintComponent(Graphics g) {  
        super.paintComponent(g);  
        Graphics2D g2d = (Graphics2D) g.create();  
                                //Paint it on screen  
        g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);  
        g2d.dispose();  
    }  

    public static void main(String[] args) {  
        JFrame window = new JFrame("An Image On Screen");  
                                window.add(new ImageScreen());  
                                window.setLocationRelativeTo(null);  
                                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                                window.pack();  
                                window.setVisible(true);  
                           //     window.setAlwaysOnTop(true);
                                window.setBackground(Color.CYAN);
                                window.setUndecorated(true);
                            //    window.dispose();

    }  

}  
Community
  • 1
  • 1
xDevilx
  • 3
  • 6

2 Answers2

0

Use - window.setVisible(false)

URL87
  • 10,667
  • 35
  • 107
  • 174
0
  1. Use a JLabel to display an image. There is no need to do custom painting when you are painting the image at its actual size.

  2. The setUndecorated(...) method should be invoked before you pack() and make the frame visible.

camickr
  • 321,443
  • 19
  • 166
  • 288