利用javafx实现绘图板

实现功能包括绘制线条(直线、曲线)和几何图形,选中图形并反色,填充颜色,移动(线条移动有些不灵敏),计算长度面积、保存成可编辑文件、清空画板。
以前自己摸索写的,有很多不足,仅供自己使用

项目结构:

实现效果:

  1. 绘图
  2. 选中反色
  3. 填充颜色
  4. 计算面积
  5. 保存为可编辑文件

    其他图就不放了。

源码:

package config;public enum Action {SELECT, RECTANGULAR,MOVE,FILL,CIRCULAR,ELLIPSE,LINE,POLYLINE
}
package config;import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import shape.*;import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class SaverLoader {ArrayList<String> arrayList;AnchorPane board;public SaverLoader() {}public ArrayList<String> getArrayList() {return arrayList;}public void setArrayList(ArrayList<String> arrayList) {this.arrayList = arrayList;}public AnchorPane getBoard() {return board;}public void setBoard(AnchorPane board) {this.board = board;}public void save() throws IOException {String time = String.valueOf(System.currentTimeMillis());DirectoryChooser directoryChooser = new DirectoryChooser();String path = String.valueOf(directoryChooser.showDialog(new Stage()).getAbsoluteFile());File file = new File(path + '\\' + time + ".edit");ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));oos.writeObject(arrayList);}private void loadItem(String s) {String[] result = s.split(",");switch (result[0]) {case "MyPolyline":MyPolyline myPolyline = new MyPolyline(result[1], result[2], result[3], result[4], result[5], result[6], result[7]);board.getChildren().add(myPolyline);break;case "MyEllipse":MyEllipse myEllipse = new MyEllipse(result[1], result[2], result[3], result[4], result[5], result[6], result[7]);board.getChildren().add(myEllipse);break;case "MyCircular":MyCircular myCircular = new MyCircular(result[1], result[2], result[3], result[4], result[5], result[6]);board.getChildren().add(myCircular);break;case "MyRectangle":MyRectangle myRectangle = new MyRectangle(result[1], result[2], result[3], result[4], result[5], result[6], result[7]);board.getChildren().add(myRectangle);break;case "MyPath":ArrayList<Double> point = new ArrayList<>();for (int i = 5; i != result.length; ++i) point.add(Double.parseDouble(result[i]));MyPath myPath = new MyPath(result[1], result[2], result[3], result[4], point);board.getChildren().add(myPath);break;default:break;}}public void load() throws IOException, ClassNotFoundException {FileChooser fileChooser = new FileChooser();fileChooser.setTitle("Open Resource File");File file = fileChooser.showOpenDialog(new Stage());ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));ArrayList<String> p = ( ArrayList<String>) ois.readObject();board.getChildren().clear();for(String item:p){loadItem(item);}}public static void main(String[] args) throws ClassNotFoundException {}
}
package config;import javafx.scene.paint.Color;public class Setting {//填充Color fill;//边框Color stroke;//字体public Color getFill() {return fill;}public void setFill(Color fill) {this.fill = fill;}public Color getStroke() {return stroke;}public void setStroke(Color stroke) {this.stroke = stroke;}
}
package sample;import config.Action;
import config.SaverLoader;
import config.Setting;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import shape.*;import java.io.*;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.ResourceBundle;public class Controller implements Initializable {public static Setting setting = new Setting();public static Action action = Action.SELECT;//鼠标点击坐标double p_x;double p_y;//鼠标抬起坐标double r_x;double r_y;//小数点坐标DecimalFormat df = new DecimalFormat("0.00");@FXMLprivate javafx.scene.layout.AnchorPane AnchorPane;//画布@FXMLprivate AnchorPane board;//图形@FXMLprivate Button RectangularButton;@FXMLprivate Button polyline;@FXMLprivate Button circular;@FXMLprivate Button ellipse;@FXMLprivate Button line;//控件@FXMLprivate javafx.scene.control.MenuBar MenuBar;@FXMLprivate Label coordinate;@FXMLprivate ColorPicker fillcolor;@FXMLprivate ColorPicker strokecolor;//工具@FXMLprivate Button fill;@FXMLprivate Button SelectButton;@FXMLprivate Button Move;MyPath path;//保存加载器SaverLoader saverLoader = new SaverLoader();@FXMLvoid RectangularButton(MouseEvent event) {action = Action.RECTANGULAR;}@FXMLvoid SelectButton(MouseEvent event) {action = Action.SELECT;}@FXMLvoid Move(MouseEvent event) {action = Action.MOVE;}@FXMLvoid fill(MouseEvent event) {action = Action.FILL;}@FXMLvoid circular(MouseEvent event) {action = Action.CIRCULAR;}@FXMLvoid ellipse(MouseEvent event) {action = Action.ELLIPSE;}@FXMLvoid line(MouseEvent event) {action = Action.LINE;}@FXMLvoid polyline(MouseEvent event) {action = Action.POLYLINE;}@Overridepublic void initialize(URL url, ResourceBundle resourceBundle) {//菜单初始化{MenuBar.getMenus().get(0).getItems().get(0).setOnAction(new EventHandler<ActionEvent>() {public void handle(ActionEvent event) {System.exit(0);}});MenuBar.getMenus().get(0).getItems().get(1).setOnAction(new EventHandler<ActionEvent>() {public void handle(ActionEvent event) {ArrayList<String> arrayList = new ArrayList<>();for (Node i : board.getChildren()) arrayList.add(i.toString());saverLoader.setBoard(board);saverLoader.setArrayList(arrayList);try {saverLoader.save();} catch (IOException e) {e.printStackTrace();}}});MenuBar.getMenus().get(0).getItems().get(2).setOnAction(new EventHandler<ActionEvent>() {public void handle(ActionEvent event) {saverLoader.setBoard(board);try {saverLoader.load();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}});MenuBar.getMenus().get(1).getItems().get(0).setOnAction(new EventHandler<ActionEvent>() {public void handle(ActionEvent event) {board.getChildren().clear();}});}//初始化设置{setting.setStroke(Color.BLACK);setting.setFill(Color.web("#00000000"));}//填充颜色监听事件{fillcolor.setOnAction((event) -> {setting.setFill(fillcolor.getValue());});}//边框颜色监听事件{strokecolor.setOnAction((event) -> {setting.setStroke(strokecolor.getValue());});}board.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {p_x = event.getX();p_y = event.getY();if (action == Action.LINE) {path = new MyPath(setting.getStroke());board.getChildren().add(path);path.getElements().add(new MoveTo(p_x, p_y));path.recordPoint(p_x, p_y);}});board.addEventHandler(MouseEvent.MOUSE_RELEASED, event -> {r_x = event.getX();r_y = event.getY();switch (action) {case RECTANGULAR:MyRectangle myRectangle = new MyRectangle(p_x, p_y, r_x - p_x, r_y - p_y, setting.getFill(), setting.getStroke());board.getChildren().add(myRectangle);break;case CIRCULAR:double r = Math.pow(Math.pow(r_x - p_x, 2) + Math.pow(r_y - p_y, 2), 0.5);MyCircular myCircular = new MyCircular(p_x, p_y, r / 2, setting.getFill(), setting.getStroke());board.getChildren().add(myCircular);break;case POLYLINE:MyPolyline myPolyline = new MyPolyline(setting.getStroke());myPolyline.getPoints().addAll(p_x, p_y,r_x, r_y);board.getChildren().add(myPolyline);
//                    Polyline polyline = new Polyline();
//                    polyline.setStroke(setting.getStroke());
//                    polyline.getPoints().addAll(p_x, p_y,
//                            r_x, r_y);
//                    board.getChildren().add(polyline);break;case ELLIPSE:double a = Math.abs(r_x - p_x) / 2;double b = Math.abs(r_y - p_y) / 2;MyEllipse myEllipse = new MyEllipse(p_x, p_y, a, b, setting.getFill(), setting.getStroke());board.getChildren().add(myEllipse);break;default:break;}});board.addEventHandler(MouseEvent.MOUSE_MOVED, event -> {coordinate.setText(String.format("x:%s px,y:%s px", df.format(event.getX()), df.format(event.getY())));});board.addEventHandler(MouseDragEvent.MOUSE_DRAGGED, event -> {if (action == Action.LINE) {//                gc = Canvas.getGraphicsContext2D();
//                gc.setStroke(setting.getStroke());
//                gc.moveTo(p_x, p_y);
//                gc.lineTo(event.getX(), event.getY());
//                gc.stroke();
//                p_x = event.getX();
//                p_y = event.getY();path.setStroke(setting.getStroke());path.getElements().add(new LineTo(event.getX(), event.getY()));path.recordPoint(event.getX(), event.getY());}});}public static void main(String[] args) throws IOException, ClassNotFoundException {}
}
package sample;import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;public class Main extends Application {@Overridepublic void start(Stage primaryStage) throws Exception{Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));primaryStage.setTitle("绘图板");primaryStage.getIcons().add(new Image("icon/logo.png"));primaryStage.setScene(new Scene(root));primaryStage.show();}public static void main(String[] args) {launch(args);}
}

