此代码的目的是从filechooser中选择一个图像,将该图像加载到ImageView中,并使用一个滑块来调整该图像的不透明度。
我目前正试图弄清楚我的代码有什么问题。我正在尝试为学校的作业完成这个程序,但我想我错过了一行代码。如果有人能解释我的错误是什么,那将非常感激。
package opacityadjuster;
import javafx.application.Application;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.FadeTransition;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class OpacityAdjuster extends Application{
ImageView myImageView;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage){
final double MIN = 0.0, MAX = 1.0, INITIAL = .50;
final double MAJOR_TICK_UNIT = .1;
final int MINOR_TICK_COUNT = 1;
final double SLIDER_WIDTH = 300.0;
Button button = new Button("Search for Image");
Slider slider = new Slider(MIN, MAX, INITIAL);
slider.setMajorTickUnit(MAJOR_TICK_UNIT);
slider.setMinorTickCount(MINOR_TICK_COUNT);
slider.setShowTickMarks(true);
slider.setShowTickLabels(true);
slider.setSnapToTicks(true);
slider.setPrefWidth(SLIDER_WIDTH);
button.setOnAction(e ->
{
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilterJPG =
new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
FileChooser.ExtensionFilter extFilterjpg =
new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilterPNG =
new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
FileChooser.ExtensionFilter extFilterpng =
new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters()
.addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
File file = fileChooser.showOpenDialog(null);
try {BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
myImageView.setImage(image);
}
catch (IOException ex) {
Logger.getLogger(OpacityAdjuster.class.getName()).log(Level.SEVERE, null, ex);
}
});
FadeTransition ft = new FadeTransition();
slider.valueProperty().addListener((observable, oldvalue, newvalue) ->{
ft.setFromValue(oldvalue.doubleValue());
ft.setToValue(newvalue.doubleValue());
ft.play();
});
myImageView = new ImageView();
myImageView.setFitWidth(500);
myImageView.setPreserveRatio(true);
VBox vBox = new VBox(10);
vBox.getChildren().addAll(button, slider, myImageView);
vBox.setAlignment(Pos.TOP_CENTER);
Scene scene = new Scene(vBox, 800, 800);
primaryStage.setTitle("Opacity Adjuster");
primaryStage.setScene(scene);
primaryStage.show();
}
}
这就是我所做的。我读过我的书,问同学,我的教授,Reddit,甚至用Google搜索出来的问题,正好寻找我遗失的最后一部分。
答案 0 :(得分:0)