0

作成したパネルのスクリーンショットを作成したいのですが、コードを以下に示します。体が私が得られない理由を教えてもらえますか?ありがとう

    public static final void makeScreenshot(JFrame argFrame) 
    {
      Rectangle rec = argFrame.getBounds();
      BufferedImage bufferedImage = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
      argFrame.paint(bufferedImage.getGraphics());

    try 
   {
        // Create temp file.
        File temp = File.createTempFile("C:\\Documents and Settings\\SriHari\\My Documents\\NetBeansProjects\\test\\src\\testscreenshot", ".jpg");

        // Use the ImageIO API to write the bufferedImage to a temporary file
        ImageIO.write(bufferedImage, "jpg", temp);

        //temp.deleteOnExit();
    } 


   catch (IOException ioe) {} 
  } // 
        public static void main(String args[])
        {
           TimeTableGraphicsRunner ts= new TimeTableGraphicsRunner();
           for(long i=1;i<1000000000;i++);
           ts.makeScreenshot(jf);
            System.out.println("hi");
        }
4

2 に答える 2

1

以下は私のために働きます:

public static void main (String [] args)
{
    final JFrame frame = new JFrame ();

    JButton button = new JButton (new AbstractAction ("Make Screenshot!")
    {
        @Override
        public void actionPerformed (ActionEvent e)
        {
            Dimension size = frame.getSize ();
            BufferedImage img = new BufferedImage (size.width, size.height, BufferedImage.TYPE_3BYTE_BGR);
            Graphics g = img.getGraphics ();
            frame.paint (g);
            g.dispose ();
            try
            {
                ImageIO.write (img, "png", new File ("screenshot.png"));
            }
            catch (IOException ex)
            {
                ex.printStackTrace ();
            }
        }
    });

    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane ().setLayout (new BorderLayout ());
    frame.getContentPane ().add (button, BorderLayout.CENTER);
    frame.pack ();
    frame.setVisible (true);
}

これはSwingではなくOSによって処理されるため、ウィンドウのタイトルと境界線はレンダリングされません。

于 2013-02-09T08:41:55.173 に答える
1

あなたは次のようなものを使ってみることができますRobot#createScreenCapture

ここに画像の説明を入力してください

画面全体をキャプチャしたくない場合はComponent#getLocationOnScreen、代わりに画面上のコンポーネントの位置を見つけるために使用できます...

public class CaptureScreen {

    public static void main(String[] args) {
        new CaptureScreen();
    }

    public CaptureScreen() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;

                frame.add(new JLabel("Smile :D"), gbc);

                JButton capture = new JButton("Click");
                capture.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            Robot robot = new Robot();
                            Rectangle bounds = frame.getBounds();
                            bounds.x -= 1;
                            bounds.y -= 1;
                            bounds.width += 2;
                            bounds.height += 2;
                            BufferedImage snapShot = robot.createScreenCapture(bounds);
                            ImageIO.write(snapShot, "png", new File("Snapshot.png"));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });

                frame.add(capture, gbc);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

}

使用例Component#getLocationOnScreen

ここに画像の説明を入力してください

public class CaptureScreen {

    public static void main(String[] args) {
        new CaptureScreen();
    }

    public CaptureScreen() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;

                frame.add(new JLabel("Smile :D"), gbc);

                JButton capture = new JButton("Click");
                capture.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            Robot robot = new Robot();
                            Container panel = frame.getContentPane();
                            Point pos = panel.getLocationOnScreen();
                            Rectangle bounds = panel.getBounds();
                            bounds.x = pos.x;
                            bounds.y = pos.y;
                            bounds.x -= 1;
                            bounds.y -= 1;
                            bounds.width += 2;
                            bounds.height += 2;
                            BufferedImage snapShot = robot.createScreenCapture(bounds);
                            ImageIO.write(snapShot, "png", new File("Snapshot.png"));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });

                frame.add(capture, gbc);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

}

別の例を表示Component#getLocationOnScreen

ここに画像の説明を入力してくださいここに画像の説明を入力してくださいここに画像の説明を入力してくださいここに画像の説明を入力してくださいここに画像の説明を入力してくださいここに画像の説明を入力してください

画像の主な理由3が同じであるのは、例がルートペイン、レイヤードペイン、およびコンテンツペインをダンプするためです...

public class CaptureScreen {

    public static void main(String[] args) {
        new CaptureScreen();
    }

    public void save(Component comp, File file) {
        if (comp.isVisible()) {
            try {
                System.out.println(comp);
                Robot robot = new Robot();
                Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
                bounds.x -= 1;
                bounds.y -= 1;
                bounds.width += 2;
                bounds.height += 2;
                BufferedImage snapShot = robot.createScreenCapture(bounds);
                ImageIO.write(snapShot, "png", file);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private int layer;

    public void capture(Container container) {
        layer = 1;
        captureLayers(container);
    }

    public void captureLayers(Container container) {
        save(container, new File("SnapShot-" + layer + "-0.png"));
        int thisLayer = layer;
        int count = 1;
        for (Component comp : container.getComponents()) {
            if (comp instanceof Container) {
                layer++;
                captureLayers((Container) comp);
            } else {
                save(comp, new File("SnapShot-" + thisLayer + "-" + count + ".png"));
                count++;
            }
        }
    }

    public CaptureScreen() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;

                frame.add(new JLabel("Smile :D"), gbc);

                JButton capture = new JButton("Click");
                capture.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        capture(frame);
                    }

                });

                frame.add(capture, gbc);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

}
于 2013-02-09T10:00:00.107 に答える