如何将图像设置为第一个场景中的矩形。当我单击“相机”按钮时,第二个场景打开时第一个场景仍处于打开状态。在第二个场景中,当我单击“捕获”按钮时,图像首先从第二个场景开始场景,但没有显示。我检查了图像是否单击,但唯一的问题是它没有以矩形显示。
提供场景控制器代码Java代码
图片链接:- First scene
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Controller implements Initializable{
@FXML
private Rectangle imageView;
@FXML
private Button button;
@FXML
private AnchorPane rootpane;
@FXML
void click(ActionEvent event) throws IOException {
Parent root=FXMLLoader.load(getClass().getResource("Selfi.fxml"));
Scene scene=new Scene(root);
Stage stage=new Stage();
stage.initStyle(StageStyle.TRANSPARENT);
stage.setScene(scene);
stage.show();
}
public void setImage(Image img)
{
imageView.setFill(new ImagePattern(img));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}}
第一个场景的Fxml代码:-
<?xml version="1.0" encoding="UTF-8"?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<AnchorPane fx:id="rootpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<Rectangle fx:id="imageView" arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="200.0" layoutX="177.0" layoutY="14.0" stroke="BLACK" strokeType="INSIDE" width="200.0" />
<Button fx:id="button" layoutX="246.0" layoutY="227.0" mnemonicParsing="false" onAction="#click">
<graphic>
<FontAwesomeIconView glyphName="CAMERA" />
</graphic>
</Button>
</children>
</AnchorPane>
第二个场景控制器(用于图像删除)
图片链接:-Image Cliker scene
public class selfiController implements Initializable {
Webcam webcam;
@FXML
private Rectangle videofeddback;
double x,y;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
webcam=Webcam.getDefault();
webcam.setViewSize(new Dimension(640,480));
webcam.open();
new videoFeedback().start();
}
@FXML
public void dragged(MouseEvent event)
{
Stage stage =(Stage)((Node)event.getSource()).getScene().getWindow();
stage.setX(event.getScreenX()-x);
stage.setY(event.getScreenY()-y);
}
@FXML
public void pressed(MouseEvent event)
{
x=event.getSceneX();
y=event.getSceneY();
}
@FXML
public void close(MouseEvent event)
{
Stage stage =(Stage)((Node)event.getSource()).getScene().getWindow();
stage.close();
webcam.close();
}
@FXML
public void min(MouseEvent event)
{
Stage stage =(Stage)((Node)event.getSource()).getScene().getWindow();
stage.setIconified(true);
}
@FXML
void capture(ActionEvent event) throws IOException {
BufferedImage image=webcam.getImage();
Image im = SwingFXUtils.toFXImage(image, null);
FXMLLoader loader=new FXMLLoader(selfiController.class.getResource("Demo.fxml"));
AnchorPane an = (AnchorPane) loader.load();
Controller secController=loader.getController();
secController.setImage(im);
webcam.close();
}
class videoFeedback extends Thread
{
@Override
public void run()
{
while(true)
{
try
{
BufferedImage image=webcam.getImage();
Image im = SwingFXUtils.toFXImage(image, null);
videofeddback.setFill(new ImagePattern(im));
Thread.sleep(40);
}
catch(Exception e)
{
}
}
}
}}
第二场景的Fxml代码:-
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.selfiController">
<children>
<HBox alignment="CENTER_RIGHT" onMouseDragged="#dragged" onMousePressed="#pressed" prefHeight="29.0" prefWidth="640.0" spacing="10.0">
<children>
<HBox alignment="CENTER_LEFT" HBox.hgrow="ALWAYS">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Image Clicker" />
</children>
<padding>
<Insets left="10.0" />
</padding>
</HBox>
<Label onMouseClicked="#min">
<graphic>
<FontAwesomeIconView glyphName="MINUS" />
</graphic>
</Label>
<Label onMouseClicked="#close">
<graphic>
<FontAwesomeIconView glyphName="CLOSE" />
</graphic>
</Label>
</children>
<padding>
<Insets right="10.0" />
</padding>
</HBox>
<JFXButton buttonType="RAISED" layoutX="289.0" layoutY="460.0" onAction="#capture" style="-fx-background-color: red;" text="Capture">
<effect>
<DropShadow />
</effect>
</JFXButton>
<Rectangle fx:id="videofeddback" arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="418.0" layoutY="29.0" stroke="WHITE" strokeType="INSIDE" width="640.0" />
</children>
<effect>
<DropShadow />
</effect>
</AnchorPane>