Solved

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

Posted on 2013-12-10
9
235 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 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
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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 77
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 76
Chrome and Firefox Java 5 67
running on tomcat not jboss eap 7.0 3 30
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

749 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