|
Draw something on WritableImage |
package javafxpixel;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFXPixel extends Application {
WritableImage myWritableImage;
PixelWriter myPixelWriter;
final int IMG_WIDTH = 255;
final int IMG_HEIGHT = 255;
@Override
public void start(Stage primaryStage) {
prepareMyWritableImage();
drawSomethingOnMyWritableImage();
ImageView myImage = new ImageView();
myImage.setImage(myWritableImage);
StackPane root = new StackPane();
root.getChildren().add(myImage);
Scene scene = new Scene(root, 300, 300);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void prepareMyWritableImage(){
myWritableImage = new WritableImage(IMG_WIDTH, IMG_HEIGHT);
myPixelWriter = myWritableImage.getPixelWriter();
//fill with background blue
for(int x = 0; x < IMG_WIDTH; x++){
for(int y = 0; y < IMG_HEIGHT; y++){
myPixelWriter.setColor(x, y, Color.GRAY);
}
}
}
private void drawSomethingOnMyWritableImage(){
for(int x = 20; x < IMG_WIDTH - 20; x++){
for(int y = 20; y < IMG_HEIGHT - 20; y++){
myPixelWriter.setColor(x, y,
new Color((double)x/255, (double)y/255, 0.0, 1.0));
}
}
double l = 100.0;
int cx = IMG_WIDTH/2;
int cy = IMG_HEIGHT/2;
for(int c = 0; c < 360; c++){
double radian = Math.toRadians((double)c);
int x = cx + (int)(l * Math.cos(radian));
int y = cy + (int)(l * Math.sin(radian));
myPixelWriter.setColor(x, y, Color.WHITE);
}
}
}