将 Java 代码翻译成 Jython 代码
Translate Java code into Jython code
下面的代码写在Java。它已被缩短以简化回答所需的时间。完整代码截取屏幕截图并将其复制到剪贴板。 (来源:http://www.coderanch.com/t/333565/GUI/java/BufferedImage-System-Clipboard)
我正在尝试在 Jython 2.7 程序中使用它。由于涉及所有私有 类,我对如何更改此代码感到困惑。
public class ClipboardImage implements ClipboardOwner {
public ClipboardImage() {
try {
Robot robot = new Robot();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screen = new Rectangle( screenSize );
BufferedImage i = robot.createScreenCapture( screen );
TransferableImage trans = new TransferableImage( i );
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
c.setContents( trans, this );
}
catch ( AWTException x ) {
x.printStackTrace();
System.exit( 1 );
}
}
public static void main( String[] arg ) {
ClipboardImage ci = new ClipboardImage();
}
public void lostOwnership( Clipboard clip, Transferable trans ) {
System.out.println( "Lost Clipboard Ownership" );
}
private class TransferableImage implements Transferable {
Image i;
public TransferableImage( Image i ) {
this.i = i;
}
public Object getTransferData( DataFlavor flavor )
throws UnsupportedFlavorException, IOException {
if ( flavor.equals( DataFlavor.imageFlavor ) && i != null ) {
return i;
}
else {
throw new UnsupportedFlavorException( flavor );
}
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] flavors = new DataFlavor[ 1 ];
flavors[ 0 ] = DataFlavor.imageFlavor;
return flavors;
}
public boolean isDataFlavorSupported( DataFlavor flavor ) {
DataFlavor[] flavors = getTransferDataFlavors();
for ( int i = 0; i < flavors.length; i++ ) {
if ( flavor.equals( flavors[ i ] ) ) {
return true;
}
}
return false;
}
}
}
我已经能够将其翻译成以下代码:
robot = Robot()
screenSize = Toolkit.getDefaultToolkit().getScreenSize()
screen = Rectangle(screenSize)
i = robot.createScreenCapture(screen)
flavors = DataFlavor.imageFlavor.equals(i)
trans = Transferable.getTransferDataFlavors(flavors)
c = Toolkit.getDefaultToolkit().getSystemClipboard()
c.setContents(trans, trans)
但是,我收到类型错误:
getTransferDataFlavors(): self arg can't be coerced to java.awt.datatransfer.Transferable.
我正在努力将 dataFlavor.imageFlavor
分配给我的图像,然后使其可转移,以便将其设置为剪贴板内容。
class TransferableImage(Transferable) :
def __init__(self, image) :
if not isinstance(image, Image) :
raise IllegalArgumentException("Parameter must be of type java.awt.Image")
self.image = image
def getTransferData(self, flavor) :
if not isinstance(flavor, DataFlavor) :
raise IllegalArgumentException("Parameter must be of type java.awt.datatransfer.DataFlavor")
if not flavor.equals(DataFlavor.imageFlavor) :
raise UnsupportedFlavorException(flavor)
return self.image
def getTransferDataFlavors(self) :
return [DataFlavor.imageFlavor];
def isDataFlavorSupported(self, flavor) :
if not isinstance(flavor, DataFlavor) :
raise IllegalArgumentException("Parameter must be of type java.awt.datatransfer.DataFlavor")
return flavor in getTransferFlavors()
class ClipboardImage(ClipboardOwner) :
def __init__(self) :
robot = Robot()
toolkit = Toolkit.getDefaultToolkit()
screenSize = toolkit.getScreenSize()
screen = Rectangle(screenSize)
image = robot.createScreenCapture(screen)
trans = TransferableImage(image)
clipboard = toolkit.getSystemClipboard()
clipboard.setContents(trans, self)
def lostOwnership(self, clip, trans) :
print("Lost Clipboard Ownership")
ci = ClipboardImage()
下面的代码写在Java。它已被缩短以简化回答所需的时间。完整代码截取屏幕截图并将其复制到剪贴板。 (来源:http://www.coderanch.com/t/333565/GUI/java/BufferedImage-System-Clipboard)
我正在尝试在 Jython 2.7 程序中使用它。由于涉及所有私有 类,我对如何更改此代码感到困惑。
public class ClipboardImage implements ClipboardOwner {
public ClipboardImage() {
try {
Robot robot = new Robot();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screen = new Rectangle( screenSize );
BufferedImage i = robot.createScreenCapture( screen );
TransferableImage trans = new TransferableImage( i );
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
c.setContents( trans, this );
}
catch ( AWTException x ) {
x.printStackTrace();
System.exit( 1 );
}
}
public static void main( String[] arg ) {
ClipboardImage ci = new ClipboardImage();
}
public void lostOwnership( Clipboard clip, Transferable trans ) {
System.out.println( "Lost Clipboard Ownership" );
}
private class TransferableImage implements Transferable {
Image i;
public TransferableImage( Image i ) {
this.i = i;
}
public Object getTransferData( DataFlavor flavor )
throws UnsupportedFlavorException, IOException {
if ( flavor.equals( DataFlavor.imageFlavor ) && i != null ) {
return i;
}
else {
throw new UnsupportedFlavorException( flavor );
}
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] flavors = new DataFlavor[ 1 ];
flavors[ 0 ] = DataFlavor.imageFlavor;
return flavors;
}
public boolean isDataFlavorSupported( DataFlavor flavor ) {
DataFlavor[] flavors = getTransferDataFlavors();
for ( int i = 0; i < flavors.length; i++ ) {
if ( flavor.equals( flavors[ i ] ) ) {
return true;
}
}
return false;
}
}
}
我已经能够将其翻译成以下代码:
robot = Robot()
screenSize = Toolkit.getDefaultToolkit().getScreenSize()
screen = Rectangle(screenSize)
i = robot.createScreenCapture(screen)
flavors = DataFlavor.imageFlavor.equals(i)
trans = Transferable.getTransferDataFlavors(flavors)
c = Toolkit.getDefaultToolkit().getSystemClipboard()
c.setContents(trans, trans)
但是,我收到类型错误:
getTransferDataFlavors(): self arg can't be coerced to java.awt.datatransfer.Transferable.
我正在努力将 dataFlavor.imageFlavor
分配给我的图像,然后使其可转移,以便将其设置为剪贴板内容。
class TransferableImage(Transferable) :
def __init__(self, image) :
if not isinstance(image, Image) :
raise IllegalArgumentException("Parameter must be of type java.awt.Image")
self.image = image
def getTransferData(self, flavor) :
if not isinstance(flavor, DataFlavor) :
raise IllegalArgumentException("Parameter must be of type java.awt.datatransfer.DataFlavor")
if not flavor.equals(DataFlavor.imageFlavor) :
raise UnsupportedFlavorException(flavor)
return self.image
def getTransferDataFlavors(self) :
return [DataFlavor.imageFlavor];
def isDataFlavorSupported(self, flavor) :
if not isinstance(flavor, DataFlavor) :
raise IllegalArgumentException("Parameter must be of type java.awt.datatransfer.DataFlavor")
return flavor in getTransferFlavors()
class ClipboardImage(ClipboardOwner) :
def __init__(self) :
robot = Robot()
toolkit = Toolkit.getDefaultToolkit()
screenSize = toolkit.getScreenSize()
screen = Rectangle(screenSize)
image = robot.createScreenCapture(screen)
trans = TransferableImage(image)
clipboard = toolkit.getSystemClipboard()
clipboard.setContents(trans, self)
def lostOwnership(self, clip, trans) :
print("Lost Clipboard Ownership")
ci = ClipboardImage()