Solved

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

Posted on 2013-12-10
9
231 Views
Last Modified: 2013-12-10
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.
0
Comment
Question by:dyarosh
  • 6
  • 2
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39709074
                           JFrame window = new JFrame("Image");
                           window.setBounds(0, 0, resultsimage.getWidth()+10, resultsimage.getHeight()+10);
                           window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                           window.getContentPane().add(new JLabel(new ImageIcon(resultsimage)));
                           window.setVisible(true);
                                  

Open in new window

Try the above instead
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39709079
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);
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39709098
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();
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39709130
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 13

Expert Comment

by:Ashok
ID: 39709274
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.
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39709330
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);
}
});
}
}
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39709346
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);
        }
    };
0
 

Author Closing Comment

by:dyarosh
ID: 39709413
THank you.  That worked for me.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39709603
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now