sample.fxml

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.Button?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.paint.Color?><GridPane alignment="center" hgap="10" stylesheets="@win7glass.css" vgap="10" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"><columnConstraints><ColumnConstraints /></columnConstraints><rowConstraints><RowConstraints /></rowConstraints><children><AnchorPane fx:id="AnchorPane" prefHeight="500.0" prefWidth="600.0"><children><MenuBar fx:id="MenuBar" layoutY="2.0" prefHeight="14.0" prefWidth="600.0"><menus><Menu mnemonicParsing="false" text="文件"><items><MenuItem mnemonicParsing="false" text="关闭" /></items><items><MenuItem mnemonicParsing="false" text="保存" /></items><items><MenuItem mnemonicParsing="false" text="打开" /></items></Menu><Menu mnemonicParsing="false" text="编辑"><items><MenuItem mnemonicParsing="false" text="清空" /></items></Menu><Menu mnemonicParsing="false" text="帮助"><items><MenuItem mnemonicParsing="false" text="关于" /></items></Menu></menus></MenuBar><Button fx:id="RectangularButton" layoutY="28.0" mnemonicParsing="false" onMouseClicked="#RectangularButton" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/rectangleZ.png')" text="" /><Button fx:id="SelectButton" layoutY="403.0" mnemonicParsing="false" onMouseClicked="#SelectButton" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/select.png')" text="" /><AnchorPane fx:id="board" layoutX="100.0" layoutY="28.0" prefHeight="474.0" prefWidth="500.0" /><Label fx:id="coordinate" layoutX="399.0" layoutY="501.0" maxWidth="200.0" prefHeight="26.0" prefWidth="200.0" /><Button fx:id="Move" layoutX="-1.0" layoutY="453.0" mnemonicParsing="false" onMouseClicked="#Move" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/shapeCursor.png')" text="" /><ColorPicker fx:id="fillcolor" layoutY="324.0"><value><Color red="1.0" green="1.0" blue="1.0" opacity="0.0" /></value></ColorPicker><ColorPicker fx:id="strokecolor" layoutX="1.0" layoutY="380.0"><value><Color /></value></ColorPicker><Label alignment="CENTER" contentDisplay="CENTER" layoutX="-1.0" layoutY="290.0" prefHeight="33.0" prefWidth="100.0" text="填充颜色" textAlignment="CENTER" /><Label alignment="CENTER" layoutX="-1.0" layoutY="349.0" prefHeight="33.0" prefWidth="100.0" text="边框颜色" /><Button fx:id="fill" layoutX="50.0" layoutY="403.0" mnemonicParsing="false" onMouseClicked="#fill" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/barrel.png')" text="" /><Button fx:id="polyline" layoutY="128.0" mnemonicParsing="false" onMouseClicked="#polyline" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/line.png')" text="" /><Button fx:id="circular" layoutX="50.0" layoutY="28.0" mnemonicParsing="false" onMouseClicked="#circular" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/circular.png')" text="" /><Button fx:id="ellipse" layoutY="78.0" mnemonicParsing="false" onMouseClicked="#ellipse" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/oval.png')" text="" /><Button fx:id="line" layoutX="50.0" layoutY="78.0" mnemonicParsing="false" onMouseClicked="#line" prefHeight="50.0" prefWidth="50.0" style="-fx-background-image:url('/icon/pen.png')" text="" /></children></AnchorPane></children>
</GridPane>
package shape;import config.Action;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import sample.Controller;import java.io.Serializable;public class MyCircular extends Circle implements Serializable {double x;double y;private double fromX, fromY, lastTranslateX, lastTranslateY;Color fill;public MyCircular(double x, double y, double r, Color fill, Color stroke) {super(x, y, r);this.fill = fill;this.setFill(fill);this.setStroke(stroke);addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {if (Controller.action == Action.MOVE) {fromX = event.getSceneX();fromY = event.getSceneY();lastTranslateX = getTranslateX();lastTranslateY = getTranslateY();}if (Controller.action == Action.SELECT) {setFill(this.fill.invert());}if (Controller.action == Action.FILL) {setFill(Controller.setting.getFill());this.fill = (Color) getFill();}});addEventHandler(MouseDragEvent.MOUSE_DRAGGED, event -> {if (Controller.action == Action.MOVE) {double deltaX = event.getSceneX() - fromX;double deltaY = event.getSceneY() - fromY;setTranslateX(deltaX + lastTranslateX);setTranslateY(deltaY + lastTranslateY);}});addEventHandler(MouseEvent.MOUSE_RELEASED, event -> {if (Controller.action == Action.SELECT) {setFill(this.fill);}});addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {if (event.getButton().name().equals("SECONDARY") && Controller.action == Action.SELECT) {Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setTitle("面积");alert.setContentText("面积");alert.setHeaderText(String.valueOf(Math.PI*Math.pow(getRadius(),2))+"px^2");alert.showAndWait();}});}public MyCircular(String a, String b, String c, String d, String e, String f){this(Double.parseDouble(a),Double.parseDouble(b),Double.parseDouble(c),Color.web(d),Color.web(e));}@Overridepublic String toString() {return "MyCircular,"+getCenterX()+","+getCenterY()+","+getRadius()+","+getFill()+","+getStroke()+","+getStrokeWidth();}
}
package shape;import config.Action;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import sample.Controller;public class MyEllipse extends Ellipse {private double fromX, fromY, lastTranslateX, lastTranslateY;Color fill;public MyEllipse(double x, double y, double a, double b, Color fill, Color stroke) {super(x, y, a, b);this.fill = fill;this.setFill(fill);this.setStroke(stroke);addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {if (Controller.action == Action.MOVE) {fromX = event.getSceneX();fromY = event.getSceneY();lastTranslateX = getTranslateX();lastTranslateY = getTranslateY();}if (Controller.action == Action.SELECT) {setFill(this.fill.invert());}if (Controller.action == Action.FILL) {setFill(Controller.setting.getFill());this.fill = (Color) getFill();}});addEventHandler(MouseDragEvent.MOUSE_DRAGGED, event -> {if (Controller.action == Action.MOVE) {double deltaX = event.getSceneX() - fromX;double deltaY = event.getSceneY() - fromY;setTranslateX(deltaX + lastTranslateX);setTranslateY(deltaY + lastTranslateY);}});addEventHandler(MouseEvent.MOUSE_RELEASED, event -> {if (Controller.action == Action.SELECT) {setFill(this.fill);}});addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {if (event.getButton().name().equals("SECONDARY") && Controller.action == Action.SELECT) {Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setTitle("面积");alert.setContentText("面积");alert.setHeaderText(String.valueOf(Math.PI * getRadiusX() * getRadiusY()) + "px^2");alert.showAndWait();}});}public MyEllipse(String a, String b, String c, String d, String e, String f, String g) {this(Double.parseDouble(a),Double.parseDouble(b),Double.parseDouble(c),Double.parseDouble(d),Color.web(e),Color.web(f));}@Overridepublic String toString() {return "MyEllipse," + getCenterX() + "," + getCenterY() + "," + getRadiusX() + "," + getRadiusY() + "," + getFill() + "," + getStroke() + "," + getStrokeWidth();}
}
package shape;import config.Action;
import javafx.collections.ObservableList;
import javafx.scene.effect.Light;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import sample.Controller;import java.util.ArrayList;public class MyPath extends Path {private double fromX, fromY, lastTranslateX, lastTranslateY;ArrayList<Double> point = new ArrayList<>();public void recordPoint(double a, double b) {point.add(a);point.add(b);}public MyPath(Color stroke) {super();this.setStroke(stroke);addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {if (Controller.action == Action.MOVE) {fromX = event.getSceneX();fromY = event.getSceneY();lastTranslateX = getTranslateX();lastTranslateY = getTranslateY();}});addEventHandler(MouseDragEvent.MOUSE_DRAGGED, event -> {if (Controller.action == Action.MOVE) {double deltaX = event.getSceneX() - fromX;double deltaY = event.getSceneY() - fromY;setTranslateX(deltaX + lastTranslateX);setTranslateY(deltaY + lastTranslateY);}});}public MyPath(String a, String b, String c, String d, ArrayList<Double> points) {this(Color.web(c));this.getElements().add(new MoveTo(points.get(0), points.get(1)));for (int i = 2; i != points.size(); i += 2) {this.getElements().add(new LineTo(points.get(i), points.get(i + 1)));}}@Overridepublic String toString() {String s = "MyPath," + getFill() + "," + getFillRule() + "," + getStroke() + "," + getStrokeWidth();for (Double p : point) {s += "," + p;}return s;}
}
package shape;import config.Action;
import javafx.collections.ObservableList;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polyline;
import sample.Controller;import java.io.PrintWriter;public class MyPolyline extends Polyline {private double fromX, fromY, lastTranslateX, lastTranslateY;public MyPolyline(Color stroke) {super();this.setStroke(stroke);addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {if (Controller.action == Action.MOVE) {fromX = event.getSceneX();fromY = event.getSceneY();lastTranslateX = getTranslateX();lastTranslateY = getTranslateY();}});addEventHandler(MouseDragEvent.MOUSE_DRAGGED, event -> {if (Controller.action == Action.MOVE) {double deltaX = event.getSceneX() - fromX;double deltaY = event.getSceneY() - fromY;setTranslateX(deltaX + lastTranslateX);setTranslateY(deltaY + lastTranslateY);}});addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {if (event.getButton().name().equals("SECONDARY") && Controller.action == Action.SELECT) {Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setTitle("长度");alert.setContentText("长度");double ap = Math.pow(getPoints().get(0) - getPoints().get(2),2);double bp = Math.pow(getPoints().get(1) - getPoints().get(3),2);alert.setHeaderText(String.valueOf(Math.pow(ap+bp,0.5)+"px"));alert.showAndWait();}});}public MyPolyline(String a, String b, String c, String d, String e, String f, String g) {this(Color.web(f));getPoints().addAll(Double.parseDouble(a),Double.parseDouble(b),Double.parseDouble(c),Double.parseDouble(d));setStrokeWidth(Double.parseDouble(g));}@Overridepublic String toString() {return "MyPolyline," + getPoints().get(0) + "," + getPoints().get(1) + "," + getPoints().get(2) + "," + getPoints().get(3) + "," + getFill() + "," + getStroke() + "," + getStrokeWidth();}
}
package shape;import config.Action;
import config.Setting;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import sample.Controller;import java.net.URL;
import java.util.ResourceBundle;public class MyRectangle extends javafx.scene.shape.Rectangle {double x;double y;private double fromX, fromY, lastTranslateX, lastTranslateY;Color fill;public MyRectangle(double x, double y, double w, double h, Color fill, Color stroke) {super(x, y, w, h);this.fill = fill;this.setFill(fill);this.setStroke(stroke);addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {if (Controller.action == Action.MOVE) {fromX = event.getSceneX();fromY = event.getSceneY();lastTranslateX = getTranslateX();lastTranslateY = getTranslateY();}if (Controller.action == Action.SELECT) {setFill(this.fill.invert());}if(Controller.action == Action.FILL){setFill(Controller.setting.getFill());this.fill = (Color) getFill();}});addEventHandler(MouseDragEvent.MOUSE_DRAGGED, event -> {if (Controller.action == Action.MOVE) {double deltaX = event.getSceneX() - fromX;double deltaY = event.getSceneY() - fromY;setTranslateX(deltaX + lastTranslateX);setTranslateY(deltaY + lastTranslateY);}});addEventHandler(MouseEvent.MOUSE_RELEASED, event -> {if (Controller.action == Action.SELECT) {setFill(this.fill);}});addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {if (event.getButton().name().equals("SECONDARY") && Controller.action == Action.SELECT) {Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setTitle("面积");alert.setContentText("面积");alert.setHeaderText(String.valueOf(getWidth()*getHeight())+"px^2");alert.showAndWait();}});}public MyRectangle(String a, String b, String c, String d, String e, String f, String g){this(Double.parseDouble(a),Double.parseDouble(b),Double.parseDouble(c),Double.parseDouble(d),Color.web(e),Color.web(f));}@Overridepublic String toString() {return "MyRectangle,"+getX()+","+getY()+","+getWidth()+","+getHeight()+","+getFill()+","+getStroke()+","+getStrokeWidth();}
}

