Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-12-10
9
Medium Priority
?
239 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

885 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