2011-06-10 42 views
1

所以我做了一個應用程序,從csv文件創建一個圖形化的時間線。我已經完成了這部分,現在我只需要幫助讓圖像「漂亮」。捕獲圖像時,JFrame的邊框也被捕獲! 我該如何使邊界不被捕獲?或者我該如何擺脫它並保持圖像的大小? enter image description here如何擺脫通過捕捉jframe圖像文件所產生的邊界?

+0

潛在的重複:[如何從JFrame屏幕截圖移除標題欄?](http://stackoverflow.com/questions/4515902/how-to-remove-the-title-bar-from-a-jframe -screenshot) – Darien 2011-06-10 20:54:18

+0

不完全是我想到的。我知道如何獲得屏幕截圖。我正在尋找直接捕獲幀內圖像 – 2011-06-10 21:28:05

+0

另一個問題提到「標題欄頂部的空白條」......不是我所看到的圍繞您的灰色邊框的一部分圖片? – Darien 2011-06-10 21:41:13

回答

0
BufferedImage image = (BufferedImage)createImage(getContentPane().getSize().width, getContentPane().getSize().height); 
getContentPane().paint(image.getGraphics()); 

這是我相信我一直在尋找的東西。

+0

它確實很短。但是...你是否介意以這種方式修改你的答案,其他人(比如我)也可以使用它?你在哪裏得到這個神奇的'createImage()'方法?哪個類包含它,或者裏面有什麼? – MockerTim 2011-06-14 09:00:04

+1

這是一種JFrame從java.awt.Component繼承的方法。 它需要在寬度和高度,並返回一個圖像。 – 2011-06-14 14:16:19

2

這是一個簡單的例子。只是爲了澄清你的需求。基於solution of How to remove the title bar from a JFrame Screenshot?

以下程序會截取其JFrame並將其寫入文件。

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JFrame; 

/* Writes self screenshot on Screenshot button click. */ 
public class ScreenshotFrame extends JFrame { 

    public ScreenshotFrame() { 
     initComponents(); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new ScreenshotFrame().setVisible(true); 
      } 
     }); 
    } 

    private void initComponents() { 
     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     JButton screenshotButton = new JButton(); 

     screenshotButton.setText("Screenshot"); 
     screenshotButton.setToolTipText("Take my screenshot."); 
     screenshotButton.addActionListener(new ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       writeImageToFile(getScreenshot()); 
      } 
     });   

     getContentPane().setLayout(new FlowLayout()); 
     getContentPane().add(screenshotButton); 

     pack(); 
    } 

    /* Modified method from pointed solution. */ 
    private BufferedImage getScreenshot() { 
     Dimension dim = this.getContentPane().getSize(); 
     BufferedImage image = 
       new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); 
     this.getContentPane().paint(image.getGraphics()); 
     return image; 
    } 

    /* Write image to png file in current dir.*/ 
    private void writeImageToFile(BufferedImage image) { 
     try { 
      File file = new File("JFrameScreenshot.png"); 
      file.createNewFile(); 
      ImageIO.write(image, "png", file); 
     } catch (IOException ex) {/*do smth*/ } 
    } 
} 

這是你想要的,if_zero_equals_one?如果沒有,也許你可以在你的問題中添加一些代碼,試圖做你想做的事情。

P.S.感謝Dariencamickr,他們指出在哪裏找到該示例的來源。 也許這應該是一條評論。但這種格式更清晰。