Created
December 5, 2014 16:38
-
-
Save orekyuu/afb416d5c722edeeb79e to your computer and use it in GitHub Desktop.
JavaFXAdventCalendar24日目で作ったやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.animation.AnimationTimer; | |
import javafx.fxml.Initializable; | |
import javafx.geometry.Point2D; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.paint.Color; | |
import java.net.URL; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
public Pane root; | |
private Canvas canvas; | |
public static final int CanvasWidth = 600; | |
public static final int CanvasHeight = 600; | |
private List<Star> stars = new LinkedList<>(); | |
private List<Light> lights = new LinkedList<>(); | |
private List<Snow> snows = new LinkedList<>(); | |
private Random random = new Random(); | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
canvas = new Canvas(CanvasWidth, CanvasHeight); | |
root.getChildren().add(canvas); | |
AnimationTimer animationTimer = new AnimationTimer() { | |
@Override | |
public void handle(long now) { | |
GraphicsContext g = canvas.getGraphicsContext2D(); | |
g.clearRect(0, 0, CanvasWidth, CanvasHeight); | |
//背景 | |
drawSky(g); | |
//星を描く | |
drawStar(g, now); | |
//木を描く | |
drawTree(g); | |
//ライトを描く | |
drawLight(g, now); | |
//雪を描く | |
drawSnow(g, now); | |
} | |
}; | |
animationTimer.start(); | |
for (int i = 0; i < 100; i++) { | |
double r = random.nextDouble() * Math.PI * 2; | |
Star star = new Star(new Point2D(random.nextFloat() * CanvasWidth, random.nextFloat() * CanvasHeight), 3, Color.YELLOW, r); | |
stars.add(star); | |
} | |
canvas.setOnMouseClicked(e -> { | |
Light light = new Light(new Point2D(e.getX(), e.getY()), 5, Color.RED); | |
lights.add(light); | |
}); | |
} | |
private long last = 0; | |
private void drawSnow(GraphicsContext g, long t) { | |
long coolTime = 40; | |
if (t - last > coolTime) { | |
last = coolTime; | |
snows.add(new Snow(random)); | |
last = t; | |
} | |
snows.forEach(snow -> snow.draw(g, t)); | |
snows.removeIf(Snow::isRemove); | |
} | |
private void drawLight(GraphicsContext g, long t) { | |
lights.forEach(light -> light.draw(g, t)); | |
} | |
private void drawStar(GraphicsContext g, long t) { | |
stars.forEach(star -> star.draw(g, t)); | |
} | |
private void drawSky(GraphicsContext g) { | |
g.setFill(new Color(0.11, 0.11, 0.51, 1)); | |
g.fillRect(0, 0, CanvasWidth, CanvasHeight); | |
} | |
private void drawTree(GraphicsContext g) { | |
g.setFill(new Color(0, 0.25, 0, 1)); | |
for (int i = 0; i < 3; i++) { | |
double width = 100 + 50 * i; | |
double yPos = 100 + 70 * i; | |
double height = 100 + 50 * i; | |
g.fillPolygon(new double[]{CanvasWidth / 2, (CanvasWidth / 2) - width, (CanvasWidth / 2) + width}, new double[]{yPos, yPos + height, yPos + height}, 3); | |
} | |
g.setFill(new Color(0.25, 0.12, 0.03, 1)); | |
int width = 70; | |
g.fillRect((CanvasWidth / 2) - (width / 2), 440, width, CanvasHeight - 440); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.geometry.Point2D; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.paint.Color; | |
public class Light { | |
private Color color; | |
private Point2D point; | |
private double size; | |
private double r; | |
public Light(Point2D point, double size, Color color) { | |
this.color = color; | |
this.point = point; | |
this.size = size; | |
} | |
public void draw(GraphicsContext g, long time) { | |
r += 0.2; | |
double alpha = (Math.sin(r) + 1) / 2; | |
g.setFill(new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha)); | |
g.fillOval(point.getX(), point.getY(), size, size); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?import javafx.scene.layout.Pane?> | |
<Pane fx:id="root" fx:controller="sample.Controller" | |
xmlns:fx="http://javafx.com/fxml" prefWidth="600" prefHeight="600"> | |
</Pane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.geometry.Point2D; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.paint.Color; | |
import java.util.Random; | |
public class Snow { | |
private Point2D point; | |
private double size; | |
private Color color; | |
private double speed; | |
public Snow(Random rand) { | |
size = 3 * rand.nextDouble() * 2; | |
color = new Color(1, 1, 1, 0.3 + rand.nextDouble() * 0.7); | |
point = new Point2D(rand.nextDouble() * Controller.CanvasWidth, -size); | |
speed = 0.2 + rand.nextDouble() * 3; | |
} | |
public void draw(GraphicsContext g, long time) { | |
point = new Point2D(point.getX(), point.getY() + speed); | |
g.setFill(color); | |
g.fillOval(point.getX(), point.getY(), size, size); | |
} | |
public boolean isRemove() { | |
return point.getY() > Controller.CanvasHeight; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.geometry.Point2D; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.paint.Color; | |
public class Star { | |
private Color color; | |
private Point2D point; | |
private double size; | |
private double r; | |
public Star(Point2D point, double size, Color color, double r) { | |
this.color = color; | |
this.point = point; | |
this.size = size; | |
this.r = r; | |
} | |
public void draw(GraphicsContext g, long time) { | |
r += 0.1; | |
double alpha = (Math.sin(r) + 1) / 2; | |
g.setFill(new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha)); | |
g.fillOval(point.getX(), point.getY(), size, size); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment