在我的JavaFX Paint程序中添加一个超类和几个子类

时间:2018-11-17 16:42:23

标签: java javafx

我已经用JavaFX创建了一个绘画程序。您所看到的所有代码都在一个BIG文件中。因此,我想创建一个名为Figure的超类,以使形状具有所有相同的东西。然后,我想为每个Line,Circle,Rectangle和Polygon创建子类,也许在每个类中都使用eventhandler来创建绘图方法,这可能吗?

这是我的代码:

package tegneprogram;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;

//------------------------------------------------------------------------------
// CONTROLFX MUST BE ADDED IN THE PROJECT LIBRARY TO SEE THE EFFECT
//------------------------------------------------------------------------------

import org.controlsfx.control.PopOver;


//---------------------------------------------------------------------------------------
// DESCRIPTION:
//
// A PROGRAM THAT ALLOWS THE USER TO DRAW GEOMETRIC FIGURES WITH DIFFERENT COLORS.
// YOU CAN ALSO ADD TEXT TO THE PANE. WHEN FIGURES AND TEXT IS APPLIED YOU CAN MOUSE_PRESS
// AND MOUSE_DRAG THEM INDIVIDUALLY AND MOVE EACH ONE AROUND THE PANE.
//
//------------------------------------------------------------------------------




public class Tegneprogram extends Application {


//------------------------------------------------------------------------------
// START
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
// GEOMETRIC FIGURES
//------------------------------------------------------------------------------

     Rectangle rect; 
     Line line;
     Polygon poly;
     Circle circ;
     TextField text;

//------------------------------------------------------------------------------
// VARIABLES FOR DRAGGING FIGURES
//------------------------------------------------------------------------------

    double nwPosX = 0, nwPosY = 0;
    double TX = 0, TY = 0;

//------------------------------------------------------------------------------ 
// DRAWING PANE
//------------------------------------------------------------------------------

     Pane pane;

//------------------------------------------------------------------------------  
// COLORPICKER
//------------------------------------------------------------------------------  

     ColorPicker cpFill;
     ColorPicker cpLine;

//------------------------------------------------------------------------------
// POLYGON VARIABLES, ARRAYLIST AND DOUBLE ARRAY
//------------------------------------------------------------------------------

int count = 0;
double x[] = new double[3];
double y[] = new double[3];
List<Double> values = new ArrayList();


//------------------------------------------------------------------------------
// SPACE
//------------------------------------------------------------------------------


