mg7938841
2021-07-29 08:17
采纳率: 88.9%
浏览 71
已结题

javaFX刚学不久遇到一个问题

下面是代码,我想水平地重复打印4个这样的笑脸图形,有什么简便方法


package application;

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

public class face extends Application {
    @Override
    public void start(Stage stage) 
    {
        Group mygroup = new Group();
        Canvas mycanvas = new Canvas(800,300);
        GraphicsContext mygraph = mycanvas.getGraphicsContext2D();
        mygraph.setStroke(Color.PLUM);
        mygraph.strokeOval(100, 50, 200, 200);
        mygraph.setFill(Color.BLUE);
        mygraph.fillOval(195,150,10,10);
        mygraph.setFill(Color.CRIMSON);
        mygraph.fillOval(160, 100, 10, 20);
        mygraph.fillOval(230, 100, 10, 20);
        mygraph.setStroke(Color.PINK);
        mygraph.strokeArc(150, 160, 100, 50, 180, 180, ArcType.OPEN);    
        mygroup.getChildren().add(mycanvas);        
        Scene mysc = new Scene(mygroup);
        stage.setScene(mysc);
        stage.show();
    }
    public static void main(String[] args) {
            launch(args);
        }
}
  • 好问题 提建议
  • 收藏

4条回答 默认 最新

  • 漠上刀栈 2021-07-29 15:56
    已采纳

    原理就是这样,我懒得调整位置,你自己调整下位置就可以了。代码如下:效果图如下:

    
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.canvas.Canvas;
    import javafx.stage.Stage;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.ArcType;
    
    public class face extends Application {
        @Override
        public void start(Stage stage) {
            Canvas mycanvas = new Canvas(800, 300);
            for (int i = 1; i < 4; i++) {
                Group mygroup = new Group();
                GraphicsContext mygraph = mycanvas.getGraphicsContext2D();
                mygraph.setStroke(Color.PLUM);
                mygraph.strokeOval(100 * i, 50, 200, 200);
                mygraph.setFill(Color.BLUE);
                mygraph.fillOval(195 * i, 150, 10, 10);
                mygraph.setFill(Color.CRIMSON);
                mygraph.fillOval(160 * i, 100, 10, 20);
                mygraph.fillOval(230 * i, 100, 10, 20);
                mygraph.setStroke(Color.PINK);
                mygraph.strokeArc(150 * i, 160, 100, 50, 180, 180, ArcType.OPEN);
                mygroup.getChildren().add(mycanvas);
                Scene mysc = new Scene(mygroup);
                stage.setScene(mysc);
            }
            stage.show();
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    

    img

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • sinJack 2021-07-29 08:37

    用循环就行,改变x轴的位置,y轴不变。

    评论
    解决 无用
    打赏 举报
  • 谍纸天眼 2021-07-29 16:03

    public class Face extends Application {
    @Override
    public void start(Stage stage) throws Exception {
    Group mygroup = new Group();
    Canvas mycanvas = new Canvas(1200,300);
    int[] a={0,200,400,600,800};
    GraphicsContext mygraph = mycanvas.getGraphicsContext2D();
    for (int i=0 ;i<a.length; i++){
    mygraph.setStroke(Color.PLUM);
    mygraph.strokeOval(100+a[i], 50, 200, 200);
    mygraph.setFill(Color.BLUE);
    mygraph.fillOval(195+a[i],150,10,10);
    mygraph.setFill(Color.CRIMSON);
    mygraph.fillOval(160+a[i], 100, 10, 20);
    mygraph.fillOval(230+a[i], 100, 10, 20);
    mygraph.setStroke(Color.PINK);
    mygraph.strokeArc(150+a[i], 160, 100, 50, 180, 180, ArcType.OPEN);
    }
    mygroup.getChildren().add(mycanvas);
    Scene mysc = new Scene(mygroup);
    stage.setScene(mysc);
    stage.show();
    }

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

    }

    评论
    解决 无用
    打赏 举报
  • 谍纸天眼 2021-07-29 16:05

    public class Face extends Application {
    @Override
    public void start(Stage stage) throws Exception {
    Group mygroup = new Group();
    Canvas mycanvas = new Canvas(1200,300);
    int[] a={0,200,400,600,800};
    GraphicsContext mygraph = mycanvas.getGraphicsContext2D();
    for (int i=0 ;i<a.length; i++){
    mygraph.setStroke(Color.PLUM);
    mygraph.strokeOval(100+a[i], 50, 200, 200);
    mygraph.setFill(Color.BLUE);
    mygraph.fillOval(195+a[i],150,10,10);
    mygraph.setFill(Color.CRIMSON);
    mygraph.fillOval(160+a[i], 100, 10, 20);
    mygraph.fillOval(230+a[i], 100, 10, 20);
    mygraph.setStroke(Color.PINK);
    mygraph.strokeArc(150+a[i], 160, 100, 50, 180, 180, ArcType.OPEN);
    }
    mygroup.getChildren().add(mycanvas);
    Scene mysc = new Scene(mygroup);
    stage.setScene(mysc);
    stage.show();
    }

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

    }

    img

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题