Created
February 4, 2014 20:33
-
-
Save gunungloli666/8811782 to your computer and use it in GitHub Desktop.
This file contains 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 fjr.image | |
import java.io.File | |
import javafx.application.Application | |
import javafx.scene.Group | |
import javafx.scene.Scene | |
import javafx.scene.canvas.Canvas | |
import javafx.scene.canvas.GraphicsContext | |
import javafx.scene.image.Image | |
import javafx.scene.image.ImageView | |
import javafx.scene.image.PixelReader | |
import javafx.scene.layout.StackPane | |
import javafx.scene.paint.Color | |
import javafx.scene.shape.LineTo | |
import javafx.scene.shape.MoveTo | |
import javafx.scene.shape.Path | |
import javafx.scene.shape.PathBuilder | |
import javafx.scene.shape.QuadCurve | |
import javafx.scene.transform.Affine | |
import javafx.scene.transform.Rotate | |
import javafx.stage.Stage | |
import Rutte._ | |
object Rutte { | |
def rotateX(gc: GraphicsContext, angle: Double) { | |
val affine = new Affine() | |
val angleRadi = Math.toRadians(angle) | |
val sinus = Math.sin(angleRadi) | |
val cosinus = Math.cos(angleRadi) | |
affine.setMxx(1) | |
affine.setMxy(0) | |
affine.setMxz(0) | |
affine.setTx(0) | |
affine.setMyx(0) | |
affine.setMyy(cosinus) | |
affine.setMyz(-sinus) | |
affine.setTy(0) | |
affine.setMzx(0) | |
affine.setMzy(sinus) | |
affine.setMzz(cosinus) | |
affine.setTz(0) | |
gc.setTransform(affine) | |
} | |
def rotateY(gc: GraphicsContext, angle: Double) { | |
val affine = new Affine() | |
val angleRadi = Math.toRadians(angle) | |
val sinus = Math.sin(angleRadi) | |
val cosinus = Math.cos(angleRadi) | |
affine.setMxx(cosinus) | |
affine.setMxy(0) | |
affine.setMxz(sinus) | |
affine.setTx(0) | |
affine.setMyx(0) | |
affine.setMyy(1) | |
affine.setMyz(0) | |
affine.setTy(0) | |
affine.setMzx(-sinus) | |
affine.setMzy(0) | |
affine.setMzz(-cosinus) | |
affine.setTz(0) | |
gc.setTransform(affine) | |
} | |
def rotateZ(gc: GraphicsContext, angle: Double) { | |
val affine = new Affine() | |
val angleRadi = Math.toRadians(angle) | |
val sinus = Math.sin(angleRadi) | |
val cosinus = Math.cos(angleRadi) | |
affine.setMxx(cosinus) | |
affine.setMxy(-sinus) | |
affine.setMxz(0) | |
affine.setTx(0) | |
affine.setMyx(sinus) | |
affine.setMyy(cosinus) | |
affine.setMyz(0) | |
affine.setTy(0) | |
affine.setMzx(0) | |
affine.setMzy(0) | |
affine.setMzz(1) | |
affine.setTz(0) | |
gc.setTransform(affine) | |
} | |
def main(args: Array[String]) { | |
Application.launch(classOf[Rutte], args:_*) | |
} | |
} | |
class Rutte extends Application { | |
var step: Int = 5 | |
var stepY: Int = 50 | |
var depth: Double = 56 | |
var width: Double = 0 | |
var height: Double = 0 | |
override def start(primaryStage: Stage) { | |
val root = getEsembel | |
primaryStage.setScene(new Scene(root)) | |
primaryStage.setTitle("TUT WURI HANDAYANI") | |
primaryStage.show() | |
} | |
def getEsembel(): Group = { | |
val root = new Group() | |
val image = new Image(new File("E:/testpoi/mamat.jpg").toURI().toString) | |
width = image.getWidth | |
height = image.getHeight | |
val pixel = image.getPixelReader | |
var originX = 0.toDouble | |
var destinyX = 0.toDouble | |
var originY = 100. | |
var destinyY = 0.0 | |
val canvas = new Canvas(width + 50, height + 50) | |
val gc = canvas.getGraphicsContext2D | |
var shifty = 0. | |
var y = 0 | |
while (y < height) { | |
var x = 0 | |
while (x < width - step) { | |
val color = pixel.getColor(x, y) | |
val color1 = pixel.getColor(x + step, y) | |
val brightness = color.getBrightness | |
val brightness1 = color1.getBrightness | |
originX = x | |
destinyX = x + step | |
originY = -brightness * depth + brightness / 2 + shifty | |
destinyY = -brightness1 * depth + brightness1 / 2 + shifty | |
gc.setStroke(color) | |
rotateX(gc, -30) | |
gc.strokeLine(originX, originY, destinyX, destinyY) | |
originX = destinyX | |
originY = destinyY | |
x += step | |
} | |
shifty += 5 | |
y += step | |
} | |
root.setTranslateX(20) | |
root.setTranslateY(20) | |
root.getChildren.add(canvas) | |
root | |
} | |
def getLine(y: Double): Group = { | |
val root = new Group() | |
root | |
} | |
/* | |
Original Java: | |
package fjr.image; | |
import java.io.File; | |
import javafx.application.Application; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.image.PixelReader; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.LineTo; | |
import javafx.scene.shape.MoveTo; | |
import javafx.scene.shape.Path; | |
import javafx.scene.shape.PathBuilder; | |
import javafx.scene.shape.QuadCurve; | |
import javafx.scene.transform.Affine; | |
import javafx.scene.transform.Rotate; | |
import javafx.stage.Stage; | |
public class Rutte extends Application { | |
// static String main = Rutte.class.getResource("mamat.jpg").toExternalForm(); // alternatif | |
int step = 5; | |
int stepY = 50; | |
double depth = 56; | |
double width = 0 ; | |
double height = 0; | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Group root = getEsembel(); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.setTitle("TUT WURI HANDAYANI"); | |
primaryStage.show(); | |
} | |
public Group getEsembel() { | |
Group root = new Group(); | |
Image image = new Image(new File("E:/testpoi/mamat.jpg").toURI().toString()); | |
width = image.getWidth(); | |
height = image.getHeight(); | |
PixelReader pixel = image.getPixelReader(); | |
double originX = 0; | |
double destinyX = 0; | |
double originY = 100; | |
double destinyY = 0.0; | |
Canvas canvas = new Canvas(width+50,height+50); | |
GraphicsContext gc = canvas.getGraphicsContext2D(); | |
double shifty =0; | |
for(int y = 0 ; y < height; y+= step){ | |
for(int x = 0; x < width-step ; x+= step){ | |
Color color = pixel.getColor(x, y); | |
Color color1 = pixel.getColor(x+step, y); | |
double brightness = color.getBrightness(); | |
double brightness1 = color1.getBrightness(); | |
originX = x ; | |
destinyX = x+step; | |
originY = - brightness*depth+brightness/2 + shifty; | |
destinyY = - brightness1*depth+brightness1/2+ shifty; | |
gc.setStroke(color); | |
this.rotateX(gc, -30); | |
gc.strokeLine(originX,originY, destinyX, destinyY ); | |
originX = destinyX ; | |
originY = destinyY; | |
} | |
shifty+= 5; | |
} | |
root.setTranslateX(20); | |
root.setTranslateY(20); | |
root.getChildren().add(canvas); | |
return root; | |
} | |
|* | |
* I'm not found the way to rotate GraphicContext with simple way like other node | |
*| | |
static void rotateX(GraphicsContext gc , double angle){ | |
Affine affine = new Affine(); | |
double angleRadi = Math.toRadians(angle); | |
double sinus = Math.sin(angleRadi); | |
double cosinus = Math.cos(angleRadi); | |
affine.setMxx(1); | |
affine.setMxy(0); | |
affine.setMxz(0); | |
affine.setTx(0); | |
affine.setMyx(0); | |
affine.setMyy(cosinus); | |
affine.setMyz(-sinus); | |
affine.setTy(0); | |
affine.setMzx(0); | |
affine.setMzy(sinus); | |
affine.setMzz(cosinus); | |
affine.setTz(0); | |
gc.setTransform(affine); | |
} | |
static void rotateY(GraphicsContext gc , double angle){ | |
Affine affine = new Affine(); | |
double angleRadi = Math.toRadians(angle); | |
double sinus = Math.sin(angleRadi); | |
double cosinus = Math.cos(angleRadi); | |
affine.setMxx(cosinus); | |
affine.setMxy(0); | |
affine.setMxz(sinus); | |
affine.setTx(0); | |
affine.setMyx(0); | |
affine.setMyy(1); | |
affine.setMyz(0); | |
affine.setTy(0); | |
affine.setMzx(-sinus); | |
affine.setMzy(0); | |
affine.setMzz(-cosinus); | |
affine.setTz(0); | |
gc.setTransform(affine); | |
} | |
static void rotateZ(GraphicsContext gc , double angle){ | |
Affine affine = new Affine(); | |
double angleRadi = Math.toRadians(angle); | |
double sinus = Math.sin(angleRadi); | |
double cosinus = Math.cos(angleRadi); | |
affine.setMxx(cosinus); | |
affine.setMxy(-sinus); | |
affine.setMxz(0); | |
affine.setTx(0); | |
affine.setMyx(sinus); | |
affine.setMyy(cosinus); | |
affine.setMyz(0); | |
affine.setTy(0); | |
affine.setMzx(0); | |
affine.setMzy(0); | |
affine.setMzz(1); | |
affine.setTz(0); | |
gc.setTransform(affine); | |
} | |
public Group getLine(double y) { | |
Group root = new Group(); | |
return root; | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment