0

I made a project and when it runs,it crops an image and make a screenshot. I want to have the screenshot in one other different JFrame. How can I load the screenshot? Any idea?

I put the code for the two different frames. The first frame (Frame1) makes the screenshot,and on the second frame (Results) I want to pass the screenshot and put it in one JLabel.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2933161
  • 33
  • 1
  • 3
  • 12

2 Answers2

1

Communication between two JFrame running in two different JVM! No, i would never ever give an attention to do what you are trying to achieve at all. This approach is taking memory as much as twice, require immature yet complex way of doing things making the whole process not only slow, but less than making good sense.

The way we should design the process:

  1. Use one JFrame
  2. if required to change content pane with different view: Make use of CardLayout
  3. If required to view more than one component at once in Stack, one above the other make use of JLayeredPane: which is more perfect for your use case
  4. For a continuous and progressive task which require background execution and updating of the GUI on ready portion of task make use of SwingWorker

Now discussing about how to use them to achieve your goal will take a huge space and time cost, hence out of the scope of your question. Please, check out this article one by one:

  1. Using multiple JFrame: good vs bad
  2. How to use Layered Pane
  3. Worker threads and SwingWorker

Edit:

Yes, i do have a solution for you which will take two additional threads:

  1. one to write the new image of screen shots to a file location from the JVM with Frame: use ImageIO.write(). I would name the file with current time and appropriate image extension(jpg, png etc) : currentTimestamp.jpg currentTimeStamp.png

  2. Another thread with the JVM containing Results frame to check if a new image file is available to work with, if so read it using ImageIO.read() and then remove from the file location.

Check out: How to use ImageIO to read and write

Community
  • 1
  • 1
Sage
  • 15,290
  • 3
  • 33
  • 38
  • You have right to all of this and I know. But,my application is too short and I need only for one or two times,something like demo.When and if I need to do something more complex,I use your advices because I know that I made my project with stupid way.But,it was more quickly and easy for me. If you have a solution for my question,it will be helpful for me.However,thank you for your advices . In addition,I am a beginner at Java. – user2933161 Dec 01 '13 at 17:24
  • @user2933161, it is ok that you are a beginner. But why not start learning better things from the beginning. it will not take more than 6-7 hours just to adopt the approach i have described. Check the edit – Sage Dec 01 '13 at 17:41
  • it is possible to use Java for more complex project at the future,so I am learning to programming better than now,I hope so. I knew about your advices,and I will learn it soon.Thank you again. – user2933161 Dec 01 '13 at 17:51
1

Since you are saving the file using these two lines:

File save_path=new File("right.jpg");
ImageIO.write(img, "JPG", save_path);

You only need to read the right.jpg file into the second frame. The below JFrame loads teh image into a JLabel, which is then added to a JPanel.

public class Result extends JFrame {

    public Result(){
        getContentPane().setBackground(Color.white);
        initComponents();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        add(panel);
        JLabel im = new JLabel(new ImageIcon("right.jpg"));
        panel.add(im);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Result tmp = new Result();
                tmp.setSize(600, 600);
                tmp.setVisible(true);
            }
        });
    }
}
Vineet Kosaraju
  • 5,572
  • 2
  • 19
  • 21
  • Thank you about your help,but unfortunately,it doesn't work. It show me the message : Couldn't find file: right.jpg – user2933161 Dec 01 '13 at 17:54