Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Created March 16, 2020 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/3981e89111e64b66a82b5ec471e18183 to your computer and use it in GitHub Desktop.
Save bytecodeman/3981e89111e64b66a82b5ec471e18183 to your computer and use it in GitHub Desktop.
HW4 Solution Chinese Flag (No Scalability)
// Antonio Silvestri
// 03/15/20
// HW4 Draw the Chinese Flag (No Scalability)
// CSC-112 Intermediate Java Programming
// silvestri@stcc.edu
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ChineseFlag extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Scene scene = new Scene(new ChineseFlagPane(), 600, 400);
primaryStage.setTitle("Chinese Flag by A.C. Silvestri");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
/**
* The main method is only needed for the IDE with limited JavaFX support.
* Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
// ****************************************************************************
class ChineseFlagPane extends Pane {
private static double FLAGWIDTH = 600;
private static double FLAGHEIGHT = 400;
private static double UNITWIDTH;
private static double UNITHEIGHT;
private static final Image STARIMAGE = new Image("/images/chineseflagstar.png");
public ChineseFlagPane() {
UNITWIDTH = FLAGWIDTH / 2 / 15;
UNITHEIGHT = FLAGHEIGHT / 2 / 10;
this.setBackgroundColor();
this.drawMainStar();
this.drawSatelliteStars();
}
private void setBackgroundColor() {
this.setStyle("-fx-background-color : #de2910ff;");
}
private void drawMainStar() {
final double CENTERXOFSTAR = 5 * UNITWIDTH;
final double CENTERYOFSTAR = 5 * UNITHEIGHT;
final double WIDTHOFSTAR = 6 * UNITWIDTH;
final double HEIGHTOFSTAR = 6 * UNITHEIGHT;
ImageView bigStar = makeStar(CENTERXOFSTAR, CENTERYOFSTAR, WIDTHOFSTAR, HEIGHTOFSTAR, 0);
this.getChildren().add(bigStar);
}
private void drawSatelliteStars() {
final double WIDTHOFSTAR = 2 * UNITWIDTH;
final double HEIGHTOFSTAR = 2 * UNITHEIGHT;
final int ssc[][] = {{10, 2}, {12, 4}, {12, 7}, {10, 9}}; //ssc = smallStarCenters
for (int i = 0; i < ssc.length; i++) {
double angle = -Math.toDegrees(Math.atan((5 - ssc[i][0]) / (ssc[i][1] - 5)));
if (angle >= 0)
angle = -angle;
ImageView smallStar = makeStar(ssc[i][0] * UNITWIDTH, ssc[i][1] * UNITHEIGHT, WIDTHOFSTAR, HEIGHTOFSTAR, angle);
this.getChildren().add(smallStar);
}
}
private ImageView makeStar(double centerX, double centerY, double width, double height, double rotate) {
ImageView starImageView = new ImageView(STARIMAGE);
starImageView.setFitWidth(width);
starImageView.setFitHeight(height);
starImageView.setPreserveRatio(true);
starImageView.setLayoutX(centerX - width / 2);
starImageView.setLayoutY(centerY - height / 2);
starImageView.setRotate(rotate);
return starImageView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment