I have made this screenshot program in hopes of eventually making a screen recorder, but I am having trouble displaying the image for the user to see. I currently have it so that it displays and saves. When it displays it is blank, but when it saves there is a screenshot. Why is this?

package screenCapture;

import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

public class CaptureScreen implements ActionListener{

	
	
	public CaptureScreen (){
		JFrame f = new JFrame ("Screen Capture");
		JPanel pane = new JPanel ();
		JButton capture = new JButton ("Capture"); capture.setActionCommand("CaptureScreen"); capture.addActionListener(this);
		pane.add (capture);
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.add (pane);
		f.setVisible(true);
		f.setSize(100,100);
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CaptureScreen cs = new CaptureScreen ();
	}



	@Override
	public void actionPerformed(ActionEvent e){
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("CaptureScreen")){
			Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
			Robot r;
			BufferedImage bI;
			
			try{
				 r= new Robot (); // creates robot not sure exactly how it works
				 Thread.sleep (1000); // waits 1 second before capture
				 bI = r.createScreenCapture(new Rectangle (d)); // tells robot to capture the screen
				 showPic(bI);
				 saveImage(bI);
			} catch (AWTException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InterruptedException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			} 
			
		}
		
		
	}



	private void saveImage(BufferedImage bI) {
		// TODO Auto-generated method stub
		try {
			ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	private void showPic(BufferedImage bI) {
		// TODO Auto-generated method stub
		JFrame f = new JFrame ();
		JScrollPane pane = new JScrollPane ();
		ImageIcon pic = new ImageIcon (bI);
		JLabel l = new JLabel (pic);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pane.add (l);
		f.add (pane);
		f.setVisible (true);
		f.setSize (700,500);
	}

}

nothing changed only created/added Swing rulles

import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

public class CaptureScreen implements ActionListener {

    private JFrame f = new JFrame("Screen Capture");
    private JPanel pane = new JPanel();
    private JButton capture = new JButton("Capture");
    private JDialog d = new JDialog();
    private JScrollPane scrollPane = new JScrollPane();
    private JLabel l = new JLabel();
    private Point location;

    public CaptureScreen() {
        capture.setActionCommand("CaptureScreen");
        capture.setFocusPainted(false);
        capture.addActionListener(this);
        capture.setPreferredSize(new Dimension(300, 50));
        pane.add(capture);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(pane);
        f.setLocation(100, 100);
        f.pack();
        f.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createPicContainer();
            }
        });
    }

    private void createPicContainer() {
        l.setPreferredSize(new Dimension(700, 500));
        scrollPane = new JScrollPane(l,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBackground(Color.white);
        scrollPane.getViewport().setBackground(Color.white);
        d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        d.add(scrollPane);
        d.pack();
        d.setVisible(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("CaptureScreen")) {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
            Robot r;
            BufferedImage bI;
            try {
                r = new Robot(); // creates robot not sure exactly how it works
                Thread.sleep(1000); // waits 1 second before capture
                bI = r.createScreenCapture(new Rectangle(d)); // tells robot to capture the screen
                showPic(bI);
                saveImage(bI);
            } catch (AWTException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
    }

    private void saveImage(BufferedImage bI) {
        try {
            ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showPic(BufferedImage bI) {
        ImageIcon pic = new ImageIcon(bI);
        l.setIcon(pic);
        l.revalidate();
        l.repaint();
        d.setVisible(false);
        location = f.getLocationOnScreen();
        int x = location.x;
        int y = location.y;
        d.setLocation(x, y + f.getHeight());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                d.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                CaptureScreen cs = new CaptureScreen();
            }
        });
    }
}

Try moving the code around and trying different combinations. I did this and that and the other and got it to work.
For example add lbl directly to the frame without the scrollpane and see what happens.

I will try that. As for the other code posted. I did not have a great teacher, so I am learning most of this myself. I have attempted to understand what it does. I have commented the code. Please tell me if it is correct.

package daniwebSwingRules;

import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

public class CaptureScreen implements ActionListener {

    private JFrame f = new JFrame("Screen Capture"); // makes the frame
    private JPanel pane = new JPanel(); // makes the panel
    private JButton capture = new JButton("Capture"); // makes the button
    private JDialog d = new JDialog(); // created dialogue box that holds image
    private JScrollPane scrollPane = new JScrollPane(); // creates scroll panel
    private JLabel l = new JLabel(); // makes the label that holds the picture
    private Point location; // creates a point

    public CaptureScreen() {
        capture.setActionCommand("CaptureScreen");
        capture.setFocusPainted(false);
        capture.addActionListener(this);
        capture.setPreferredSize(new Dimension(300, 50)); // resizes the button. Not actualy needed
        pane.add(capture);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(pane);
        f.setLocation(100, 100); // moves the frame 100 px right and 100 px down
        f.pack();
        f.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createPicContainer(); // created the container that will hold the screenshot
            }
        });
    }

    private void createPicContainer() {
        l.setPreferredSize(new Dimension(700, 500)); // sets label size
        scrollPane = new JScrollPane(l, // adds scrolls to label
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, // sets vertical scroll bar
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); // sets horizontal scroll bar
        scrollPane.setBackground(Color.white); // sets background of scroll pane
        scrollPane.getViewport().setBackground(Color.white);
        d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); // dialogue box close operation
        d.add(scrollPane); // add scroll label to dialogue box
        d.pack(); // sets size
        d.setVisible(false); // doesn't display it
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("CaptureScreen")) {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
            Robot r;
            BufferedImage bI;
            try {
                r = new Robot(); // creates robot not sure exactly how it works
                Thread.sleep(1000); // waits 1 second before capture
                bI = r.createScreenCapture(new Rectangle(d)); // tells robot to capture the screen
                showPic(bI);
                saveImage(bI);
            } catch (AWTException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
    }

    private void saveImage(BufferedImage bI) {
        try {
            ImageIO.write(bI, "JPG", new File("screenShot.jpg")); // writes the file to the specified folder
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showPic(BufferedImage bI) {
        ImageIcon pic = new ImageIcon(bI);
        l.setIcon(pic); // displays the picture
        l.revalidate(); // updates the label
        l.repaint();
        d.setVisible(false);
        location = f.getLocationOnScreen(); // gets frames location
        int x = location.x; //gets the x from the location
        int y = location.y; // gets the y from the location
        d.setLocation(x, y + f.getHeight()); // sets the dialogue to be inline with he x and under the frame for the y
        SwingUtilities.invokeLater(new Runnable() { // once all of the above is done it sets it visible

            @Override
            public void run() {
                d.setVisible(true); // could you not remove the public void run, because it only executes this after the picture has been taken?
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() { // runs this after everything before is done
                CaptureScreen cs = new CaptureScreen(); // creates the frame
            }
        });
    }
}

Thanks for the help

Also the scroll bars are not showing up with the posted code.

Please tell me if it is correct.

Can you tell me if there are problems with it?

The changes I made to your first posted code were only in the showPic() method.

There were no problems with the first code other then not displaying the pictures. I was asking if my commenting on the second code was correct (the post 2 before this one).

Also I changed around the original code a little bit and I replaced

JScrollPane pane = new JScrollPane ();
//with
JScrollPane pane = new JScrollPane (l);

and then I just added pane to the frame and that displayed the image with scroll bars.

With the comments I am just wondering if they are correct, since I have not really used swing rules before.

I'm having a hard time understanding what your are trying to do in this code.
The other code worked very nicely with some small changes to the showPic method.
What it did was:
get an image,
create a frame,
create a component to hold the image,
create a scroll pane with that component,
add that scroll pane to the frame
and make it visible.

What are all the extra things you are trying to do in this last code?

It isn't my code. it is mKorbel's code. I am trying to understand what he did. my current code is this

package screenCapture;

import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

public class CaptureScreen implements ActionListener{

	
	
	public CaptureScreen (){
		JFrame f = new JFrame ("Screen Capture");
		JPanel pane = new JPanel ();
		JButton capture = new JButton ("Capture"); capture.setActionCommand("CaptureScreen"); capture.addActionListener(this);
		pane.add (capture);
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.add (pane);
		f.setVisible(true);
		f.setSize(100,100);
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CaptureScreen cs = new CaptureScreen ();
	}



	@Override
	public void actionPerformed(ActionEvent e){
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("CaptureScreen")){
			Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
			Robot r;
			BufferedImage bI;
			
			try{
				 r= new Robot (); // creates robot not sure exactly how it works
				 Thread.sleep (1000); // waits 1 second before capture
				 bI = r.createScreenCapture(new Rectangle (d)); // tells robot to capture the screen
				 showPic(bI);
				 saveImage(bI);
			} catch (AWTException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InterruptedException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			} 
			
		}
		
		
	}



	private void saveImage(BufferedImage bI) {
		// TODO Auto-generated method stub
		try {
			ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	private void showPic(BufferedImage bI) {
		// TODO Auto-generated method stub
		JFrame f = new JFrame ();
		ImageIcon pic = new ImageIcon (bI);
		JLabel l = new JLabel (pic);
		JScrollPane pane = new JScrollPane (l);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.add (pane);
		f.setVisible (true);
		f.setSize (700,500);
	}

}

it works fine. I just want to understand the changes that mKorbel made to the code, sine I have never used swing rules before.

You need to ask mKorbel about his code.
To get it to work I had to change a few things.
Your last version of showPic is very similar to mine.

@sirlink99
1/ Swing implementations that I posted here, works as I excepted, there are no changes, just added Swing Rules, works correctly on WinXp, Win7, Win2008

2/ don't create lots of JFrames on RunTime more here in English language :-)

3/ all your post as I remember correctly have got problems with Concurency in Swing , then you built house on feet of clay

NormR1 about the JScrollPane, if is possible then always put JScrollPane as 1.st JComponent to the Top Level Container, there are 1T reasons why yes , I don't know any why not

I am attempting to implement a save option into this program, but I am having trouble with the JFileChooser. I don't know how to make the buttons work. I am also wondering if there is a way to change the open text on the button to select. This is the code I have so far.

else if (e.getActionCommand().equals("Browse")){
			JFrame f = new JFrame ();
			f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
			JFileChooser fc = new JFileChooser ();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			f.add (fc);
			f.setResizable (false);
			f.pack();
			f.setVisible(true);
		}

Thanks.

Have you read the API doc for the class?
Which methods have you tried? I see lots that could be useful.

I have added an action listener to the file chooser. What event is generated when they click the button (how will I separate the open and cancel)?

I am looking at the API currently

You don't use events to get the user's input to the file chooser.
The example in the API doc shows one way to get the user's input.

Thanks. I got it working. Another two questions is it possible to capture the mouse somehow using this same method, because now when I take a screenshot the mouse does not appear. Secondly is it possible to convert an array of pictures to a video?

I don't know that the cursor is captured in a screen shot.

I have no ideas about making videos.

myComponent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

sure there are aonther options for (forCursor.getPredefinedCursor(Cursor....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.