1

I'm having trouble getting an image to show on a JFrame. The frame is completely black upon running. Here's my code:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class JFrameTesting extends JFrame {

    BufferedImage test = null;

    public static void main(String[] args) throws URISyntaxException {
        new JFrameTesting();
    }
    public JFrameTesting() throws URISyntaxException {
        JFrame frame = new JFrame("My first JFrame!");
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
        } catch (IOException ex) {
            Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(test, 200, 200, null);
    }
}

I'm not sure if I'm nessecarily doing anything wrong. I have no errors at all when running.

Thanks in advance!

J-Kode
  • 13
  • 4

3 Answers3

0

You haven't actually added your image to the JFrame yet. To have the image appear you need to add the BufferedImage onto a component then draw that. You can do that using a JLabel and an ImageIcon.

public class JFrameTesting extends JFrame {

    BufferedImage test = null;
    ImageIcon image = new ImageIcon();

    public static void main(String[] args) throws URISyntaxException {
        new JFrameTesting();
    }
    public JFrameTesting() throws URISyntaxException {
        JFrame frame = new JFrame("My first JFrame!");
        try {
           test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
            image.setImage(test);
        } catch (IOException ex) {
            Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
        }
        JLabel label = new JLabel();
        label.setIcon(image);
        frame.add(label);
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Alternatively you can skip the Label and draw onto a component if you want. In which case you you'll have to override the draw method of a JPanel.

JPanel pane = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 200, 200, null);
        }
    }; 
frame.add(pane);

Another note is that you're extending JFrame but also making a new JFrame inside of the class. You can remove the extra JFrame and all the "frame." The class itself is a JFrame so you don't need an extra one.

//set the title using the setTitle method
setTitle("My first JFrame!");

add(label);
setSize(400, 400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Also, I believe the ImageIO.read(...) method can take a URI as a parameter so you shouldn't have to create a File from it.

Brion
  • 746
  • 3
  • 10
  • "You haven't actually added your image to the JFrame yet.": oh yeah?? and the whole drawImage what does it do there ??? this is fluff what you are adding here – gpasch Nov 16 '16 at 22:25
  • The image needs to be added to a component that will go on the JFrame rather than directly onto the JFrame. In my answer I specify that overriding the paintComponent method of a JPanel would work as an alternative. – Brion Nov 18 '16 at 14:28
0

My code draws image, but need repaint. For this you need for example to change size of frame using you mouse.

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class JFrameTesting extends JFrame {

    BufferedImage test = null;

    public static void main(String[] args) throws URISyntaxException {
        new JFrameTesting();
    }
    public JFrameTesting() throws URISyntaxException {
        JFrame frame = new JFrame("My first JFrame!");
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            System.out.println("init");
            test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
            System.out.println(test);
        } catch (IOException ex) {
            Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
        }

        final JPanel pane = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                System.out.println("paint");
                super.paintComponent(g);
                g.drawImage(test, 0, 0, null);
            }
        };
        frame.add(pane);
        frame.repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        System.out.println("paint");
        g.drawImage(test, 200, 200, null);
    }
}
Alykoff Gali
  • 1,121
  • 17
  • 32
0

you can try, with this code. you need to load a JLabel on Jframe when you add a image.

BufferedImage test = null;

public static void main(String[] args) throws URISyntaxException {
    new JFrameTesting();
}
public JFrameTesting() throws URISyntaxException {
    JFrame frame = new JFrame("My first JFrame!");
    JLabel label = new JLabel();
    frame.setSize(800, 800);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try {
        test = ImageIO.read(new File(getClass().getResource("test.png").toURI())); 
        frame.add( new JLabel(new ImageIcon(test)),BorderLayout.CENTER);
        frame.setIconImage(test);
        frame.setVisible(true);
        label.setVisible(true);
    } catch (IOException ex) {
        Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(test, 200, 200, null);
}

}

dunknisai
  • 16
  • 1