Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-10
9
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

670 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