This question already has an answer here:
Swing: Obtain Image of JFrame
(1 answer)
Closed 7 years ago.
Heys guys, I've developped a code to take a screen shot of my whole screen, but I want it to take the screen shot of only the things inside my Jframe. Ill be using it to print the image later on by the way. And one of the main problem is, the mouse also comes inside the snapshot. I don't want the mouse or the two buttons at the bottom. I can just change visi of buttons but what should be done for mouse and inside Jframe only shot? Here is my code it takes screen shot of whole screen.
try{
Thread.sleep(1000);
Toolkit tk = Toolkit.getDefaultToolkit(); //Toolkit class returns the default toolkit
Dimension d = tk.getScreenSize();
//Dimension class object stores width & height of the toolkit screen
// toolkit.getScreenSize() determines the size of the screen
Rectangle rec = new Rectangle(0, 0, d.width, d.height);
//Creates a Rectangle with screen dimensions,
Robot ro = new Robot(); //to capture the screen image
BufferedImage img = ro.createScreenCapture(rec);
File f;
f = new File("myimage.jpg"); // File class is used to write the above generated buffered image to a file
ImageIO.write(img, "jpg", f);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
IMHO it is better to make an image of your component (a JFrame is also a Component):
BufferedImage img = new BufferedImage(yourComponent.getWidth(), yourComponent.getHeight(), BufferedImage.TYPE_INT_RGB);
yourComponent.paint(img.getGraphics());
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
Related
I want to give a visual indication that a node has been transferred to clipboard with a "Cut" action. One intuitive look used by at least one proprietary OS is to make this the same image, but slightly transparent.
I'd quite like to know whether it is in fact possible somehow to use the icons used by the Windoze OS (W7)... but I'd be more intrigued if it were possible to interfere in some way (in the renderer) with the Icon, by somehow messing with the Graphics object used by Icon.paintIcon() ... just for a given node, obviously. I'm not clear where an Icon goes hunting for the Graphics object it uses when it is painted ... any enlightenment would be most welcome.
later
Many thanks to MadProgrammer. Spotted this possibility as a way of extracting obfuscated visuals with a view to their manipulation: https://home.java.net/node/674913 ... it works. Putting code here in case of broken link...
public class IconTest {
public static void main(String[] args) {
Icon leafIcon = UIManager.getIcon("Tree.leafIcon");
// ... ("Tree.closedIcon") ("Tree.openIcon")
BufferedImage img1 = new BufferedImage(leafIcon.getIconWidth(),
leafIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = img1.createGraphics();
g.drawImage(((ImageIcon) leafIcon).getImage(), 0, 0, null);
g.dispose();
try {
ImageIO.write(img1, "PNG", new File("leafIcon.png"));
} catch (IOException e) {
System.out.println("Error writing to file leafIcon" + ", e = " + e);
System.exit(0);
}
}
}
Then use MadProgrammer's technique to alter the image in any way one likes: change transparency, colour, etc. Great stuff.
I'd quite like to know whether it is in fact possible somehow to use the icons used by the Windoze OS (W7)
FileSystemView#getSystemIcon will give you the OS's icon representation of a given File, for example...
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("ThatImportantDoc.docx"));
I want to give a visual indication that a node has been transferred to clipboard with a "Cut" action. One intuitive look used by at least one proprietary OS is to make this the same image, but slightly transparent.
You need to paint the previous Icon to BufferedImage, which has had a AlphaComposite applied to it, for example
BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
You then need to wrap the resulting BufferedImage in a ImageIcon, which allows you to pass the image as a Icon to the rest of the API.
JPanel panel = new JPanel();
panel.add(new JLabel(icon));
panel.add(new JLabel(new ImageIcon(img)));
JOptionPane.showMessageDialog(null, panel, "Icon", JOptionPane.PLAIN_MESSAGE);
To get this to finally work, you will need to provide a TreeCellRenderer capable of supporting your functionality. Have a look at How to Use Trees for more details
Just one tweak enabling me to do what I mainly wanted to do: get the UI images "from the source code".
public class IconTest {
public static void main(String[] args) {
// OS folder icon
// Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("."));
// proprietary word processor
// Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("Doc1.docx"));
// taken from PNG file
// Icon icon = new ImageIcon( "openIcon.png" );
// taken directly from the Java images held somewhere (?) in the code
Icon icon = UIManager.getIcon("Tree.leafIcon");
// Icon icon = UIManager.getIcon("Tree.openIcon");
// ... ("Tree.closedIcon") ("Tree.openIcon")
BufferedImage img = new BufferedImage( icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setComposite(AlphaComposite.SrcOver.derive( 0.5f));
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
JPanel panel = new JPanel();
panel.add(new JLabel(icon));
panel.add(new JLabel(new ImageIcon(img)));
JOptionPane.showMessageDialog(null, panel, "Icon", JOptionPane.PLAIN_MESSAGE);
}
}
I am writing a code to make an image file of a chart appearing on a panel. For that purpose I create the buffered image of that and then use ImageIO.write(). It works but it only displays the panel(grey coloured panel) but does not show the chart present on that panel. What to do in this case?? Here is my code
com.objectplanet.chart.NonFlickerPanel p =
new com.objectplanet.chart.NonFlickerPanel(new BorderLayout());
p.add("Center", chart); // this statements adds the chart in the center of the panel
ChartPanel.add("Center", p);
ChartPanel.setSize(500, 200);
ChartPanel.show();
int w = ChartPanel.getWidth();
int h = ChartPanel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
ChartPanel.paint(g);
ChartPanel.printAll(g);
File f = new File("D:\\image.png");
try {
// png is an image format (like gif or jpg)
ImageIO.write(bi, "png", f);
} catch (IOException ex) {
ex.printStackTrace();
}
Well i solved the problem .Anyone facing the same problem ,here is the solution
Use paintall function rather than just paint function
I am taking a screenshot of the current screen then saving the image. I want to open that image up and be able to select a box of a certain element or whatever it is i want the pic to be of and to be able to in turn save that smaller selected image to
a file. Please help.
RemoteControlConfiguration config = new RemoteControlConfiguration();
config.setPort(4447);
SeleniumServer server = new SeleniumServer(config);
try{
// TODO Auto-generated method stub
server.start();
DefaultSelenium selenium = new DefaultSelenium("localhost", 4447, "*firefox", "http://www.google.com/");
selenium.start();
selenium.open("http://www.google.com/");
selenium.waitForPageToLoad("10000");
selenium.windowMaximize();
BufferedImage image1 = Screenshot("screen1.jpg");
//selenium.type("q", "Hello world");
Thread.sleep(2000);
BufferedImage image2 = Screenshot("screen2.jpg");
public static BufferedImage Screenshot(String fileName) throws Exception
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
File file = new File(fileName);
ImageIO.write(image, "jpg", file);
return image;
}
Assuming you know the coordinates of your new bounds, create a new BufferedImage with the new size, create a graphics object for your new image, and paint the big image on this graphics object, specifying negative values for the x,y. The source image is bigger than the destination, so only the bits that fit within the destination will be written. Then you save out the smaller one using ImageIO.write()
EDIT
Thanks to Andrew Thompson for the suggestion to use subImage
BufferedImage image1 = Screenshot("screen1.jpg");
BufferedImage subImage = image1.getSubImage(x, y, width, height);
i want to capture the image of any control i.e textbox, image frame from the GUI form. Kindly notice i need the screen which only contain only one GUI item
You have to get the position and dimension of the Component, so you can create a rectangle and pass it to the Robot screenshot taker :
//Design the rectangle
Component c = somecomponent;
Rectangle rect = new Rectangle(c.getLocation(), c.getSize());
//Get the ScreenShot
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rect);
ImageIO.write(image, "png", new File(fileName));
I wanted to display on the JFrame on the program the full screenshot of my screen.
So far using the code below, I was able only to display part of the screen.
The code below is the content of the paint(Graphics g).
How can I make it full screen?
// the screen resolution is 1280 x 1024 while the JPanel size is only 1024 x 768
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
Maybe using something like this:
//get the screen size
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image = robot.createScreenCapture (dim);
//other code
//...
I see you have some errors, I don't know if your code even compiles, 'cause references seems not to be declared, but a code similar to this one will caputure a screenshoot of your desktop:
import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class ScreenCapturer
{
public static void main(String[] args)throws Exception
{
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
Robot robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
Graphics g = bufferedImage.getGraphics();
//g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
File out = new File("image.png");
ImageIO.write(bufferedImage,"png",out);
}
}
I saved to an png image file instead of drawing it on the screen or the frame.
Use java.awt.Toolkit.getDefaultToolkit().getScreenSize() to get the size of the screen: http://www.roseindia.net/java/java-get-example/screen-dimensions.shtml
g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
Simplify your code by only using one statement per line then you might be able to understand the code.
Why are you ue the width and height of the image? How does that scale the image if you specify the full size of the image? I would guess you want:
Image scaled = bufferedImage.getScaledInstance(1024, 768, Image.SCALE_DEFAULT);
Now instead of doing custom painting you can just add your image to a JLabel:
ImageIcon icon = new ImageIcon( scaled );
JLabel label = new JLabel( icon );
frame.add( label );