简体   繁体   English

如何使用Javafx canvas在另一个Circle上移动Point?

[英]How to move a Point around another Circle use Javafx canvas?

I want to move a black point around the edge of another circle. 我想在另一个圆的边缘周围移动一个黑点。

The move path of this point is edge of circle. 该点的移动路径是圆的边缘。

I draw theme in Canvas . 我在“ 画布”中绘制主题。

But PathTransition() only receive Shape as parameter. 但是PathTransition()仅接收Shape作为参数。

How can I use this circle in canvas as move path ??? 如何在画布中将此圆用作移动路径 ???

btNewCircleClick():in this method,click "new 2 ciecle" will creat point and circle btNewCircleClick():在此方法中,单击“ new 2 ciecle”将创建点和圆

btRun():in this method,clike run will make this point move around the circle. btRun():在此方法中,clike run将使该点绕圆运动。

在此处输入图片说明

Controller.kt: Controller.kt:

package sample

import javafx.fxml.FXML
import javafx.scene.canvas.Canvas

class Controller{
    @FXML
    private lateinit var cv: Canvas
    @FXML
    fun btNewCircleClick(){
        val gc=cv.graphicsContext2D
        val c2=gc.fillOval(0.0, 0.0, 3.0, 3.0)
        val c1=gc.strokeOval(60.0, 60.0, 60.0, 60.0)
        //val pt=PathTransition(Duration.millis(4000.0),Circle(60.0,60.0,30.0),)
    }
    @FXML
    fun btRun(){

    }
}

sample.fxml sample.fxml

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <Button fx:id="btNewCircle" onMouseClicked="#btNewCircleClick" text="new 2 circle" GridPane.rowIndex="0" GridPane.columnIndex="0"></Button>
    <Button fx:id="btRun" onMouseClicked="#btRun" text="run" GridPane.rowIndex="1" GridPane.columnIndex="0"></Button>
    <Canvas fx:id="cv" width="200" height="200" GridPane.rowIndex="2" GridPane.columnIndex="0"></Canvas>
</GridPane>

I don't know how to make it move... 我不知道该如何移动...

Why do you want to use a canvas? 为什么要使用画布? In FX you could do this easily within a Pane..... Canvas is for raw drawing via GraphicsContext - not much difference to an AWT Graphics. 在FX中,您可以在窗格中轻松完成此操作。....画布用于通过GraphicsContext进行原始绘图-与AWT图形没有太大区别。 Effects/Anims in JavaFX are for Nodes within the scenegraph, not for pixel graphics JavaFX中的效果/动画适用于场景图中的节点,不适用于像素图形

public class Main extends Application {

Circle c, p;
PathTransition pt;

@Override
public void start(Stage primaryStage) {
    try {

        VBox root = new VBox();
        HBox hb = new HBox(5);
        Button movePoint = new Button("MOVE");
        movePoint.setOnAction(e -> {
            if (pt.getStatus() != Status.RUNNING) {
                pt.playFromStart();
            }

        });
        Button resetPoint = new Button("Reset");
        resetPoint.setOnAction(e -> {
            if (pt.getStatus() == Status.RUNNING) {
                pt.jumpTo("end");
            }
        });
        hb.getChildren().addAll(movePoint, resetPoint);

        StackPane sp = new StackPane();
        sp.setMinWidth(640);
        sp.setMinHeight(480);

        c = new Circle(100);
        c.setStroke(Color.BLACK);
        c.setFill(Color.TRANSPARENT);

        p = new Circle(5);
        p.setManaged(false);
        p.setFill(Color.ORANGERED);
        p.setTranslateX(sp.getMinWidth() / 2 + c.getRadius());
        p.setTranslateY(sp.getMinHeight() / 2);

        sp.getChildren().addAll(c, p);

        root.getChildren().addAll(hb, sp);
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.show();
        initPT(p);

    } catch (Exception e) {
        e.printStackTrace();
    }
  }

  private void initPT(Node p) {
    pt = new PathTransition(Duration.millis(1500), c, p);
  }

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

Altered code from here and here . 这里这里更改代码。 The is not a perfect rotation but you can figure out the math for that. 这不是一个完美的轮换,但是您可以算出它的数学值。 This example uses AnimationTimer 本示例使用AnimationTimer

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication135 extends Application
{
    @Override
    public void start(Stage theStage)
    {
        theStage.setTitle("Timeline Example");

        Group root = new Group();
        Scene theScene = new Scene(root);
        theStage.setScene(theScene);

        Canvas canvas = new Canvas(512, 512);
        root.getChildren().add(canvas);

        final long startNanoTime = System.nanoTime();

        new AnimationTimer()
        {
            @Override
            public void handle(long currentNanoTime)
            {
                GraphicsContext gc = canvas.getGraphicsContext2D();
                double t = (currentNanoTime - startNanoTime) / 1000000000.0;

                double x = 232 + 128 * Math.cos(t);
                double y = 232 + 128 * Math.sin(t);

                drawEarth(gc, x, y, Color.BLUE);
                drawSun(gc, 512 / 2.0, 512 / 2.0, Color.YELLOW);
            }
        }.start();

        theStage.show();
    }

    private void drawEarth(GraphicsContext gc, double x, double y, Color color)
    {
        // background image clears canvas
        gc.setFill(Color.CORNSILK);
        gc.fillRect(0, 0, 512, 512);//Clear the Canvas

        //Redraw Earth Circle
        gc.setFill(color);
        gc.fillOval(x, y, 50, 50);
    }

    private void drawSun(GraphicsContext gc, double x, double y, Color color)
    {
        gc.setFill(color);
        gc.fillOval(x, y, 50, 50);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

I don't know the Kotlin translation 我不知道Kotlin翻译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM