Crop a part of an image ( javafx ) - java

I am new to overflow so I apologize in advance if I am not precise enough.
During a class project I have to make a tanquin game in javafx. I must be able to cut an image into x part in order to insert them then in an Arraylist with an id and the part of the image. But I have the error attached here.
I guess the reader is null and that the error is related to that. But I don't know how to fix it.
I never used those library so i dont know how to use them.
Thank you in advance for your reply.
Code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Image image = new Image("https://scontent-yyz1-1.cdninstagram.com/v/t51.2885-15/sh0.08/e35/c180.0.720.720a/s640x640/75272157_461406551233157_7963091763107249286_n.jpg?_nc_ht=scontent-yyz1-1.cdninstagram.com&_nc_cat=105&_nc_ohc=eu-9K8wyx6oAX9H695F&oh=429cc1e6dcec03badc9aac439b6b8ac0&oe=5EAD8EA3", true);
PixelReader reader = image.getPixelReader();
WritableImage newImage = new WritableImage(reader,100,100,100,100);
ImageView imageView = new ImageView(newImage);
HBox hbox = new HBox(imageView);
Scene scene = new Scene(hbox, 640, 640);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

Disable ImageView Anti-Aliasing in JavaFX [duplicate]

This question already has answers here:
JavaFX ImageView without any smoothing
(4 answers)
Closed 1 year ago.
I made an ImageView in JavaFX and load a pixel art into it. And I scaled up, but I can't clearly see the pixels of the art. The problem should be the anti-aliasing. So I need to see clearly the pixels like on the second link. How can I turn anti-aliasing off?
I tried with img1.setSmooth(false);
What I made in JavaFX:
How it supposed to look like: //preview made in piskel
Here's my code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override public void start(Stage primaryStage) {
AnchorPane root = new AnchorPane();
StackPane hb = new StackPane();
Image image = new Image("pixelart.png");
ImageView img = new ImageView(image);
img.setFitWidth(500);
img.setFitHeight(500);
img.setSmooth(false);
hb.getChildren().add(img);
root.getChildren().addAll(hb);
Scene scene = new Scene(root,800,400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {launch(args);}
}
pixelart.png:
One approach is to turn smoothing off when you construct the Image. Set the last argument of the constructor, smooth, to false.
Image image = new Image("pixelart.png", 400, 400, true, false);
As tested:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Image image = new Image("pixelart.png", 400, 400, true, false);
ImageView view = new ImageView(image);
root.getChildren().add(view);
Scene scene = new Scene(root, 450, 450);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX Slider ticks not showing correctly

What I want to do:
Have a slider, from 0 - 10, have it move only by a full tick(1), and show the value of the tick above the tick.
What happens:
It shows all values from 0-10 except for 9 which is mysteriously missing..
I wanted to upload an image here but I lack the reputation it seems ;)
https://pasteboard.co/HdPtzVp.png
Code:
Main Class:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class StartGui extends Application
{
#Override
public void start(Stage stage)
{
HBox box = new HBox();
PionnenSlider slider = new PionnenSlider();
box.getChildren().add(slider);
Scene scene = new Scene(box, 500, 300);
stage.setScene(scene);
stage.setTitle("SliderMinimalCode");
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Slider class:
import javafx.scene.control.Slider;
public class PionnenSlider extends Slider
{
public PionnenSlider()
{
setMinorTickCount(0);
setBlockIncrement(1);
setMajorTickUnit(1);
setValue(0);
setMax(10);
setShowTickMarks(true);
setShowTickLabels(true);
setSnapToTicks(true);
}
}
apparently it is due to lack of space for the slider, found a workaround to automaticly size it with HBox.setHgrow(sliderPionnen, Priority.ALWAYS);, although that just grows it to fill the entire HBOX.. so I guess I'l have to find a workaround for that

No image appearing on pane, how to fix this?

I wrote this java application on NetBeans IDE 8.2 to show an image stored on my local directory in the same directory of the classes files directory (as stated in the documentation), but when running there is no image appearing on the scene although there is no exception,, anyone has any idea how to fix this??
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.geometry.Insets;
import java.io.File;
public class ShowImage extends Application{
#Override
public void start(Stage primaryStage) {
Pane pane = new HBox();
pane.setPadding(new Insets(5, 5, 5, 5));
Image image = new Image("image.jpg");
pane.getChildren().add(new ImageView(image));
ImageView imageView2 = new ImageView(image);
imageView2.setFitHeight(100);
imageView2.setFitWidth(100);
pane.getChildren().add(imageView2);
ImageView imageView3 = new ImageView(image);
imageView3.setRotate(90);
pane.getChildren().add(imageView3);
Scene scene = new Scene(pane, 300, 300);
primaryStage.setTitle("Show Image");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
The final solution to this repeated problem is to create a new sub packadge from NetBeans under your packadge, name it "resources" for example and and put the your image in the directory of the sub package and edit the path in the code as this
Image image = new Image("resources/image.jpg");
this will avoid the path issues, and route the file from package directly.

Image doesn't load when converting from FXML to JAVA

I have converted a FXML file to Java and everything works except one image
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.StrokeType;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Screen extends Application {
#Override
public void start(Stage primaryStage){
primaryStage.setTitle("Player Screen");
GridPane grid = new GridPane();
Text coins = new Text();
coins.setStrokeWidth(0.0);
coins.setStrokeType(StrokeType.OUTSIDE);
GridPane.setColumnIndex(coins, 1);
coins.setId("coins");
coins.setText("11");
coins.setWrappingWidth(41.46875);
This is the code part of the image:
ImageView imageTile = new ImageView();
imageTile.setPickOnBounds(true);
imageTile.setFitWidth(200.0);
GridPane.setColumnIndex(imageTile, 17);
imageTile.setFitHeight(150.0);
GridPane.setRowIndex(imageTile, 2);
imageTile.setPreserveRatio(true);
File file = new File("/excommunicationTile1.jpg");
Image image = new Image(file.toURI().toString());
imageTile.setImage(image);
Both the image and the css file are in the same folder so i don't understand why it loads the css information but not the image.
grid.getChildren().add(coins);
grid.getChildren().add(imageTile);
Scene scene = new Scene(grid,546,200);
primaryStage.setScene(scene);
scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Replace File file = new File("/excommunicationTile1.jpg");
to
InputStream inputStream = this.getClass().getResourceAsStream("/excommunicationTile1.jpg")

Updating the location of a sprite on StackPane JavaFX

I have a question regarding JavaFX. I taught myself Java and now I'm learning JavaFX.
I've been trying to update the location of a 50x50 black block on the screen. I have a YAxis variable that when I change changes the location of the block.
I want the block to "flow" down the screen similar to Tetris.
My code is messy, as I'm just messing around with it, so please excuse that:
package gamefx;
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GameFX extends Application {
public Image img = new Image("Block1.png");
public ImageView image = new ImageView(img);
public int YAxis = -200;
#Override
public void start(Stage primaryStage) throws InterruptedException{
primaryStage.setTitle("Game");
StackPane stckp1 = new StackPane();
Scene scn = new Scene(stckp1, 700, 700);
primaryStage.setScene(scn);
primaryStage.show();
image.setTranslateY(YAxis);
stckp1.getChildren().add(image);
}
}
Since you want to see your block moving, you need an animation. See Oracle tutorial for more information.
A sample code, quickly written:
TranslateTransition tt = new TranslateTransition(Duration.millis(2000), image);
tt.setByY(yAxis);
tt.setCycleCount(1);
tt.play();
Side-note: Variable names shall not start with an upper-case letter. Use yAxis instead of YAxis.

Categories

Resources