用Java制作屏幕截图的JTextPane的坐标
我希望有人能帮助我,这就是我想做的 我有一个JTextPane,我想截图到具体的JTextPane坐标和大小,到目前为止,我可以截图到JTextPane的大小,但我不能得到具体的坐标,我的截图总是得到(0,0)坐标 这是我的方法:用Java制作屏幕截图的JTextPane的坐标,java,swing,coordinates,screenshot,jtextpane,Java,Swing,Coordinates,Screenshot,Jtextpane,我希望有人能帮助我,这就是我想做的 我有一个JTextPane,我想截图到具体的JTextPane坐标和大小,到目前为止,我可以截图到JTextPane的大小,但我不能得到具体的坐标,我的截图总是得到(0,0)坐标 这是我的方法: void capturaPantalla () { try { int x = txtCodigo.getX(); int y = txtCodigo.getY(); Rectangle areaCa
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);
}
}
当您在任何Swing组件上调用getX()
和getY()
时,您将得到相对于组件容器的x和y,而不是相对于屏幕的x和y。相反,您需要组件相对于屏幕的位置,并通过getLocationOnScreen()
根据MadProgrammer的评论,您可以简单地在txtCodigo组件上调用printAll(Graphics g)
,传入从大小合适的BuffereImage获得的图形对象,并放弃使用机器人
Dimension d = txtCodigo.getSize();
BufferedImage img = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
txtCodigo.printAll(g);
g.dispose();
// use ImageIO to write BufferedImage to file
要比较这两种方法:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class RobotVsPrintAll extends JPanel {
private static final int WORDS = 400;
private JTextArea textArea = new JTextArea(20, 40);
private JScrollPane scrollPane = new JScrollPane(textArea);
private Random random = new Random();
public RobotVsPrintAll() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < WORDS; i++) {
int wordLength = random.nextInt(4) + 4;
for (int j = 0; j < wordLength; j++) {
char myChar = (char) (random.nextInt('z' - 'a' + 1) + 'a');
sb.append(myChar);
}
sb.append(" ");
}
textArea.setText(sb.toString());
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JButton robot1Btn = new JButton(new Robot1Action("Robot 1"));
JButton robot2Btn = new JButton(new Robot2Action("Robot 2"));
JButton printAllBtn = new JButton(new PrintAllAction("Print All"));
JPanel btnPanel = new JPanel();
btnPanel.add(robot1Btn);
btnPanel.add(robot2Btn);
btnPanel.add(printAllBtn);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}
private void displayImg(BufferedImage img) {
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(this, icon, "Display Image",
JOptionPane.PLAIN_MESSAGE);
}
private class Robot1Action extends AbstractAction {
public Robot1Action(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
Component comp = scrollPane.getViewport();
Point p = comp.getLocationOnScreen();
Dimension d = comp.getSize();
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(p.x, p.y, d.width, d.height);
BufferedImage img = robot.createScreenCapture(screenRect);
displayImg(img);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
private class Robot2Action extends AbstractAction {
public Robot2Action(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
Component comp = textArea;
Point p = comp.getLocationOnScreen();
Dimension d = comp.getSize();
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(p.x, p.y, d.width, d.height);
BufferedImage img = robot.createScreenCapture(screenRect);
displayImg(img);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
private class PrintAllAction extends AbstractAction {
public PrintAllAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e) {
Dimension d = textArea.getSize();
BufferedImage img = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
textArea.printAll(g);
g.dispose();
displayImg(img);
}
}
private static void createAndShowGui() {
RobotVsPrintAll mainPanel = new RobotVsPrintAll();
JFrame frame = new JFrame("Robot Vs PrintAll");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
import java.awt.AWTException;
导入java.awt.BorderLayout;
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Point;
导入java.awt.Rectangle;
导入java.awt.Robot;
导入java.awt.event.ActionEvent;
导入java.awt.image.buffereImage;
导入java.util.Random;
导入javax.swing.*;
@抑制警告(“串行”)
公共类RobotVsPrintAll扩展了JPanel{
专用静态最终整数字=400;
私有JTextArea textArea=新JTextArea(20,40);
私有JScrollPane scrollPane=新JScrollPane(textArea);
私有随机=新随机();
公共机器人VSPRINTALL(){
StringBuilder sb=新的StringBuilder();
for(int i=0;i
如果使用printAll打印文本组件,则会得到整个文本组件,甚至是JScrollPane视口中未显示的部分。您可以使用完成所有工作的类。您只需指定要捕获的组件
守则是:
BufferedImage bi = ScreenImage.createImage( component );
您可以使用以下方法将图像保存到文件:
ScreenImage.writeImage(bi, "imageName.jpg");
这门课将使用Swing组件的绘画方法,这比使用机器人更有效。就要提交我的答案了,你赢了我@克里斯托弗史密斯:对不起!也可以使用printAll
ScreenImage.writeImage(bi, "imageName.jpg");