栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Swing:获取JFrame的图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swing:获取JFrame的图像

ComponentImageCapture.java

import java.awt.BorderLayout;import java.awt.Component;import java.awt.Image;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.KeyEvent;import java.awt.event.InputEvent;import javax.swing.*;import javax.swing.border.TitledBorder;import javax.imageio.ImageIO;import java.io.File;class ComponentImageCapture {  static final String HELP =    "Type Ctrl-0 to get a screenshot of the current GUI.n" +    "The screenshot will be saved to the current " +    "directory as 'screenshot.png'.";  public static BufferedImage getScreenShot(    Component component) {    BufferedImage image = new BufferedImage(      component.getWidth(),      component.getHeight(),      BufferedImage.TYPE_INT_RGB      );    // call the Component's paint method, using    // the Graphics object of the image.    component.paint( image.getGraphics() ); // alternately use .printAll(..)    return image;  }  public static void main(String[] args) {    Runnable r = new Runnable() {      public void run() {        final Jframe f = new Jframe("Test Screenshot");        JMenuItem screenshot =          new JMenuItem("Screenshot");        screenshot.setAccelerator(          KeyStroke.getKeyStroke( KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK          ));        screenshot.addActionListener(          new ActionListener(){ public void actionPerformed(ActionEvent ae) {   BufferedImage img = getScreenShot(     f.getContentPane() );   JOptionPane.showMessageDialog(     null,     new JLabel(       new ImageIcon(         img.getScaledInstance(img.getWidth(null)/2,img.getHeight(null)/2,Image.SCALE_SMOOTH )         )));   try {     // write the image as a PNG     ImageIO.write(       img,       "png",       new File("screenshot.png"));   } catch(Exception e) {     e.printStackTrace();   } }          } );        JMenu menu = new JMenu("Other");        menu.add(screenshot);        JMenuBar mb = new JMenuBar();        mb.add(menu);        f.setJMenuBar(mb);        JPanel p = new JPanel( new BorderLayout(5,5) );        p.setBorder( new TitledBorder("Main GUI") );        p.add( new JScrollPane(new JTree()),          BorderLayout.WEST );        p.add( new JScrollPane( new Jtextarea(HELP,10,30) ),          BorderLayout.CENTER );        f.setContentPane( p );        f.pack();        f.setLocationRelativeTo(null);        f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        f.setVisible(true);      }    };    SwingUtilities.invokeLater(r);  }} 

另见

上面显示的代码假定组件在渲染之前已在屏幕上实现。

Rob Camick展示了如何在Screen Image类中绘制未实现的组件。

另一个可能与此相关的主题是Render JLabel,没有第一个显示,特别是Darryl Burke的“一个行修复”。

LabelRenderTest.java

这是第二个链接上显示的代码的更新后的变体。

import java.awt.*;import java.awt.image.BufferedImage;import javax.swing.*;public class LabelRenderTest {    public static void main(String[] args) {        SwingUtilities.invokeLater( new Runnable() { public void run() { String title = "<html><body style='width: 200px; padding: 5px;'>"     + "<h1>Do U C Me?</h1>"     + "Here is a long string that will wrap.  "     + "The effect we want is a multi-line label.";     Jframe f = new Jframe("Label Render Test");     f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     BufferedImage image = new BufferedImage(         400,         300,         BufferedImage.TYPE_INT_RGB);     Graphics2D imageGraphics = image.createGraphics();     GradientPaint gp = new GradientPaint(         20f,         20f,         Color.red,         380f,         280f,         Color.orange);     imageGraphics.setPaint(gp);     imageGraphics.fillRect(0, 0, 400, 300);     JLabel textLabel = new JLabel(title);     textLabel.setSize(textLabel.getPreferredSize());     Dimension d = textLabel.getPreferredSize();     BufferedImage bi = new BufferedImage(         d.width,         d.height,         BufferedImage.TYPE_INT_ARGB);     Graphics g = bi.createGraphics();     g.setColor(new Color(255, 255, 255, 128));     g.fillRoundRect(         0,         0,         bi.getWidth(f),         bi.getHeight(f),         15,         10);     g.setColor(Color.black);     textLabel.paint(g);     Graphics g2 = image.getGraphics();     g2.drawImage(bi, 20, 20, f);     ImageIcon ii = new ImageIcon(image);     JLabel imageLabel = new JLabel(ii);     f.getContentPane().add(imageLabel);     f.pack();     f.setLocationByPlatform(true);     f.setVisible(true); }        });    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号