import java.nio.IntBuffer; import java.util.Arrays; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.geometry.Rectangle2D; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ScrollPane; import javafx.scene.effect.DropShadow; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.image.PixelReader; import javafx.scene.image.PixelWriter; import javafx.scene.image.WritableImage; import javafx.scene.image.WritablePixelFormat; import javafx.scene.input.DragEvent; import javafx.scene.input.Dragboard; import javafx.scene.input.TransferMode; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundImage; import javafx.scene.layout.BackgroundPosition; import javafx.scene.layout.BackgroundRepeat; import javafx.scene.layout.BackgroundSize; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class Images extends Application { public static void main(String[] args) { Application.launch(args); } public void start(Stage stage) throws Exception { VBox root = new VBox(15.0); root.setAlignment(Pos.CENTER); root.setPadding(new Insets(20.0)); /*root.setStyle( "-fx-background-image: url(/img/bg_image.jpg); " + "-fx-background-repeat: repeat;" );*/ try { Image im = new Image( getClass().getResourceAsStream("bg_image.jpg")); root.setBackground(new Background( new BackgroundImage(im, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT))); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); } Button button = new Button("Image"); Button button2 = new Button("ImageView"); Button button3 = new Button("ImageView drag & drop"); Button button4 = new Button("ImageView Viewport"); Button button5 = new Button("PixelReader"); Button button6 = new Button("WritableImage"); Button button7 = new Button("PixelWriter"); button.setOnAction(event -> { newWindow(); }); button2.setOnAction(event -> { newWindow2(); }); button3.setOnAction(event -> { newWindow3(); }); button4.setOnAction(event -> { newWindow4(); }); button5.setOnAction(event -> { newWindow5(); }); button6.setOnAction(event -> { newWindow6(); }); button7.setOnAction(event -> { newWindow7(); }); root.getChildren().addAll(button, button2, button3, button4, button5, button6, button7); Scene scene = new Scene(root, 400.0, 400.0); stage.setTitle("Работа с изображениями"); stage.setScene(scene); stage.show(); } public void newWindow() { Image image = null; try { image = new Image( getClass().getResourceAsStream("image1.jpg")); System.out.println(image.isError() ? "Ошибка" : "Нет"); if (image.isError()) new RuntimeException(); } catch (Exception e) { System.out.println("Не удалось загрузить изображение1"); return; } Image image2 = null; try { image2 = new Image(getClass().getResourceAsStream("image2.jpg"), 0, 600, true, true); if (image2.isError()) new RuntimeException(); System.out.println(image2.getWidth() + " " + image2.getHeight()); System.out.println(image2.getRequestedWidth() + " " + image2.getRequestedHeight()); System.out.println(image2.isPreserveRatio()); System.out.println(image2.isSmooth()); System.out.println(image2.isBackgroundLoading()); } catch (Exception e) { System.out.println("Не удалось загрузить изображение2"); return; } Image image3 = null; try { image3 = new Image( getClass().getResource("image3.jpg").toExternalForm()); if (image3.isError()) new RuntimeException(); } catch (Exception e) { System.out.println("Не удалось загрузить изображение3"); return; } Image image4 = null; try { image4 = new Image( getClass().getResource("image4.jpg").toExternalForm(), 500, 100, false, false); /*image4 = new Image(getClass().getResource("/img/image4.jpg").toExternalForm(), 400, 500, true, true);*/ if (image4.isError()) new RuntimeException(); System.out.println(image4.getWidth() + " " + image4.getHeight()); } catch (Exception e) { System.out.println("Не удалось загрузить изображение4"); return; } Image image5 = new Image("http://www.mmcs.sfedu.ru/images/stories/main_img.jpg", true); //image5.cancel(); System.out.println(image5.getUrl()); image5.progressProperty().addListener((obj, oldValue, newValue) -> { System.out.println("Процент загрузки " + newValue); if (image5.getProgress() == 1.0) { System.out.print("Загрузка завершена "); if (!image5.isError()) { System.out.println("удачно"); } else { System.out.println("неудачно"); } } }); image5.exceptionProperty().addListener((obj, oldValue, newValue) -> { System.out.println("Ошибка при загрузке " + image5.getException()); }); Stage window = new Stage(); ScrollPane sp = new ScrollPane(); VBox pane = new VBox(15.0); ImageView iv = new ImageView(image); ImageView iv2 = new ImageView(image2); ImageView iv3 = new ImageView(image3); ImageView iv4 = new ImageView(image4); ImageView iv5 = new ImageView(image5); PixelReader pr = image.getPixelReader(); System.out.println(pr.getColor(0, 0)); pane.getChildren().addAll(iv, iv2, iv3, iv4, iv5); sp.setContent(pane); Scene scene = new Scene(sp, 600, 700); window.setScene(scene); window.setTitle("Image"); window.show(); } public void newWindow2() { Image image = null; try { image = new Image( getClass().getResourceAsStream("image1.jpg")); if (image.isError()) new RuntimeException(); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); return; } Stage window = new Stage(); ScrollPane sp = new ScrollPane(); VBox pane = new VBox(15.0); ImageView iv = new ImageView(); iv.setImage(image); System.out.println(iv.getImage().getWidth()); ImageView iv2 = new ImageView(image); iv2.setFitWidth(200.0); iv2.setFitHeight(200.0); iv2.setPreserveRatio(true); iv2.setSmooth(true); iv2.setCache(true); System.out.println(iv2.getFitWidth()); System.out.println(iv2.getFitHeight()); System.out.println(iv2.isPreserveRatio()); System.out.println(iv2.isSmooth()); System.out.println(iv2.isCache()); ImageView iv3 = new ImageView( getClass().getResource("image1.jpg").toExternalForm()); iv3.setRotate(90.0); pane.getChildren().addAll(iv, iv2, iv3); sp.setContent(pane); Scene scene = new Scene(sp, 600, 400); window.setScene(scene); window.setTitle("ImageView"); window.show(); } public void newWindow3() { ImageView iv = new ImageView(); try { iv.setImage(new Image( getClass().getResourceAsStream("image1.jpg"))); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); return; } Stage window = new Stage(); ScrollPane sp = new ScrollPane(); VBox pane = new VBox(15.0); sp.addEventHandler(DragEvent.DRAG_OVER, event -> { Dragboard db = event.getDragboard(); if (db.hasUrl()) { String url = db.getUrl(); if (url.indexOf("\n") == -1) { url = url.toLowerCase(); if (url.endsWith(".jpg") || url.endsWith(".jpeg")) { event.acceptTransferModes(TransferMode.COPY); } } } event.consume(); }); sp.addEventHandler(DragEvent.DRAG_DROPPED, event -> { Dragboard db = event.getDragboard(); boolean success = false; if (db.hasUrl()) { String url = db.getUrl(); if (url.indexOf("\n") == -1) { String url2 = url.toLowerCase(); if (url2.endsWith(".jpg") || url2.endsWith(".jpeg")) { success = true; try { Image im = new Image(url); iv.setImage(im); double w = sp.getWidth() - 5.0; double h = sp.getHeight() - 5.0; if (im.getWidth() < w) w = im.getWidth(); if (im.getHeight() < h) h = im.getHeight(); iv.setFitWidth(w); iv.setFitHeight(h); iv.setPreserveRatio(true); iv.setSmooth(true); iv.setCache(true); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); } } } } event.setDropCompleted(success); event.consume(); }); pane.getChildren().addAll(iv); sp.setContent(pane); Scene scene = new Scene(sp, 600, 400); window.setScene(scene); window.setTitle("ImageView. Перетащите изображение JPEG в окно"); window.show(); } public void newWindow4() { Image im = null; try { im = new Image( getClass().getResourceAsStream("image1.jpg")); if (im.isError()) new RuntimeException(); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); return; } Stage window = new Stage(); ScrollPane sp = new ScrollPane(); Pane pane = new Pane(); ImageView iv = new ImageView(); iv.setImage(im); iv.setX(20.0); iv.setY(20.0); iv.setEffect(new DropShadow()); System.out.println(iv.getX()); System.out.println(iv.getY()); ImageView iv2 = new ImageView(); iv2.setImage(im); iv2.setX(20.0); iv2.setY(500.0); iv2.setViewport(new Rectangle2D(200.0, 100.0, 300.0, 200.0)); iv2.setEffect(new DropShadow()); System.out.println(iv2.getViewport()); ImageView iv3 = new ImageView(); iv3.setImage(im); iv3.setX(20.0); iv3.setY(750.0); iv3.setClip(new Circle(450.0, 950.0, 100.0)); pane.getChildren().addAll(iv, iv2, iv3); sp.setContent(pane); Scene scene = new Scene(sp, 700, 500); window.setScene(scene); window.setTitle("ImageView Viewport"); window.show(); } public void newWindow5() { Image im = null; try { im = new Image(getClass().getResourceAsStream("image1.jpg")); if (im.isError()) new RuntimeException(); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); return; } Stage window = new Stage(); ScrollPane sp = new ScrollPane(); Pane pane = new Pane(); ImageView iv = new ImageView(im); iv.setX(20.0); iv.setY(20.0); iv.setEffect(new DropShadow()); PixelReader pr = im.getPixelReader(); Color color = pr.getColor(10, 10); System.out.println(color.getRed() + " " + color.getGreen() + " " + color.getBlue() + " " + color.getOpacity()); System.out.println((int)(color.getRed() * 255) + " " + (int)(color.getGreen() * 255) + " " + (int)(color.getBlue() * 255) + " " + (int)(color.getOpacity() * 255)); System.out.println(color.toString()); // 0xa48c72ff System.out.println(pr.getArgb(10, 10)); // -5993358 int argb = pr.getArgb(10, 10); int alpha = ((argb >> 24) & 0xff); int red = ((argb >> 16) & 0xff); int green = ((argb >> 8) & 0xff); int blue = ((argb ) & 0xff); System.out.println(red + " " + green + " " + blue + " " + alpha); System.out.println(pr.getPixelFormat().getType()); System.out.println(pr.getPixelFormat().isWritable()); System.out.println(pr.getPixelFormat().isPremultiplied()); int[] buffer = new int[100]; pr.getPixels(0, 0, 10, 10, WritablePixelFormat.getIntArgbInstance(), buffer, 0, 10); System.out.println(Arrays.toString(buffer)); byte[] buffer2 = new byte[400]; pr.getPixels(0, 0, 10, 10, WritablePixelFormat.getByteBgraInstance(), buffer2, 0, 10 * 4); System.out.println(Arrays.toString(buffer2)); IntBuffer buffer3 = IntBuffer.allocate(100); pr.getPixels(0, 0, 10, 10, WritablePixelFormat.getIntArgbInstance(), buffer3, 10); System.out.println(Arrays.toString(buffer3.array())); pane.getChildren().addAll(iv); sp.setContent(pane); Scene scene = new Scene(sp, 700, 500); window.setScene(scene); window.setTitle("PixelReader"); window.show(); } public void newWindow6() { Image im = null; try { im = new Image(getClass().getResourceAsStream("image1.jpg")); if (im.isError()) new RuntimeException(); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); return; } Stage window = new Stage(); ScrollPane sp = new ScrollPane(); VBox pane = new VBox(15.0); pane.setPadding(new Insets(15.0)); WritableImage wim = new WritableImage((int)im.getWidth(), (int)im.getHeight()); System.out.println(wim.getWidth() + " " + wim.getHeight()); System.out.println(wim.getPixelReader().getPixelFormat().getType()); PixelReader pr = wim.getPixelReader(); System.out.println(pr.getColor(0, 0)); // 0x00000000 PixelWriter pw = wim.getPixelWriter(); for (int x = 0, w = (int)wim.getWidth(); x < w; x++) { for (int y = 0, h = (int)wim.getHeight(); y < h; y++) { pw.setColor(x, y, Color.CHOCOLATE); } } ImageView iv = new ImageView(wim); WritableImage wim2 = new WritableImage(im.getPixelReader(), (int)im.getWidth(), (int)im.getHeight()); ImageView iv2 = new ImageView(wim2); WritableImage wim3 = new WritableImage(im.getPixelReader(), 200, 100, 300, 200); ImageView iv3 = new ImageView(wim3); pane.getChildren().addAll(iv, iv2, iv3); sp.setContent(pane); Scene scene = new Scene(sp, 700, 600); window.setScene(scene); window.setTitle("WritableImage"); window.show(); } public void newWindow7() { Image im = null; try { im = new Image(getClass().getResourceAsStream("image1.jpg")); if (im.isError()) new RuntimeException(); } catch (Exception e) { System.out.println("Не удалось загрузить изображение"); return; } Stage window = new Stage(); ScrollPane sp = new ScrollPane(); VBox pane = new VBox(15.0); pane.setPadding(new Insets(15.0)); WritableImage wim = new WritableImage(300, 200); PixelReader pread = wim.getPixelReader(); System.out.println(pread.getColor(0, 0)); // 0x00000000 System.out.println(pread.getPixelFormat().getType()); PixelWriter pw = wim.getPixelWriter(); System.out.println(pw.getPixelFormat().getType()); pw.setColor(0, 0, Color.CHOCOLATE); pw.setArgb(10, 10, -6452363); int argb = -6452363; int alpha = ((argb >> 24) & 0xff); int red = ((argb >> 16) & 0xff); int green = ((argb >> 8) & 0xff); int blue = ((argb ) & 0xff); System.out.println(red + " " + green + " " + blue + " " + alpha); // 157 139 117 255 Color color = Color.rgb(red, green, blue, alpha / 255.0); System.out.println( (int)(color.getRed() * 255) + " " + (int)(color.getGreen() * 255) + " " + (int)(color.getBlue() * 255) + " " + (int)(color.getOpacity() * 255)); ImageView iv = new ImageView(wim); WritableImage wim2 = new WritableImage(500, 400); PixelWriter pw2 = wim2.getPixelWriter(); System.out.println(pw2.getPixelFormat().getType()); PixelReader pr = im.getPixelReader(); int[] buffer = new int[100]; pr.getPixels(0, 0, 10, 10, WritablePixelFormat.getIntArgbInstance(), buffer, 0, 10); pw2.setPixels(0, 0, 10, 10, WritablePixelFormat.getIntArgbInstance(), buffer, 0, 10); byte[] buffer2 = new byte[400]; pr.getPixels(0, 0, 10, 10, WritablePixelFormat.getByteBgraInstance(), buffer2, 0, 10 * 4); pw2.setPixels(50, 50, 10, 10, WritablePixelFormat.getByteBgraInstance(), buffer2, 0, 10 * 4); IntBuffer buffer3 = IntBuffer.allocate(100); pr.getPixels(0, 0, 10, 10, WritablePixelFormat.getIntArgbInstance(), buffer3, 10); pw2.setPixels(100, 100, 10, 10, WritablePixelFormat.getIntArgbInstance(), buffer3, 10); ImageView iv2 = new ImageView(wim2); WritableImage wim3 = new WritableImage((int)im.getWidth(), (int)im.getHeight()); PixelWriter pw3 = wim3.getPixelWriter(); pw3.setPixels(0, 0, (int)im.getWidth(), (int)im.getHeight(), im.getPixelReader(), 0, 0); ImageView iv3 = new ImageView(wim3); pane.getChildren().addAll(iv, iv2, iv3); sp.setContent(pane); Scene scene = new Scene(sp, 700, 600); window.setScene(scene); window.setTitle("PixelWriter"); window.show(); } }