Java Applet & Java FX
Berikut ini adalah contoh pembuatan java applet:
source code java applet:
Berikut ini adalah tampilan java applet:
Project Scheme:
Contoh aplikasi dengan java applet (fortune teller)
Source Code fortune teller:
Tampilan Fortune Teller
Klik "New Fortune"
source code java applet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.awt.*; public class TesApplet extends java.applet.Applet{ public void paint(Graphics g){ Font f = new Font("SansSerif", Font.BOLD, 20); g.setFont(f); g.setColor(Color.BLUE); int xPusat = this.getSize().width/2; int yPusat = this.getSize().height/2; String s = "Selamat Belajar Java Applet"; FontMetrics fm = this.getFontMetrics(f); int posisiX = xPusat - (fm.stringWidth(s)/2); g.drawString("Selamat Belajar java Applet", posisiX, yPusat); } } |
Berikut ini adalah tampilan java applet:
Project Scheme:
Start Program
Contoh aplikasi dengan java applet (fortune teller)
Source Code fortune teller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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; /** * JavaFX application that tells your fortune. * * @author Aaron Astonvilla * @version 1.0 */ public class FortuneTeller extends Application { Text fortune = new Text(""); String[] fortunes = {"masa depan anda cerah", "masa depan anda suram", "anda akan jadi orang baik yang kaya raya","anda akan mengalami kesulitan selama 2 detik",}; @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)]); } } |
Tampilan Fortune Teller
Klik "New Fortune"
Komentar
Posting Komentar