css样式比较丑,可以自己设计一下

javafx 实现绘图板相关推荐

  1. JDK11使用IDEA,配置JavaFX

    JDK11使用IDEA,配置JavaFX 1.下载javaFX相关的包 2.在实际Demo中试验哪里少了添加哪里 导入lib文件夹,之后点击OK 配置VMoption 配置成功 3.运行,大功告成 1 ...

  2. JavaFX项目jar使用javafxpackager生成exe

    2019独角兽企业重金招聘Python工程师标准>>> JavaFX项目jar使用javafxpackager生成exe 编译JavaFX生成可执行jar 新建文件夹test1,将第 ...

  3. 像素颜色JavaFX示例--简易图片处理工具

    文章结束给大家来个序程员笑话:[M] 声明:   本博客文章原创类别的均为个人原创,版权所有.载转请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http:/ ...

  4. javaFX中解决填充(拉伸)问题

    1.margin设置实现 在项目过程中,遇到此问题,如图: 如果窗口缩小,HBox(左边的包含TitledPane那部分)看不到底部 如果窗口拉大,下面就出现空白,HBox高度没拉神 办法:对包含HB ...

  5. JavaFX打包工具(javafxpackager)

    2019独角兽企业重金招聘Python工程师标准>>> 首先创建一个JavaFX文件,Hello World package test;import javafx.applicati ...

  6. javafx官方文档学习之二Scene体系学习一

    2019独角兽企业重金招聘Python工程师标准>>> 我的博文小站:http://www.xby1993.net,文章更新以博文小站为主,一般与oschina同步发布 原创文章,转 ...

  7. 【Android开发】范例2-实现简易绘图板

    下面这个实例通过前面学过的Paint.Canvas等2D绘画技术来实现一个简单的Android的绘图板. 具体实现代码: 创建一个名为DrawView的类,该类继承自android.view.View ...

  8. Silverlight、JavaFX、Flex技术比较

    Techie在他最近写的一篇博客中,从定义.大小.应用平台.授权许可等方面深入比较了这几种技术的不同. Silverlight是一个跨浏览器和跨平台的插件,能在微软的.NET上交付炫目的多媒体体验和有 ...

  9. java 属性自定义配置,将自定义FXML属性设置为自定义javafx组件的参数

    我创建了自定义组件TableBlock . 它由Label和TableView组成 . 例如,TableView可以有1到1000行 . 行数由FXML文件中的参数"rowsFromPref ...

