如何在javafx中拍摄屏幕选定区域的快照?
我正在尝试拍摄屏幕上当前打开的任何文件的快照。在谷歌搜索之后,我得到的只是用javafx截图。我不想使用任何AWT或Swing组件。有什么办法吗?这是我使用过的主要机制:一个透明的如何在javafx中拍摄屏幕选定区域的快照?,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我正在尝试拍摄屏幕上当前打开的任何文件的快照。在谷歌搜索之后,我得到的只是用javafx截图。我不想使用任何AWT或Swing组件。有什么办法吗?这是我使用过的主要机制:一个透明的舞台和透明的画布,它有一个边框窗格和不透明度0.1 下面是一个简单的示例(仅用于选择区域…): 启动应用程序时,关闭它的唯一方法是使用ESCAPE 测试仪等级: import javafx.application.Application; import javafx.stage.Screen; import jav
舞台
和透明的画布
,它有一个边框窗格
和不透明度0.1
下面是一个简单的示例(仅用于选择区域…):
启动应用程序时,关闭它的唯一方法是使用ESCAPE
测试仪等级:
import javafx.application.Application;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class Tester extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
CaptureWindow window = new CaptureWindow(Screen.getPrimary().getBounds().getWidth(),Screen.getPrimary().getBounds().getHeight(), primaryStage);
window.show();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
* Is used to capture an area of the screen.
*
* @author GOXR3PLUS
*/
public class CaptureWindow extends Stage {
/** The border pane. */
// BorderPane and Canvas
BorderPane borderPane = new BorderPane();
/** The canvas. */
Canvas canvas = new Canvas();
/** The gc. */
GraphicsContext gc = canvas.getGraphicsContext2D();
/** The stage. */
Stage stage;
/** The width. */
// Variables
int width;
/** The height. */
int height;
/** The x pressed. */
int xPressed = 0;
/** The y pressed. */
int yPressed = 0;
/** The x now. */
int xNow = 0;
/** The y now. */
int yNow = 0;
/** The foreground. */
Color foreground = Color.rgb(255, 167, 0);
/** The background. */
Color background = Color.rgb(0, 0, 0, 0.3);
/**
* Constructor.
*
* @param screenWidth the screen width
* @param screenHeight the screen height
* @param primary the primary
*/
public CaptureWindow(double screenWidth, double screenHeight, Stage primary) {
stage = primary;
setX(0);
setY(0);
setWidth(screenWidth);
setHeight(screenHeight);
initOwner(primary);
initStyle(StageStyle.TRANSPARENT);
setAlwaysOnTop(true);
// BorderPane
borderPane.setStyle("-fx-background-color:rgb(0,0,0,0.1);");
// Canvas
canvas.setWidth(screenWidth);
canvas.setHeight(screenHeight);
canvas.setOnMousePressed(m -> {
xPressed = (int) m.getScreenX();
yPressed = (int) m.getScreenY();
});
canvas.setOnMouseDragged(m -> {
xNow = (int) m.getScreenX();
yNow = (int) m.getScreenY();
repaintCanvas();
});
borderPane.setCenter(canvas);
// Scene
setScene(new Scene(borderPane, Color.TRANSPARENT));
getScene().setCursor(Cursor.CROSSHAIR);
getScene().setOnKeyReleased(key -> {
if (key.getCode() == KeyCode.B) {
close();
System.out.println("Key Released....");
}else if(key.getCode() == KeyCode.ESCAPE)
close();
});
// gc
gc.setLineDashes(6);
gc.setFont(Font.font("null", FontWeight.BOLD, 14));
}
/**
* Repaints the canvas *.
*/
protected void repaintCanvas() {
gc.clearRect(0, 0, getWidth(), getHeight());
gc.setStroke(foreground);
gc.setFill(background);
gc.setLineWidth(3);
if (xNow > xPressed && yNow > yPressed) { // Right and Down
calculateWidthAndHeight(xNow - xPressed, yNow - yPressed);
gc.strokeRect(xPressed, yPressed, width, height);
gc.fillRect(xPressed, yPressed, width, height);
} else if (xNow < xPressed && yNow < yPressed) { // Left and Up
calculateWidthAndHeight(xPressed - xNow, yPressed - yNow);
gc.strokeRect(xNow, yNow, width, height);
gc.fillRect(xNow, yNow, width, height);
} else if (xNow > xPressed && yNow < yPressed) { // Right and Up
calculateWidthAndHeight(xNow - xPressed, yPressed - yNow);
gc.strokeRect(xPressed, yNow, width, height);
gc.fillRect(xPressed, yNow, width, height);
} else if (xNow < xPressed && yNow > yPressed) { // Left and Down
calculateWidthAndHeight(xPressed - xNow, yNow - yPressed);
gc.strokeRect(xNow, yPressed, width, height);
gc.fillRect(xNow, yPressed, width, height);
}
}
/**
* Show the window.
*/
public void showWindow() {
xNow = 0;
yNow = 0;
xPressed = 0;
yPressed = 0;
repaintCanvas();
show();
}
/**
* Calculates the width and height of the rectangle.
*
* @param w the w
* @param h the h
*/
private final void calculateWidthAndHeight(int w, int h) {
width = w;
height = h;
}
/**
* Selects whole Screen.
*/
public void selectWholeScreen() {
xPressed = 0;
yPressed = 0;
xNow = (int) getWidth();
yNow = (int) getHeight();
}
/**
* Return an array witch contains the (UPPER_LEFT) Point2D of the rectangle
* and the width and height of the rectangle.
*
* @return the int[]
*/
public int[] calculatedRectangle() {
if (xNow > xPressed) { // Right
if (yNow > yPressed) // and DOWN
return new int[] { xPressed, yPressed, xNow - xPressed, yNow - yPressed };
else if (yNow < yPressed) // and UP
return new int[] { xPressed, yNow, xNow - xPressed, yPressed - yNow };
} else if (xNow < xPressed) { // LEFT
if (yNow > yPressed) // and DOWN
return new int[] { xNow, yPressed, xPressed - xNow, yNow - yPressed };
else if (yNow < yPressed) // and UP
return new int[] { xNow, yNow, xPressed - xNow, yPressed - yNow };
}
return new int[] { xPressed, yPressed, xNow, yNow };
}
捕获窗口类:
import javafx.application.Application;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class Tester extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
CaptureWindow window = new CaptureWindow(Screen.getPrimary().getBounds().getWidth(),Screen.getPrimary().getBounds().getHeight(), primaryStage);
window.show();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
* Is used to capture an area of the screen.
*
* @author GOXR3PLUS
*/
public class CaptureWindow extends Stage {
/** The border pane. */
// BorderPane and Canvas
BorderPane borderPane = new BorderPane();
/** The canvas. */
Canvas canvas = new Canvas();
/** The gc. */
GraphicsContext gc = canvas.getGraphicsContext2D();
/** The stage. */
Stage stage;
/** The width. */
// Variables
int width;
/** The height. */
int height;
/** The x pressed. */
int xPressed = 0;
/** The y pressed. */
int yPressed = 0;
/** The x now. */
int xNow = 0;
/** The y now. */
int yNow = 0;
/** The foreground. */
Color foreground = Color.rgb(255, 167, 0);
/** The background. */
Color background = Color.rgb(0, 0, 0, 0.3);
/**
* Constructor.
*
* @param screenWidth the screen width
* @param screenHeight the screen height
* @param primary the primary
*/
public CaptureWindow(double screenWidth, double screenHeight, Stage primary) {
stage = primary;
setX(0);
setY(0);
setWidth(screenWidth);
setHeight(screenHeight);
initOwner(primary);
initStyle(StageStyle.TRANSPARENT);
setAlwaysOnTop(true);
// BorderPane
borderPane.setStyle("-fx-background-color:rgb(0,0,0,0.1);");
// Canvas
canvas.setWidth(screenWidth);
canvas.setHeight(screenHeight);
canvas.setOnMousePressed(m -> {
xPressed = (int) m.getScreenX();
yPressed = (int) m.getScreenY();
});
canvas.setOnMouseDragged(m -> {
xNow = (int) m.getScreenX();
yNow = (int) m.getScreenY();
repaintCanvas();
});
borderPane.setCenter(canvas);
// Scene
setScene(new Scene(borderPane, Color.TRANSPARENT));
getScene().setCursor(Cursor.CROSSHAIR);
getScene().setOnKeyReleased(key -> {
if (key.getCode() == KeyCode.B) {
close();
System.out.println("Key Released....");
}else if(key.getCode() == KeyCode.ESCAPE)
close();
});
// gc
gc.setLineDashes(6);
gc.setFont(Font.font("null", FontWeight.BOLD, 14));
}
/**
* Repaints the canvas *.
*/
protected void repaintCanvas() {
gc.clearRect(0, 0, getWidth(), getHeight());
gc.setStroke(foreground);
gc.setFill(background);
gc.setLineWidth(3);
if (xNow > xPressed && yNow > yPressed) { // Right and Down
calculateWidthAndHeight(xNow - xPressed, yNow - yPressed);
gc.strokeRect(xPressed, yPressed, width, height);
gc.fillRect(xPressed, yPressed, width, height);
} else if (xNow < xPressed && yNow < yPressed) { // Left and Up
calculateWidthAndHeight(xPressed - xNow, yPressed - yNow);
gc.strokeRect(xNow, yNow, width, height);
gc.fillRect(xNow, yNow, width, height);
} else if (xNow > xPressed && yNow < yPressed) { // Right and Up
calculateWidthAndHeight(xNow - xPressed, yPressed - yNow);
gc.strokeRect(xPressed, yNow, width, height);
gc.fillRect(xPressed, yNow, width, height);
} else if (xNow < xPressed && yNow > yPressed) { // Left and Down
calculateWidthAndHeight(xPressed - xNow, yNow - yPressed);
gc.strokeRect(xNow, yPressed, width, height);
gc.fillRect(xNow, yPressed, width, height);
}
}
/**
* Show the window.
*/
public void showWindow() {
xNow = 0;
yNow = 0;
xPressed = 0;
yPressed = 0;
repaintCanvas();
show();
}
/**
* Calculates the width and height of the rectangle.
*
* @param w the w
* @param h the h
*/
private final void calculateWidthAndHeight(int w, int h) {
width = w;
height = h;
}
/**
* Selects whole Screen.
*/
public void selectWholeScreen() {
xPressed = 0;
yPressed = 0;
xNow = (int) getWidth();
yNow = (int) getHeight();
}
/**
* Return an array witch contains the (UPPER_LEFT) Point2D of the rectangle
* and the width and height of the rectangle.
*
* @return the int[]
*/
public int[] calculatedRectangle() {
if (xNow > xPressed) { // Right
if (yNow > yPressed) // and DOWN
return new int[] { xPressed, yPressed, xNow - xPressed, yNow - yPressed };
else if (yNow < yPressed) // and UP
return new int[] { xPressed, yNow, xNow - xPressed, yPressed - yNow };
} else if (xNow < xPressed) { // LEFT
if (yNow > yPressed) // and DOWN
return new int[] { xNow, yPressed, xPressed - xNow, yNow - yPressed };
else if (yNow < yPressed) // and UP
return new int[] { xNow, yNow, xPressed - xNow, yPressed - yNow };
}
return new int[] { xPressed, yPressed, xNow, yNow };
}
导入javafx.scene.Cursor;
导入javafx.scene.scene;
导入javafx.scene.canvas.canvas;
导入javafx.scene.canvas.GraphicsContext;
导入javafx.scene.input.KeyCode;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.paint.Color;
导入javafx.scene.text.Font;
导入javafx.scene.text.FontWeight;
导入javafx.stage.stage;
导入javafx.stage.StageStyle;
/**
*用于捕获屏幕的一个区域。
*
*@author GOXR3PLUS
*/
公共类捕获窗口扩展阶段{
/**边框窗格*/
//边框窗格和画布
BorderPane BorderPane=新的BorderPane();
/**画布*/
画布=新画布();
/**gc*/
GraphicsContext gc=canvas.getGraphicsContext2D();
/**舞台*/
阶段性;
/**宽度*/
//变数
整数宽度;
/**高度*/
内部高度;
/**按下x键*/
int xPressed=0;
/**那个男孩按了按*/
int yPressed=0;
/**现在是x*/
int xNow=0;
/**现在的天气很冷*/
int-yNow=0;
/**前景*/
彩色前景=Color.rgb(255,167,0);
/**背景*/
颜色背景=Color.rgb(0,0,0,0.3);
/**
*构造器。
*
*@param screenWidth屏幕宽度
*@param screenHeight屏幕高度
*@param primary主参数
*/
公共捕获窗口(双屏宽、双屏高、舞台主屏幕){
阶段=初级;
setX(0);
setY(0);
设置宽度(屏幕宽度);
设置高度(屏幕高度);
初始所有者(主要);
initStyle(StageStyle.TRANSPARENT);
setAlwaysOnTop(真);
//边界窗格
边框窗格.setStyle(“-fx背景色:rgb(0,0,0,0.1);”);
//帆布
canvas.setWidth(屏幕宽度);
canvas.setHeight(屏幕高度);
canvas.setOnMousePressed(m->{
xPressed=(int)m.getScreenX();
yPressed=(int)m.getScreenY();
});
canvas.setonMouseDrawed(m->{
xNow=(int)m.getScreenX();
yNow=(int)m.getScreenY();
重新绘制画布();
});
边框窗格。设置中心(画布);
//场面
setScene(新场景(边框窗格,颜色.透明));
getScene().setCursor(Cursor.CROSSHAIR);
getScene().setOnKeyReleased(键->{
if(key.getCode()==KeyCode.B){
close();
System.out.println(“密钥释放…”);
}else if(key.getCode()==KeyCode.ESCAPE)
close();
});
//gc
gc.setlinedash(6);
gc.setFont(Font.Font(“null”,fontwweight.BOLD,14));
}
/**
*重新绘制画布*。
*/
受保护的void repaincanvas(){
clearRect(0,0,getWidth(),getHeight());
gc.设定行程(前景);
gc.setFill(背景);
gc.设置线宽(3);
如果(xNow>xPressed&&yNow>yppressed){//右下
计算手的高度(xNow-xPressed,yNow-yPressed);
gc.strokeRect(X压缩、Y压缩、宽度、高度);
gc.fillRect(X压缩、Y压缩、宽度、高度);
}else如果(xNowxPressed&&yNowyppressed){//Left和Down
计算手高度(xPressed-xNow,yNow-yPressed);
gc.strokeRect(xNow、yPressed、宽度、高度);
gc.fillRect(xNow、yPressed、宽度、高度);
}
}
/**
*显示窗口。
*/
公共void showWindow(){
xNow=0;
yNow=0;
xPressed=0;
yPressed=0;
重新绘制画布();
show();
}
/**
*计算矩形的宽度和高度。
*
*@param w the w
*@param h the h
*/
专用最终空隙计算宽度高度(int w,int h){
宽度=w;
高度=h;
}
/**
*选择整个屏幕。
*/
公共屏幕(){
xPressed=0;
yPressed=0;
xNow=(int)getWidth();
yNow=(int)getHeight();
}
/**
*返回包含矩形(左上)点2D的数组
*矩形的宽度和高度。
*
*@返回int[]
*/
public int[]calculatedRectangle(){
如果(xNow>xPressed){//Right
如果(yNow>yppressed)//并向下
返回新的int[]{xPressed,yPressed,xNow-xPressed,yNow-yppressed};
else if(yNowyppressed)//并向下
返回新的int[]{xNow,yPressed,xPressed-xNow,yNow-yPr