I am working on a JavaFX gui, but for some reason, none of my ImageViews seem to be showing any images. I tried setting images in SceneBuilder, where they will only show in SceneBuilder and not when the actual program is run. I also tried setting writable images, which also did not show in the program. I am certain that the image paths are correct and I tried printing the pixel ArrayList, which seems to be correct. Why wouldn’t the ImageView work?
My image writer:
public ImageProcessor() {
width = 1;
height = 1;
image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
monochromeImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
pixels = new ArrayList<>();
}
// process image given its path
public void process(String path) {
try {
this.image = ImageIO.read(new File(path));
width = this.image.getWidth();
height = this.image.getHeight();
this.monochromeImage = new BufferedImage(this.width, this.height, BufferedImage.TYPE_BYTE_GRAY);
pixels.clear();
for (int y = 0; y < this.monochromeImage.getHeight(); ++y) {
pixels.add(new ArrayList<>());
for (int x = 0; x < this.width; ++x) {
int rgb = this.image.getRGB(x, y);
int grayValue = (int) (0.299 * ((rgb >> 16) & 0xFF) + 0.587 * ((rgb >> 8) & 0xFF) + 0.114 * (rgb & 0xFF));
this.monochromeImage.setRGB(x, y, (grayValue << 16) | (grayValue << 8) | grayValue);
pixels.get(y).add(grayValue);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
// write and return a WritableImage
public WritableImage writeImage() {
WritableImage writableImage = new WritableImage(this.width, this.height);
PixelWriter pixelWriter = writableImage.getPixelWriter();
for (int y = 0; y < this.height; ++y) {
for (int x = 0; x < this.width; ++x) {
int pixelValue = this.pixels.get(y).get(x);
pixelWriter.setArgb(x, y, pixelValue);
}
}
return writableImage;
}
My controller:
public class Controller {
// normal attributes
private ImageProcessor imageProcessor;
private WritableImage image;
// FXML attributes
@FXML private Button openImageButton;
@FXML private ImageView imageView;
// constructor
public Controller() {
imageProcessor = new ImageProcessor();
image = new WritableImage(1000, 1000);
}
// initialize after FXML fields are injected
@FXML public void initialize() {
imageView.setImage(image);
}
// user chooses an image when the openImageButton is pressed
@FXML void loadImage() {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Images", "*.png", "*.jpg", "*.jpeg", "*.bmp");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setTitle("Open Image File");
String userHome = System.getProperty("user.home");
fileChooser.setInitialDirectory(new File(userHome));
Stage stage = (Stage) this.openImageButton.getScene().getWindow();
File selectedFile = fileChooser.showOpenDialog(stage);
if (selectedFile != null) {
String path = selectedFile.getAbsolutePath();
this.imageProcessor.process(path);
this.imageProcessor.printPixels();
this.image = this.imageProcessor.writeImage();
this.imageView.setImage(this.image);
}
}
}
Main:
public class Main extends javafx.application.Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1500, 900);
Controller controller = fxmlLoader.getController();
stage.setTitle("Dose Mapper");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
ImageView’s FXML line:
<ImageView fx:id="imageView" fitHeight="861.0" fitWidth="909.0" layoutX="506.0" layoutY="113.0" pickOnBounds="true" preserveRatio="true" />
I tried setting an image path using SceneBuilder and creating objects in the program. After debugging, I found that the WritableImage and pixels should be correct. I want to set the ImageView’s image to the WritableImage returned from the function call, but nothing I’ve thrown at it is seeming to cooperate.
tsa7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.