如何从jeditorpane获取BuffereImage?

68de4m5k  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(345)

我需要将一个url加载到一个jeditorpane中,然后从jeditorpane中获取一个bufferedimge,下面的代码将得到一个空白/黑色图像:

  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import javax.imageio.*;
  4. import javax.swing.JEditorPane;
  5. import javax.swing.JFrame;
  6. import javax.swing.text.html.*;
  7. import java.io.*;
  8. public class Html_Browser
  9. {
  10. public static void main(String[] args)
  11. {
  12. Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
  13. JEditorPane editorPane=new JEditorPane();
  14. editorPane.setEditorKit(new HTMLEditorKit());
  15. editorPane.setEditable(false);
  16. try
  17. {
  18. editorPane.setPage("https://news.yahoo.com/");
  19. Thread.sleep(3000);
  20. BufferedImage saveimg=new BufferedImage((int)screenSize.getWidth(),(int)screenSize.getHeight()-36,BufferedImage.TYPE_INT_RGB);
  21. Graphics2D g2=saveimg.createGraphics();
  22. editorPane.paint(g2);
  23. ImageIO.write(saveimg,"png",new File("test.png"));
  24. }
  25. catch (Exception e)
  26. {
  27. editorPane.setContentType("text/html");
  28. editorPane.setText("<html>Connection issues!</html>");
  29. }
  30. JFrame frame=new JFrame();
  31. frame.getContentPane().add(editorPane);
  32. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. frame.setBounds(0,0,(int)screenSize.getWidth(),(int)screenSize.getHeight()-36);
  34. frame.setLocationRelativeTo(null);
  35. frame.setVisible(true);
  36. }
  37. }

我加了3秒的延迟,但没用,怎么办?

axzmvihb

axzmvihb1#

你也许可以使用这种方法。
它修改编辑器窗格以同步读取所有文件。然后是 PropertyChanngeEvent 在i/o完成时生成。

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.beans.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import javax.swing.*;
  7. import javax.swing.text.*;
  8. import javax.swing.text.html.*;
  9. import java.awt.image.*;
  10. public class EditorPaneLoadSynchronously extends JFrame
  11. implements ActionListener, PropertyChangeListener
  12. {
  13. private JEditorPane html;
  14. private JTextField webURL;
  15. public EditorPaneLoadSynchronously()
  16. {
  17. JPanel urlPanel = new JPanel();
  18. getContentPane().add(urlPanel, BorderLayout.NORTH);
  19. webURL = new JTextField("https://stackoverflow.com", 15);
  20. webURL.addActionListener(this);
  21. urlPanel.add(webURL);
  22. JButton gotoURL = new JButton("Goto URL");
  23. gotoURL.addActionListener(this);
  24. urlPanel.add(gotoURL);
  25. HTMLEditorKit editorKit = new HTMLEditorKit()
  26. {
  27. private final ViewFactory factory = new HTMLFactory()
  28. {
  29. public View create(Element elem)
  30. {
  31. View v = super.create(elem);
  32. if ((v != null) && (v instanceof ImageView))
  33. {
  34. ((ImageView)v).setLoadsSynchronously( true );
  35. }
  36. return v;
  37. }
  38. };
  39. public ViewFactory getViewFactory()
  40. {
  41. return factory;
  42. }
  43. };
  44. html = new JEditorPane();
  45. // html.setEditorKit( editorKit );
  46. // html.setEditable( false );
  47. html.addPropertyChangeListener("page", this);
  48. JScrollPane scrollPane = new JScrollPane(html);
  49. scrollPane.setPreferredSize( new Dimension(400, 400) );
  50. getContentPane().add(scrollPane);
  51. }
  52. public void actionPerformed(ActionEvent e)
  53. {
  54. try
  55. {
  56. // html.setDocument( new HTMLDocument() );
  57. html.setPage( new URL(webURL.getText()) );
  58. System.out.println("After setPage");
  59. }
  60. catch(Exception exc)
  61. {
  62. System.out.println(exc);
  63. }
  64. }
  65. public void propertyChange(PropertyChangeEvent e)
  66. {
  67. try
  68. {
  69. System.out.println("Page Loaded");
  70. // BufferedImage bi = ScreenImage.createImage(html);
  71. // ScreenImage.writeImage(bi, "sync.jpg");
  72. }
  73. catch(Exception ee) {}
  74. }
  75. private static void createAndShowGUI()
  76. {
  77. EditorPaneLoadSynchronously frame = new EditorPaneLoadSynchronously();
  78. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79. frame.pack();
  80. frame.setLocationByPlatform( true );
  81. frame.setVisible( true );
  82. frame.actionPerformed(null);
  83. }
  84. public static void main(String[] args) throws Exception
  85. {
  86. java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
  87. }
  88. }

注意:上面的代码使用屏幕图像便利类。您可以将其替换为创建和编写BuffereImage的唯一代码。

展开查看全部
vs91vp4v

vs91vp4v2#

您试图在呈现gui(包括组件)之前绘制一个组件,但这行不通。建议包括:
使用在swing事件线程上加载gui SwingUtilities.invokeLater() 使用swingworker在后台线程中加载任何web数据。或者在创建swing gui之前在主线程中获取web页面,然后将其传递到swing gui。
仅在加载网页并呈现gui(使其可见)后创建图像
除掉所有的 Thread.sleep 电话。这些不是您的朋友,只会阻碍swing gui的呈现。
例如:

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.net.URL;
  8. import javax.imageio.ImageIO;
  9. import javax.swing.*;
  10. public class EditorStuff extends JPanel {
  11. private static final String URL_PATH = "https://docs.oracle.com/javase/tutorial/index.html";
  12. private JEditorPane editorPane;
  13. public EditorStuff(URL url) throws IOException {
  14. int w = 800;
  15. int h = 650;
  16. setPreferredSize(new Dimension(w, h));
  17. editorPane = new JEditorPane(url);
  18. // editorPane.setPage(url);
  19. JScrollPane scrollPane = new JScrollPane(editorPane);
  20. setLayout(new BorderLayout());
  21. add(scrollPane);
  22. }
  23. public void captureImage() throws IOException {
  24. int w = editorPane.getWidth();
  25. int h = editorPane.getHeight();
  26. BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  27. Graphics2D g2 = img.createGraphics();
  28. editorPane.paint(g2);
  29. ImageIO.write(img, "png", new File("test.png"));
  30. g2.dispose();
  31. }
  32. public static void main(String[] args) {
  33. try {
  34. URL url = new URL(URL_PATH);
  35. SwingUtilities.invokeLater(() -> createAndShowGui(url));
  36. } catch (IOException ioEx) {
  37. ioEx.printStackTrace();
  38. }
  39. }
  40. private static void createAndShowGui(final URL url) {
  41. try {
  42. EditorStuff mainPanel = new EditorStuff(url);
  43. JFrame frame = new JFrame("EditorStuff");
  44. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  45. frame.add(mainPanel);
  46. frame.pack();
  47. frame.setLocationByPlatform(true);
  48. frame.setVisible(true);
  49. mainPanel.captureImage(); // called *after* rendering
  50. } catch (IOException ioEx) {
  51. ioEx.printStackTrace();
  52. }
  53. }
  54. }
展开查看全部

相关问题