Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

java - JavaFX Making a Face

I am working on an a Java FX program where I have to make an emoji face. I have got the circle made with a border, the eyes and the frown. I am having a problem with the eyebrows. I have one of the eyebrows set in place but I can't seem to get the other eyebrow to match on the other side of the face. This is my first JavaFX program so I have very limited knowledge and the textbook I have is not very good. Anything would be helpful. The eyebrows are line1 and line2.

  package application;

  import javafx.application.Application;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.Arc;
  import javafx.scene.shape.ArcType;
  import javafx.scene.shape.Circle;
  import javafx.scene.shape.Line;

public class TracyAssignment2Chap14 extends Application {

@Override
public void start(Stage primaryStage) {
try {
    Circle circle = new Circle();
    Circle circle2 = new Circle();
    Circle circle3 = new Circle();
    circle.setCenterX(220.0);
    circle.setCenterY(220.0);
    circle.setRadius(190.0);
    circle.setFill(Color.YELLOW);
    circle.setStrokeWidth(15.0);
    circle.setStroke(Color.BLACK);
    circle2.setCenterX(300.0);
    circle2.setCenterY(215.0);
    circle2.setRadius(25.0);
    circle2.setFill(Color.BLACK);
    circle3.setCenterX(150.0);
    circle3.setCenterY(215.0);
    circle3.setRadius(25.0);
    circle3.setFill(Color.BLACK);
    Arc arc = new Arc(220,310,80,60,0,180);
    arc.setFill(null);
    arc.setStroke(Color.BLACK);
    arc.setStrokeWidth(15.0);
    arc.setType(ArcType.OPEN);
    Line line1= new Line();
    line1.setStartX(180.0);
    line1.setStartY(180.0);
    line1.setEndX(150.0);
    line1.setEndY(160.0);
    line1.setStrokeWidth(15.0);
    line1.setStroke(Color.BLACK);
    Line line2 = new Line ();
    line2.setStartX(200.0);
    line2.setStartY(180.0);
    line2.setEndX(270.0);
    line2.setEndY(160.0);
    line2.setStrokeWidth(15.0);
    line2.setStroke(Color.BLACK);
    Group root = new Group(circle,circle2,circle3,arc,line1,line2);
    Scene scene = new Scene(root,450,450);
    primaryStage.setTitle("Emoji Assignment");
    scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
    //add the scene to the stage
    primaryStage.setScene(scene);
    //show the contents of the stage 
    primaryStage.show();
} catch(Exception e) {
    e.printStackTrace();
}

}



public static void main(String[] args) {
    launch(args);
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One thing you can do is base everything off the face center. I changed some of your variables so that their roles are clear. Also, what error are you catching?

Example code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplication34;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;

public class TracyAssignment2Chap14 extends Application {

    @Override
    public void start(Stage primaryStage) {
        double faceCenterX = 220.0;
        double faceCenterY = 220.0;
        double eyeYFromFaceCenter = 5.0;
        double eyeXFromFaceCenter = 80.0;
        double eyeBrowStartYFromFaceCenter = 40.0;
        double eyeBrowStartXFromFaceCenter = 40.0;
        double eyeBrowEndYFromFaceCenter = 70.0;
        double eyeBrowEndXFromFaceCenter = 80.0;

        Circle face = new Circle();
        face.setCenterX(faceCenterX);
        face.setCenterY(faceCenterY);
        face.setRadius(190.0);
        face.setFill(Color.YELLOW);
        face.setStrokeWidth(15.0);
        face.setStroke(Color.BLACK);

        Circle leftEye = new Circle();
        leftEye.setCenterX(faceCenterX - eyeXFromFaceCenter);
        leftEye.setCenterY(faceCenterY - eyeYFromFaceCenter);
        leftEye.setRadius(25.0);
        leftEye.setFill(Color.BLACK);

        Circle rightEye = new Circle();
        rightEye.setCenterX(faceCenterX + eyeXFromFaceCenter);
        rightEye.setCenterY(faceCenterY - eyeYFromFaceCenter);
        rightEye.setRadius(25.0);
        rightEye.setFill(Color.BLACK);

        Line leftEyebrow = new Line();
        leftEyebrow.setStartX(faceCenterX - eyeBrowStartXFromFaceCenter);
        leftEyebrow.setStartY(faceCenterY - eyeBrowStartYFromFaceCenter);
        leftEyebrow.setEndX(faceCenterX - eyeBrowEndXFromFaceCenter);
        leftEyebrow.setEndY(faceCenterY - eyeBrowEndYFromFaceCenter);
        leftEyebrow.setStrokeWidth(15.0);
        leftEyebrow.setStroke(Color.BLACK);

        Line RightEyebrow = new Line();
        RightEyebrow.setStartX(faceCenterX + eyeBrowStartXFromFaceCenter);
        RightEyebrow.setStartY(faceCenterY - eyeBrowStartYFromFaceCenter);
        RightEyebrow.setEndX(faceCenterX + eyeBrowEndXFromFaceCenter);
        RightEyebrow.setEndY(faceCenterY - eyeBrowEndYFromFaceCenter);
        RightEyebrow.setStrokeWidth(15.0);
        RightEyebrow.setStroke(Color.BLACK);

        Arc mouth = new Arc(faceCenterX, faceCenterY + 90, 80, 60, 0, 180);//(220,310,80,60,0,180);
        mouth.setFill(null);
        mouth.setStroke(Color.BLACK);
        mouth.setStrokeWidth(15.0);
        mouth.setType(ArcType.OPEN);

//   
        Group root = new Group(face, leftEye, rightEye, leftEyebrow, RightEyebrow, mouth);
        Scene scene = new Scene(root, 450, 450);
        primaryStage.setTitle("Emoji Assignment");
        ///scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        //add the scene to the stage
        primaryStage.setScene(scene);
        //show the contents of the stage 
        primaryStage.show();
    }

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...