最新文章

  1. phpMyAdmin安装图解教程
  2. 科普:不要对移动机器人有误解
  3. scipy/python quad()数值积分
  4. adb查看手机cpu使用率_记录一下Unity打包Android在骁龙cpu上概率性卡死的问题
  5. handlebars 基础
  6. HDU 3709 Balanced Number
  7. 端口映射的几种实现方法
  8. 7.定义一个有80个元素的字符数组,从键盘输入一串字符,将其中的大写字母转换为小写字母,而将原来为小写的字母转换为大写字母,其他字符不变。
  9. 470p 更换固态硬盘_联想G510换固态硬盘遇到的问题
  10. Oracle 以某字段分组,以某字段排序,取前几条
  11. python idle是什么_下载下来的IDLE是个什么鬼
  12. JFinal上传文件时用getFile()方法报错
  13. linux挂载目录已存在可以么,Linux如何更改硬盘已挂载目录
  14. Julia: 调用Python 库
  15. oracle新建定时任务,创建 Oracle 定时任务
  16. 快速注册认证小程序,三分钟学会免300元认证企业小程序
  17. Mac 不小心断开移动硬盘导致磁盘无法读取和加载(顺利解决!)
  18. OSPF-LSA详解
  19. Java 时间相关 获取某月的某一天
  20. 【大疆DJI】安卓开发实习历程- 0.前期准备到面试(HR电话初面+技术一面+技术二面/终面+OC)

热门文章

  1. MC9S12XS128硬件底层驱动_set_bus_clk.h(总线时钟设置)
  2. 学习没有动力的解决方法
  3. R语言-文本文件读写 txt / csv / xlsx
  4. 安卓逆向之双剑合璧实现内存扫描
  5. TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值
  6. 百度地图API 学习网站
  7. SSM校园好货APP的设计与实现毕业设计源码121619
  8. win10升级Java版本
  9. 计算机专业文科生录取分数线,文科生适合报考的5所学校,录取分数线不高,但就业前景很好...
  10. 2.3.1-4. IEEE 754 标准