2016-05-31 6 views
0

私はSwing Appをテストしていますが、ロボットを使ってAlt + ScreenPrintを実行して現在のウィンドウのイメージを取得していますが、テキストをsystemclipboardに最初にコピーすると、私は「テスト」ボタンをクリックした場合、それは画像と出力次を取得します。Javaでテキストをコピーした後にsystemclipboardイメージを取得するには?

Get_Clipboard_Image : [In Test_Clip_Board] transferable = [email protected] 
Get_Clipboard_Image : [In Test_Clip_Board] [email protected]: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 816 height = 664 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0 
Test_Image = [email protected]: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 816 height = 664 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0 

「Test_Image」ポイントのBufferedImageに、あなたはMSペイントプログラムに貼り付けることができます。

「コピー&テスト」ボタンをクリックすると、テキストがクリップボードにコピーされ、イメージがクリップボードにコピーされますが、イメージには表示されず、出力が表示されますこのように:あなたが見ることができるように

Get_Clipboard_Image : [In Test_Clip_Board] transferable = [email protected] 
Test_Image = null 

、何とか譲渡ではなく、画像を保持している「ClipboardTransferable」、および「Test_Image = NULL」の「としてTransferableProxy」になります。

しかし、奇妙なことは、画像がsystemclipboardにあり、それをMSペイントに貼り付けて、それが存在するのを見ることができますが、現時点ではJavaは"TransferableProxy"。

テキストをクリップボードにコピーしてから、クリップボードにイメージをコピーする必要があります。これを修正するにはどうすればよいですか?あなたはActionListenerでコードを実行するとき

import java.awt.*; 
import java.awt.datatransfer.*; 
import java.awt.event.*; 
import java.io.*; 
import javax.swing.*; 
import javax.swing.border.*; 

public class Test_Clip_Board extends JPanel 
{ 
    public static final long serialVersionUID=26362862L; 
    static Dimension Screen_Size=Toolkit.getDefaultToolkit().getScreenSize(); 
    static JFrame frame=new JFrame("Test_Clip_Board"); 
    int W=800,H=626,Upper_Left_Button_Panel_W=90; 
    String Current_Path=new File("").getAbsolutePath().replace("\\","/")+"/"; 
    static Insets An_Inset=new Insets(0,0,0,0); 
    JTextArea TextArea=new JTextArea("Test Clip Board"); 
    Robot robot; 
    Point Location; 

    public Test_Clip_Board() 
    { 
    TextArea.append("\n\nCurrent_Path = "+Current_Path); 
    try { robot=new Robot(); } 
    catch (Exception e) { e.printStackTrace(); } 

    FlowLayout Fl=new FlowLayout(0,0,0); 
    setLayout(Fl); 

    JPanel leftPanel=new JPanel(Fl); 
    leftPanel.setBorder(new EtchedBorder()); 
    leftPanel.setPreferredSize(new Dimension(W/2,H)); 
    add(leftPanel); 

    JPanel upperPanel=new JPanel(Fl); 
    upperPanel.setBorder(new EtchedBorder()); 
    upperPanel.setPreferredSize(new Dimension(W/2-2,H/4-2)); 
    leftPanel.add(upperPanel); 

    TextArea.setFont(new Font("Times New Roman",0,18)); 
    TextArea.setBorder(new EtchedBorder()); 
    TextArea.setPreferredSize(new Dimension(W/2-2-Upper_Left_Button_Panel_W-4,H/4-6)); 
    upperPanel.add(TextArea); 

    JPanel buttonPanel=new JPanel(new FlowLayout(0,0,30)); 
    buttonPanel.setBorder(new EtchedBorder()); 
    buttonPanel.setPreferredSize(new Dimension(Upper_Left_Button_Panel_W,H/4-6)); 
    upperPanel.add(buttonPanel); 

    JButton testButton=new JButton("Test"); 
    testButton.setForeground(new Color(0,0,230)); 
    testButton.setFont(new Font("Times New Roman",0,16)); 
    testButton.setMargin(An_Inset); 
    testButton.setPreferredSize(new Dimension(Upper_Left_Button_Panel_W-5,26)); 
    testButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Do_Test(); } }); 
    buttonPanel.add(testButton); 

    JButton copyAndTestButton=new JButton("Copy & Test"); 
    copyAndTestButton.setForeground(new Color(0,0,230)); 
    copyAndTestButton.setFont(new Font("Times New Roman",0,13)); 
    copyAndTestButton.setMargin(An_Inset); 
    copyAndTestButton.setPreferredSize(new Dimension(Upper_Left_Button_Panel_W-5,26)); 
    copyAndTestButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Do_Copy_And_Test(); } }); 
    buttonPanel.add(copyAndTestButton); 

    setPreferredSize(new Dimension(W,H)); 
    } 

    void Do_Copy_And_Test() 
    { 
    Clipboard system=Toolkit.getDefaultToolkit().getSystemClipboard(); 
    StringSelection sel=new StringSelection(TextArea.getText()); 
    system.setContents(sel,sel); 
    Do_Test(); 
    } 

    void Do_Test() 
    { 
    Point TeatArea_Location=TextArea.getLocationOnScreen(); 
    Robot_Do_Alt_PrintScreen(20,TeatArea_Location.x+30,TeatArea_Location.y+30,20); 
    Image Test_Image=Get_Clipboard_Image("In Test_Clip_Board"); 
    Out(" Test_Image = "+Test_Image); 
    } 

    Image Get_Clipboard_Image(String Text) 
    { 
    Image image=null; 
    Transferable transferable=Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 
    try 
    { 
     Out("Get_Clipboard_Image : ["+Text+"] transferable = "+transferable.toString()); 
     if (transferable!=null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) 
     { 
     image=(Image)transferable.getTransferData(DataFlavor.imageFlavor);  // getting image from Clipboard 
     Out("Get_Clipboard_Image : ["+Text+"] "+image); 
     } 
    } 
    catch (UnsupportedFlavorException e) { e.printStackTrace(); } 
    catch (IOException e) { e.printStackTrace(); } 

    return image; 
    } 

    void Robot_Do_Alt_PrintScreen(int Delay_Before_Click,int X,int Y,int Delay_After_Click) 
    { 
    robot.delay(Delay_Before_Click); 
    robot.mouseMove(X,Y); 
    robot.mousePress(InputEvent.BUTTON1_MASK); 
    robot.mouseRelease(InputEvent.BUTTON1_MASK); 
    robot.delay(20); 
    robot.keyPress(KeyEvent.VK_ALT); 
    robot.keyPress(KeyEvent.VK_PRINTSCREEN); 
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN); 
    robot.keyRelease(KeyEvent.VK_ALT); 
