Avatar of dyarosh
dyarosh
 asked on

New to Java - Need to display an image in a window using JAVA

I am new to JAVA and have a project that manipulates images.  I am able to save the new image to a file but want to display it in a window on the screen.  Here is the code I am using.  The window opens but the image doesn't display.  Resultsimage is a BuffereImage that contains the new image.

                           Graphics g = resultsimage.getGraphics();
                           JFrame window = new JFrame("Image");
                           window.setBounds(0, 0, resultsimage.getWidth()+10, resultsimage.getHeight()+10);
                           window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                           g.drawImage(resultsimage, 5, 5, resultsimage.getWidth(), resultsimage.getHeight(), Color.WHITE, null);
                           window.setVisible(true);

Open in new window


Any help is greatly appreciated.
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ashok

try adding

protected void paintComponent(Graphics g)  {
        super.paintComponent(g);  
        g.drawImage(resultsimage, 5, 5, resultsimage.getWidth(), resultsimage.getHeight(), Color.WHITE, null);
}

then make a call to above passing g

paintComponent(g);
Ashok

import java.awt.Graphics2D;

.....

Graphics2D g2d = (Graphics2D) g.create();  
//Paint it on screen  
g2d.drawImage(resultsimage, 5, 5, resultsimage.getWidth(), resultsimage.getHeight(), Color.WHITE, null);
g2d.dispose();
Ashok

If you understand little Java, here is lot of good stuff with graphic.

http://www.ntu.edu.sg/home/ehchua/programming/java/J8b_Game_2DGraphics.html

I do not have Java code in front of me right now so I am not able to give you exact code.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ashok

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class TestPaintImage {

    public static void main(String[] args) {
        new TestPaintImage();
    }

    public TestPaintImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ImagePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage background;

        public ImagePane() {
            try {
                background = ImageIO.read(new File("/path/to/your/image"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g.drawImage(background, x, y, this);
            }
        }
    }
}

Test above first.  background is same as your resultsimage.
Ashok

Here is another helpful code.....

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test extends JPanel {
public test() {
setPreferredSize(new Dimension(400,300));
}

public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.GREEN);
g.drawLine(0,0,getWidth(),getHeight());
g.drawLine(getWidth(),0,0,getHeight());
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final test t = new test();
f.add(t,BorderLayout.CENTER);
JButton b = new JButton("Save Image");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
BufferedImage bi = new BufferedImage(
t.getWidth(),t.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
t.paintComponent(g);
try {
ImageIO.write(bi,"JPEG",
new File("image.jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
f.add(b,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
});
}
}
Ashok

JPanel panel = new JPanel(){
        @Override
        public void paintComponent(Graphics g) {
            BufferedImage image = null; // get your buffered image.
            Graphics2D graphics2d = (Graphics2D) g;
            graphics2d.drawImage(image, 0, 0, null);
            super.paintComponents(g);
        }
    };
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dyarosh

ASKER
THank you.  That worked for me.
CEHJ

:)