javafx 实现绘图板
生活随笔
收集整理的這篇文章主要介紹了
javafx 实现绘图板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用javafx實現繪圖板
實現功能包括繪制線條(直線、曲線)和幾何圖形,選中圖形并反色,填充顏色,移動(線條移動有些不靈敏),計算長度面積、保存成可編輯文件、清空畫板。
以前自己摸索寫的,有很多不足,僅供自己使用
項目結構:
實現效果:
其他圖就不放了。
源碼:
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 实现绘图板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XML知识3--Schema
- 下一篇: Textstudio 应用程序无法正常启