// if (Is_Test) Out("Robot_Click : [ X = "+X+" Y = "+Y+" ]"); 
    robot.delay(Delay_After_Click);  
    } 

    private static void out(String message) { System.out.print(message); } 
    private static void Out(String message) { System.out.println(message); } 

    // Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread. 
    static void Create_And_Show_GUI() 
    { 
    final Test_Clip_Board demo=new Test_Clip_Board(); 

    frame.add(demo); 
    frame.addWindowListener(new WindowAdapter() 
    { 
     public void windowActivated(WindowEvent e) { } 
     public void windowClosed(WindowEvent e) { } 
     public void windowClosing(WindowEvent e) { System.exit(0); } 
     public void windowDeactivated(WindowEvent e) { } 
     public void windowDeiconified(WindowEvent e) { demo.repaint(); } 
     public void windowGainedFocus(WindowEvent e) { demo.repaint(); } 
     public void windowIconified(WindowEvent e) { } 
     public void windowLostFocus(WindowEvent e) { } 
     public void windowOpening(WindowEvent e) { demo.repaint(); } 
     public void windowOpened(WindowEvent e) { } 
     public void windowResized(WindowEvent e) { demo.repaint(); } 
     public void windowStateChanged(WindowEvent e) { demo.repaint(); } 
    }); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
    // Schedule a job for the event-dispatching thread : creating and showing this application's GUI. 
    SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } }); 
    } 
} 

答えて

2

PrintScreenイベントがまだAWT-EventQueueによって処理されていません。

は、ここに私のアプリです。

あなたは

void Do_Test() { 
    Point TeatArea_Location = TextArea.getLocationOnScreen(); 
    Robot_Do_Alt_PrintScreen(20, TeatArea_Location.x + 30, TeatArea_Location.y + 30, 20); 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      Image Test_Image = Get_Clipboard_Image("In Test_Clip_Board"); 
      Out(" Test_Image = " + Test_Image); 
     } 
    }); 
} 

ようにコードを修正した場合、ボタンからActionListerのコードが終了した後SwingUtilities.invokeLaterの呼び出しから実行可能が実行されるように画像は、クリップボードから読み込まれます。

AWTイベントキューの順にprinscreenがエンキューされます

  • 同時に実行するために、他のAWTイベントをブロックしますボタンから

    • アクションハンドラを、イベントを処理します
    • 後で実行可能な呼び出しがエンキューされます

    FlavorListenerをクリップボードに追加する方が良い解決策と思われます。

  • +0

    私はそれを疑ったが、何をすべきか分からなかった、これはまさに私が探していたものであり、ありがとう! – Frank

    関連する問題