    @Override
    public void start(Stage primaryStage) {

//------------------------------------------------------------------------------
// TOGGLE BUTTON FOR FIGURES
//------------------------------------------------------------------------------

        ToggleButton linebtn = new ToggleButton("Line");
        ToggleButton rectbtn = new ToggleButton("Rectangel");
        ToggleButton circbtn = new ToggleButton("Circle");
        ToggleButton polybtn = new ToggleButton("Polygon");
        ToggleButton textbtn = new ToggleButton("Text");

//------------------------------------------------------------------------------
// LABELS FOR INFORMATION ABOUT COLOR
//------------------------------------------------------------------------------
        Label line_color = new Label("Line Color");
        Label fill_color = new Label("Fill Color");

 //-----------------------------------------------------------------------------
 // TABLE ARRAY FOR ALL THE TOGGLE BUTTONS
 //-----------------------------------------------------------------------------

        ToggleButton[] FigureTool = {linebtn, rectbtn, circbtn, polybtn, textbtn};

//------------------------------------------------------------------------------
// GROUP OBJECT FOR TOGGLEBUTTONS
//------------------------------------------------------------------------------

        ToggleGroup tools = new ToggleGroup();

//------------------------------------------------------------------------------ 
// COUNTING ALL THE BUTTONS IN TABLE ARRAY AND GROUPS THEM IN A TOGGLEGROUP
//------------------------------------------------------------------------------

        for(ToggleButton tool : FigureTool) {
            tool.setMinWidth(90);//width on buttons
            tool.setToggleGroup(tools);
            tool.setCursor(Cursor.HAND);

        }
//------------------------------------------------------------------------------
// COLORPICKER
//------------------------------------------------------------------------------
         cpLine = new ColorPicker(Color.BLACK);
         cpFill = new ColorPicker(Color.TRANSPARENT);         


//------------------------------------------------------------------------------
// HBOX FOR PLACING BUTTONS
//------------------------------------------------------------------------------

        HBox buttons = new HBox(10);
        buttons.getChildren().addAll(linebtn, rectbtn, circbtn, polybtn, textbtn);

//------------------------------------------------------------------------------
// STYLING FOR HBOX BUTTONS
//------------------------------------------------------------------------------

        buttons.setPadding(new Insets(5));
        buttons.setStyle("-fx-background: #777");
        buttons.setPrefWidth(90);


//------------------------------------------------------------------------------       
// VBOX FOR PLACING LINE AND COLOR BUTTONS
//------------------------------------------------------------------------------

        VBox colors = new VBox(10);
        colors.getChildren().addAll(line_color,cpLine,fill_color, cpFill);

//------------------------------------------------------------------------------  
// STYLING FOR VBOX COLORS BUTTONS 
//------------------------------------------------------------------------------

        colors.setPadding(new Insets(5));
        colors.setStyle("-fx-background: #777");


//------------------------------------------------------------------------------
// SETTING UP DRAWING PANE
//------------------------------------------------------------------------------

        pane = new Pane();
        pane.setStyle("-fx-background-color: white");


//------------------------------------------------------------------------------
// MOUSE_DRAGGED LINE
//------------------------------------------------------------------------------



    EventHandler<MouseEvent> lineMousePressed = (MouseEvent e) -> {
            nwPosX  = e.getSceneX();
            nwPosY  = e.getSceneY();
            TX  = ((Line)(e.getSource())).getTranslateX();
            TY  = ((Line)(e.getSource())).getTranslateY();


            ((Line)(e.getSource())).toFront();

        };

    EventHandler<MouseEvent> lineMoveDrag = (MouseEvent e) -> {

            double newSetX = e.getSceneX() - nwPosX;
            double newSetY = e.getSceneY() - nwPosY;
            double NSX = TX + newSetX;
            double NSY = TY + newSetY;


            ((Line)(e.getSource())).setTranslateX(NSX);
            ((Line)(e.getSource())).setTranslateY(NSY);
        };


//------------------------------------------------------------------------------
// MOUSE_DRAGGED RECTANGLE
//------------------------------------------------------------------------------



    EventHandler<MouseEvent> rectMousePressed = (MouseEvent e) -> {
            nwPosX  = e.getSceneX();
            nwPosY  = e.getSceneY();
            TX  = ((Rectangle)(e.getSource())).getTranslateX();
            TY  = ((Rectangle)(e.getSource())).getTranslateY();


           ((Rectangle)(e.getSource())).toFront();
        };

    EventHandler<MouseEvent> rectMoveDrag = (MouseEvent e) -> {

            double newSetX = e.getSceneX() - nwPosX;
            double newSetY = e.getSceneY() - nwPosY;
            double NSX = TX + newSetX;
            double NSY = TY + newSetY;


            ((Rectangle)(e.getSource())).setTranslateX(NSX);
            ((Rectangle)(e.getSource())).setTranslateY(NSY);
        };



//------------------------------------------------------------------------------
// MOUSE_DRAGGED CIRCLE
//------------------------------------------------------------------------------


    EventHandler<MouseEvent> circlMousePressed = (MouseEvent e) -> {
            nwPosX  = e.getSceneX();
            nwPosY  = e.getSceneY();
            TX  = ((Circle)(e.getSource())).getTranslateX();
            TY  = ((Circle)(e.getSource())).getTranslateY();


            ((Circle)(e.getSource())).toFront();

        };


    EventHandler<MouseEvent> circMoveDrag = (MouseEvent e) -> {

            double NSX = e.getSceneX() - nwPosX;
            double NSY = e.getSceneY() - nwPosY;
            double newX = TX + NSX;
            double newY = TY + NSY;


            ((Circle)(e.getSource())).setTranslateX(newX);
            ((Circle)(e.getSource())).setTranslateY(newY);
        };



//------------------------------------------------------------------------------
// MOUSE_DRAGGED POLYGON
//------------------------------------------------------------------------------

    EventHandler<MouseEvent> polyMousePressed = (MouseEvent e) -> {

            nwPosX  = e.getSceneX();
            nwPosY  = e.getSceneY();
            TX  = ((Polygon)(e.getSource())).getTranslateX();
            TY  = ((Polygon)(e.getSource())).getTranslateY();


            ((Polygon)(e.getSource())).toFront();

        };

    EventHandler<MouseEvent> polyMoveDrag = (MouseEvent e) -> {

            double newSetX = e.getSceneX() - nwPosX;
            double newSetY = e.getSceneY() - nwPosY;
            double NSX = TX + newSetX;
            double NSY = TY + newSetY;


            ((Polygon)(e.getSource())).setTranslateX(NSX);
            ((Polygon)(e.getSource())).setTranslateY(NSY);
        };

//------------------------------------------------------------------------------
// MOUSE_DRAGGED TEXT
//------------------------------------------------------------------------------


    EventHandler<MouseEvent> textMousePressed = (MouseEvent e) -> {

            nwPosX  = e.getSceneX();
            nwPosY  = e.getSceneY();
            TX  = ((TextField)(e.getSource())).getTranslateX();
            TY  = ((TextField)(e.getSource())).getTranslateY();


            ((TextField)(e.getSource())).toFront();

        };


    EventHandler<MouseEvent> textMoveDrag = (MouseEvent e) -> {

            double newSetX = e.getSceneX() - nwPosX;
            double newSetY = e.getSceneY() - nwPosY;
            double NSX = TX + newSetX;
            double NSY = TY + newSetY;


            ((TextField)(e.getSource())).setTranslateX(NSX);
            ((TextField)(e.getSource())).setTranslateY(NSY);
        };    


//------------------------------------------------------------------------------
// POPOVER PANELS FOR SIDE INFORMATION ON THE RIGHT
//------------------------------------------------------------------------------

     String linjetype = "Straight line";
     String rectangle = "Rectangle";
     String circle = "Circle";
     String poly1 = "Polygon";
     String text1 = "TEXT";


     Label line1 = new Label("\n"+linjetype+" ");
     Label rect1 = new Label("\n"+rectangle+" ");
     Label circ1 = new Label("\n"+circle+" ");
     Label poly2 = new Label("\n"+poly1+" ");
     Label text2 = new Label("\n"+text1+" ");

     VBox vBox = new VBox(line1);
     VBox vBox2 = new VBox(rect1);
     VBox vBox3 = new VBox(circ1);
     VBox vBox4 = new VBox(poly2);
     VBox vBox5 = new VBox(text2);

     PopOver popOver = new PopOver(vBox);
     PopOver popOver2 = new PopOver(vBox2);
     PopOver popOver3 = new PopOver(vBox3);
     PopOver popOver4 = new PopOver(vBox4);
     PopOver popOver5 = new PopOver(vBox5);


//------------------------------------------------------------------------------
// EVENTHANDLER: MOUSE_PRESSED LINE
//------------------------------------------------------------------------------

        pane.addEventHandler(MouseEvent.MOUSE_PRESSED, e-> {
            if(linebtn.isSelected()) {
            popOver.show(pane);
            line = new Line();
            line.setStartX(e.getX());
            line.setStartY(e.getY());  
            line.setStroke(cpFill.getValue()); 
            line.setOnMousePressed(lineMousePressed);
            line.setOnMouseDragged(lineMoveDrag);
            pane.getChildren().add(line);  
            }



//------------------------------------------------------------------------------
// MOUSE_PRESSED RECTANGLE
//------------------------------------------------------------------------------

            else if(rectbtn.isSelected()) {
            popOver2.show(pane);
            rect = new Rectangle(); 
            rect.setX(e.getX());                
            rect.setY(e.getY());     
            rect.setStroke(cpLine.getValue());
            rect.setFill(cpFill.getValue());  
            rect.setOnMousePressed(rectMousePressed);
            rect.setOnMouseDragged(rectMoveDrag);
            pane.getChildren().add(rect);
            }

//------------------------------------------------------------------------------
// MOUSE_PRESSED CIRCLE
//------------------------------------------------------------------------------

            else if(circbtn.isSelected()) {
            popOver3.show(pane);
            circ = new Circle();
            circ.setStroke(cpLine.getValue());
            circ.setFill(cpFill.getValue());
            circ.setCenterY(e.getY());
            circ.setCenterX(e.getX());
            pane.getChildren().add(circ);
            circ.setOnMousePressed(circlMousePressed);
            circ.setOnMouseDragged(circMoveDrag);
            }

//------------------------------------------------------------------------------
// MOUSE_PRESSED POLYGON
//------------------------------------------------------------------------------

            else if(polybtn.isSelected()) {
            popOver4.show(pane);
            poly = new Polygon();
            x[count] = e.getX();
            y[count] = e.getY(); 
            poly.setStroke(cpLine.getValue());
            poly.setFill(cpFill.getValue());
            poly.setOnMousePressed(polyMousePressed);
            poly.setOnMouseDragged(polyMoveDrag);
            pane.getChildren().add(poly);
            } 


//------------------------------------------------------------------------------
// MOUSE_PRESSED TEXTFIELD
//------------------------------------------------------------------------------

            else if(textbtn.isSelected()){
            popOver5.show(pane);
            text = new TextField();
            text.setFont(Font.font("Verdana", 30));    
            text.setOnMousePressed(textMousePressed);
            text.setOnMouseDragged(textMoveDrag);
            text.toFront();
            pane.getChildren().add(text);
            }

         }); 


//------------------------------------------------------------------------------
// EVENTHANDLER: MOUSE_RELEASED RECTANGLE
//------------------------------------------------------------------------------

            pane.addEventHandler(MouseEvent.MOUSE_RELEASED, e-> {
            if(rectbtn.isSelected()) {
            rect.setWidth(e.getX() - rect.getX());
            rect.setHeight(e.getY() - rect.getY());
            }     

//------------------------------------------------------------------------------
// MOUSE_RELEASED LINE
//------------------------------------------------------------------------------

            else if(linebtn.isSelected()) {
            line.setEndX(e.getX());
            line.setEndY(e.getY());

            }

//------------------------------------------------------------------------------
// MOUSE_RELEASED CIRCLE
//------------------------------------------------------------------------------

            else if(circbtn.isSelected()) {
            //Math.abs gives exact value of the radius
            circ.setRadius(Math.abs(e.getX() - circ.getCenterX()) + Math.abs(e.getY() - circ.getCenterY()) / 2);
             }

//------------------------------------------------------------------------------
// MOUSE_RELEASED POLYGON
//------------------------------------------------------------------------------

           else if(polybtn.isSelected()) {
            values.add(x[count]);
            values.add(y[count]);
            count++;
            poly.getPoints().addAll(values);
           }
      });

//------------------------------------------------------------------------------
// STAGE AND SCENE
//------------------------------------------------------------------------------

        BorderPane pane1 = new BorderPane();
        pane1.setTop(buttons);
        pane1.setRight(colors);
        pane1.setCenter(pane);
        Scene scene = new Scene(pane1, 800, 450);
        primaryStage.setTitle("Tegneprogram");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }  
}

