使用JavaFX中的画布上的OnMouseMoved在x轴上移动形状

时间:2017-02-05 22:58:09

标签: canvas javafx

我创建了Shape,我需要在move horizontal position Canvas x-axistimelineClass Square,而不使用canvas }

public class Square{ //calculate the position of the rand column to //draw and insert in the position of the canvas public void drawSquare(GraphicsContext gc) { //Square Shadow //gc.rect(posX, posY, w, h); gc.rect(1, 53, 50, 50); gc.fill(); gc.beginPath(); //Square gc.beginPath(); gc.setFill(Color.WHITE); gc.setStroke(Color.BLACK); gc.setLineWidth(2); //gc.rect(posX, posY, w, h); gc.rect(1, 53, 48, 48); gc.fill(); gc.stroke(); } } 绘制一个正方形并插入Canvas instance位置

height = 450

width = 600 Canvas canvas = new Canvas(); canvas.setHeight(450); canvas.setWidth(600); GraphicsContext

draw square

GraphicsContext gc = canvas.getGraphicsContext2D(); shape

create

draw squareSquare square= new Square(); square.drawSquare(gc); drawSquare

canvas

以及mouse eventmove square

的示例

以及canvasX-Axis上的canvas.setOnMouseMoved((MouseEvent event) -> { //select the square and move on x-axis }); int32_t constructFinal (unsigned opcode, unsigned rs, unsigned rt, unsigned rd, unsigned shamt, unsigned funct) { unsigned final = ((opcode & 0x3F) << 26) | ((rs & 0x1F) << 21) | ((rt & 0x1F) << 16) | ((rd & 0x1F) << 11) | ((shamt & 0x1F) << 6) | (funct & 0x3F); return (int32_t)final; } ,我做了些什么 但它不起作用,也许可以有另一种解决方案

reg [63:0] seq_input;
$memreadb("pattern.in", seq_input);

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

试试这个(基于MikaelF

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        Rectangle rectBound = new Rectangle();
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        Canvas c = new Canvas(400,400);

        root.setCenter(c);
        c.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
            boolean shouldDraw = false;
            double dX,dY;

            @Override
            public void handle(MouseEvent arg0) {
                if (arg0.getEventType() == MouseEvent.MOUSE_PRESSED) {
                    shouldDraw = rectBound.contains(arg0.getX(), arg0.getY());
                    if (shouldDraw) {
                        dX = arg0.getX();
                        dY = arg0.getY();
                    }
                } else if (arg0.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                    if (shouldDraw) {
                        double x = (arg0.getX() + rectBound.getX() - dX),
                                y = (arg0.getY() + rectBound.getY() - dY);

                        c.getGraphicsContext2D().clearRect(rectBound.getX(),
                                rectBound.getY(), rectBound.getWidth(),//50
                                rectBound.getHeight());

                        rectBound.setY(y);
                        rectBound.setX(x);

                        dX = arg0.getX();
                        dY = arg0.getY();
                    }
                }
            }
        });

        c.getGraphicsContext2D().setFill(Color.AQUAMARINE);
        rectBound.xProperty().addListener(new ChangeListener<Number>(){//since they go together
            @Override
            public void changed(ObservableValue<? extends Number> arg0,
                    Number arg1, Number arg2) {
                c.getGraphicsContext2D().fillRect(rectBound.getX(),rectBound.getY(),
                        rectBound.getWidth(),rectBound.getHeight()); //just an example
            }

        });

        primaryStage.setScene(scene);
        primaryStage.show();
        rectBound.setWidth(50);rectBound.setHeight(50);
        rectBound.setY(50);rectBound.setX(10);//because of the listener x is last
    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

这可能是一种替代方法,要注意这是一项艰苦的工作