I hope some one can help me, this is what I want to do.
I have a JTextPane and I want to take a screenshot to that specific JTextPane coordinates and size, so far I can do a screenshot with the size of the JTextPane but I can't get the specific coordinates my screenshots always gets the (0,0) coordinates.
This is my method:
void capturaPantalla ()
{
try
{
int x = txtCodigo.getX();
int y = txtCodigo.getY();
Rectangle areaCaptura = new Rectangle(x, y, txtCodigo.getWidth(), txtCodigo.getHeight());
BufferedImage capturaPantalla = new Robot().createScreenCapture(areaCaptura);
File ruta = new File("P:\\captura.png");
ImageIO.write(capturaPantalla, "png", ruta);
JOptionPane.showMessageDialog(null, "Codigo de barras guardado!");
}
catch (IOException ioe)
{
System.out.println(ioe);
}
catch(AWTException ex)
{
System.out.println(ex);
}
}
When you call
getX()
andgetY()
on any Swing component, you get the x and y relative to the component's container, not the screen. Instead you want the location of the component relative to the screen and get position based on that viagetLocationOnScreen()
As per MadProgrammer's comment, you could simply call
printAll(Graphics g)
on your txtCodigo component, passing in a Graphics object obtained from a properly sized BufferedImage and forgo use of a Robot.To compare to the two methods:
If you print the text component with printAll, you get the entire text component, even the parts that are not displayed in the JScrollPane's viewport.