//------------------------------------------------------------------------------
// END
//------------------------------------------------------------------------------

所以在我看来,我认为Figure超类将是这样的,您这里拥有所有对象实例以及形状的颜色,等等:

public class Figur {

     Rectangle rect; 
     Line line;
     Polygon poly;
     Circle circ;
     TextField text;

     ColorPicker cpFill;
     ColorPicker cpLine;

}

然后设置所有事件处理程序以在单独的子类中绘制图形。那有可能吗,我将如何将它们连接在一起?

Line子类可以是类似的东西,可以使用“ createdLine”方法绘制线条。其他形状也是如此。

public class Line extends Figur {

    public void createLine(){

            popOver.show(pane);
            super(line);
            line.setStartX(e.getX());
            line.setStartY(e.getY());  
            line.setStroke(cpFill.getValue()); 
            line.setOnMousePressed(lineMousePressed);
            line.setOnMouseDragged(lineMoveDrag);
            pane.getChildren().add(line);  
         }
    }

以及结束行的绘制方法:

public void endLine(){
        line.setEndX(e.getX());
        line.setEndY(e.getY());
     }
}

1 个答案:

答案 0 :(得分:0)

您可能想将Figur(我想是您想要的这个拼写错误的版本)声明为仅包含以下成员的抽象类:cpFillcpLine,{{1 }},mousePressedHandler和一个构造函数,该构造函数接收mouseDraggedHandlercpFill的参数。然后,对于每个子类,您需要声明其中包含的形状对象以及定义cpLinemousePressedHandler的构造函数。

