• 基于javafx实现一个简单的图片查看器


    功能分析

    1、查看图片

    2、图片旋转

    3、拖拽查看图片

    4、放大图片

    5、缩小图片

    代码实现

    package view;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    /**
     * @author 索半斤
     */
    public class ViewPort extends Application {
        private String myPicPath=null;
        public ViewPort(String myPicPath) {
            this.myPicPath=myPicPath;
        }
    
        public static void main(String[] args) {
            Platform.runLater(() -> {
                try {
                    ViewPort v=new ViewPort("file:cache/30ada9469b40c6c9b80e9e63a90352dd.jpg");
                    v.start(new Stage());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        private int flag=90;
        private int reduceNum=10;
        private int addNum=10;
        @Override
        public void start(Stage stage) throws Exception {
            stage.setHeight(700);
            stage.setWidth(800);
            Image image=new Image("file:src/images/seePic.jpg");
            stage.getIcons().add(image);
            stage.setTitle("图片查看");
            VBox gp=new VBox();
            StackPane ap=new StackPane();
            ScrollPane sp=new ScrollPane();
            sp.setPrefHeight(570);
            sp.setPrefWidth(800);
            ap.setPrefHeight(570);
            ap.setPrefWidth(800);
            sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); //从不显示垂直ScrollBar
            sp.setPannable(true);
            sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
            sp.setContent(ap);
            //
            Label label=new Label();
            ap.setAlignment(label, Pos.CENTER);
            Image seeImage=new Image(myPicPath);
            ImageView iv26=new ImageView(seeImage);
            if (seeImage.getHeight()>570){
                iv26.setFitHeight(570);
            }
            if (seeImage.getWidth()>800){
                iv26.setFitWidth(800);
            }
            label.setGraphic(iv26);
            ap.getChildren().add(label);
            //设置下面的部分
            HBox hbox=new HBox();
            Button add=new Button();
    
            add.setPrefWidth(40);
            Image image1=new Image("file:src/images/增大1.png");
            ImageView iv1=new ImageView(image1);
            iv1.setFitHeight(image1.getHeight()/1.3);
            iv1.setFitWidth(image1.getWidth()/1.3);
            add.setGraphic(iv1);
            add.setStyle("-fx-background-color: rgba(255,255,255,0.1)");
            add.setOnMouseClicked(mouseEvent -> {
                iv26.setFitHeight(iv26.getFitHeight()+addNum);
                iv26.setFitWidth(iv26.getFitWidth()+addNum);
                label.setGraphic(iv26);
                ap.setAlignment(label, Pos.CENTER);
            });
            add.setOnMouseEntered(mouseEvent -> add.setStyle("-fx-background-color: rgba(190,190,190,0.4)"));
            add.setOnMouseExited(mouseEvent -> add.setStyle("-fx-background-color: rgba(255,255,255,0)"));
            Button reduce=new Button();
            reduce.setPrefWidth(40);
            Image image2=new Image("file:src/images/减小1.png");
            ImageView iv2=new ImageView(image2);
            iv2.setFitHeight(image2.getHeight()/1.3);
            iv2.setFitWidth(image2.getWidth()/1.3);
            reduce.setGraphic(iv2);
            reduce.setStyle("-fx-background-color: rgba(255,255,255,0)");
    
            reduce.setOnMouseClicked(mouseEvent -> {
               iv26.setFitHeight(iv26.getFitHeight()-reduceNum);
               iv26.setFitWidth(iv26.getFitWidth()-reduceNum);
               label.setGraphic(iv26);
               ap.setAlignment(label, Pos.CENTER);
            });
            reduce.setOnMouseEntered(mouseEvent -> reduce.setStyle("-fx-background-color: rgba(190,190,190,0.4)"));
            reduce.setOnMouseExited(mouseEvent -> reduce.setStyle("-fx-background-color: rgba(255,255,255,0)"));
            Button radio=new Button();
            radio.setPrefWidth(40);
            Image image3=new Image("file:src/images/旋转1.png");
            ImageView iv3=new ImageView(image3);
            iv3.setFitHeight(image3.getHeight()/1.3);
            iv3.setFitWidth(image3.getWidth()/1.3);
            radio.setGraphic(iv3);
            radio.setStyle("-fx-background-color: rgba(255,255,255,0)");
            radio.setOnMouseClicked(mouseEvent -> {
                radio.setStyle("-fx-background-color: rgba(190,190,190,0.4)");
                if (label.getHeight()>570){
                    label.setPrefHeight(570);
                }
                label.setRotate(flag);
                flag+=90;
            });
            radio.setOnMouseEntered(mouseEvent -> radio.setStyle("-fx-background-color: rgba(190,190,190,0.4)"));
            radio.setOnMouseExited(mouseEvent -> radio.setStyle("-fx-background-color: rgba(255,255,255,0)"));
            hbox.setSpacing(40);
            hbox.getChildren().addAll(add,radio,reduce);
            hbox.setPadding(new Insets(30,0,0,280));
            gp.getChildren().addAll(sp,hbox);
            gp.setStyle("-fx-background-color: white");
            Scene scene=new Scene(gp);
            stage.setScene(scene);
            stage.show();
        }
    }
    

    效果展示

    在这里插入图片描述

  • 相关阅读:
    poj 2778 AC自己主动机 + 矩阵高速幂
    Web Services 指南之:Web Services 综述
    SQL多表连接查询(具体实例)
    HibernateUtil
    哈夫曼编码问题再续(下篇)——优先队列求解
    MySQL Merge存储引擎
    程序的入口及AppDelegate窗体显示原理
    几个免费的DNS地址
    kettle与各数据库建立链接的链接字符串
    【转】利用optimize、存储过程和系统表对mysql数据库表进行批量碎片清理释放表空间
  • 原文地址:https://www.cnblogs.com/zwscode/p/14284054.html
Copyright © 2020-2023  润新知