Tugas 8 PBO - Applet & JavaFX
Muhammad Izzuddin Al Islami
05111740000035
Contoh program Hello World dalam applet java:
Contoh program Fortune Telling dalam JavaFX:
05111740000035
Applet dan JavaFX
Applet
Applet adalah sebuah tipe program yang dapat diletakkan dalam sebuah webpage sebagai dynamic content. Applet akan dijalankan oleh browser dan bekerja pada sisi client.Contoh program Hello World dalam applet java:
import java.awt.*;
import javax.swing.*;
/**
* Class HelloWorld - write a description of the class here
*
* @author Izzud
* @version v0.Hello
*/
public class TesApplet extends java.applet.Applet {
public void paint(Graphics g){
int fontSize = 20;
Font f = new Font("SansSerif", Font.BOLD, fontSize);
g.setFont(f);
g.setColor(Color.BLACK);
int xCent = this.getSize().width/2;
int yCent = this.getSize().height/2;
String str = "Hello Java Applet";
FontMetrics fm = this.getFontMetrics(f);
int wordXPos = xCent - (fm.stringWidth(str)/2);
g.drawString(str, wordXPos, yCent);
}
}
Output
JavaFX
JavaFX adalah sebuah platform software untuk membangun sebuah aplikasi rich internet application (RIA) yang bisa berjalan pada berbagai macam perangkat. Seperti komputer dekstop, web browser di Windows, Linux dan Mac OSX.Contoh program Fortune Telling dalam JavaFX:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.Random;
public class TellFortune extends Application {
Text fortune = new Text("");
String[] fortunes = {"It's going to rain tomorrow","You'll meet your special someone",
"You'll be drowned in happiness.. soon",
"Your fortune is in another day"};
@Override
public void start(Stage stage) throws Exception {
VBox box = new VBox();
box.setPadding(new Insets(20));
box.setSpacing(20);
box.setAlignment(Pos.CENTER);
Text title = new Text("Fortune Teller");
title.setFont(Font.font("SanSerif", 36));
box.getChildren().add(title);
fortune.setFont(Font.font("SanSerif", 18));
box.getChildren().add(fortune);
Button button = new Button("New Fortune");
box.getChildren().add(button);
button.setOnAction(this::buttonClick);
Scene scene = new Scene(box, 500, 250);
stage.setTitle("Fortune Teller");
stage.setScene(scene);
stage.show();
}
private void buttonClick(ActionEvent event) {
Random rand = new Random();
fortune.setText(fortunes[rand.nextInt(fortunes.length)]);
}
}
Comments
Post a Comment