编辑:代码示例可以更好地证明我上面所述的内容

mouseDraggedHandler

注意:既然我已经编写了代码,我意识到您不必在抽象类中声明public abstract class Figure { private ColorPicker cpFill; private ColorPicker cpLine; private EventHandler<MouseEvent> mousePressedHandler; private EventHandler<MouseEvent> mouseDraggedHandler; public Figur(ColorPicker cpFill, ColorPicker cpLine) { this.cpFill = cpFill; this.cpLine = cpLine; } } public class MyRectangle extends Figure { private Rectangle rect; public MyRectangle(ColorPicker cpFill, ColorPicker cpLine) { super(cpFill, cpLine); mousePressedHandler = e -> { // do something with your rectangle here } mouseDraggedHandler = e -> { // do another thing with your rectangle here } rect.setOnMousePressed(mousePressedHandler); rect.setOnMouseDragged(mouseDraggedHandler); } } public class MyLine extends Figure { private Line line; public MyRectangle(ColorPicker cpFill, ColorPicker cpLine) { super(cpFill, cpLine); mousePressedHandler = e -> { // do something with your line here } mouseDraggedHandler = e -> { // do another thing with your line here } line.setOnMousePressed(mousePressedHandler); line.setOnMouseDragged(mouseDraggedHandler); } } mousePressedHandler。但是,我仍然将它们保留在此处,以使事情更加清楚。随时删除它们,只需致电

mouseDraggedHandler

代替