2011-07-29 10 views
1

私はボタンプレスでスクリーンショットを増分するこのbeanShellスクリプトを作成しました。これはJythonでJavaを使用して実際のスクリーンショット(クロスプラットフォームなので)を行う方法を理解しようとしています。Javaコードをjythonスクリプトに埋め込む方法。

私はあまりうまくやっていないし、誰かがJythonの部分にJavaの部分を挿入する方法を私に見せてもらえるかどうか疑問に思っていた(私はGUIとイベントがある - 下記参照)?

これはJavaの一部です...

Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); 
Robot robot = new Robot(); 
Rectangle rect = new Rectangle(0, 0, scr.width, scr.height); 
BufferedImage image = robot.createScreenCapture(rect); 
ImageIO.write(image, "jpeg", new File("Captured" + c + ".jpg")); 

これはこれは私がこれまで持っているJythonスクリプトで全体のBeanShellスクリプト

import java.awt.Toolkit; 
import java.awt.event.KeyEvent; 
import java.awt.image.BufferedImage; 
import javax.imageio.ImageIO; 
import java.io.File; 
import java.io.IOException; 

int c = 0; // image counter 

buttonHandler = new ActionListener() { 
    actionPerformed(this) { 

    Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); 

    // Allocate a Robot instance, and do a screen capture 
    Robot robot = new Robot(); 
    Rectangle rect = new Rectangle(0, 0, scr.width, scr.height); 
    BufferedImage image = robot.createScreenCapture(rect); 

    // Save the captured image to file with ImageIO (JDK 1.4) 
    ImageIO.write(image, "jpeg", new File("Captured" + c + ".jpg")); 
    c++; 
    } 
}; 

button = new JButton("Click to save incrementing screenshots to this app's location"); 
button.addActionListener(buttonHandler); 
// JLabel label1 = new JLabel("hello"); 
frame(button); 

です...

from javax.swing import JButton, JFrame 
from java.awt import Toolkit 
from java.awt.event import KeyEvent; 
from java.awt.image import BufferedImage; 
from javax.imageio import ImageIO;  
from java.io import File, IOException 

c = 0 

frame = JFrame(
    'App Title', 
    defaultCloseOperation = JFrame.EXIT_ON_CLOSE, 
    size = (450, 60) 
) 


def change_text(event): 
    global c 
    ... 
    // Java part 
    ... 
    c = c + 1 


button = JButton(
    "Click to save incrementing screenshots to this app's location", 
    actionPerformed=change_text 
) 

frame.add(button) 
frame.visible = True 

感謝:)

+0

@レオネルの答えがあなたの問題を解決した場合、彼の答えを受け入れるべきです。彼の答えの隣にある緑のチックをクリックしてください:) –

答えて

1

ラップパブリックJavaクラスでJavaコードの抜粋:

package com.mycompany; 
public class ScreenshotEngine { 
    public void takeScreenshot(String filename) { 
    // Code that actually takes the screenshot and saves it to a file 
    } 
} 

それをコンパイルし、アプリケーションのクラスパス上で、それを利用可能にすることを忘れないでください。

次に、jythonスクリプトから、ほかのJavaクラスと同じように使用できます。あなたは上記のスニペットでは、JDKからJButtonJFrameを使用する方法を知っている

# Using the fully qualified name of the class 
engine = com.mycompany.ScreenshotEngine() 
engine.takeScreenshot('/tmp/sc1.png') 

# You can also use import to shorten class names 
from com.mycompany import ScreenshotEngine 
engine = ScreenshotEngine() 
engine.takeScreenshot('/tmp/sc2.png') 

?それは同じことです。

関連する問題