HMExample.java
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 |
import java.util.HashMap; class Student { private int id; private String name; private String address; public Student(int id, String name, String address){ this.id = id; this.name = name; this.address = address; } public String getName(){ return name; } public String getAddress(){ return address; } public String toString(){ return name + " (" + id + ") lives at " + address; } } public class HMExample { private static HashMap<int, Student> idStudentMap = new HashMap<>(); private static void printStudentSummary(Integer id){ if(idStudentMap.get(id) == null) System.out.println("No such student with id: " + id); else System.out.println(idStudentMap.get(id)); } public static void main(String[] args){ Student s1 = new Student(800, "John Smith", "24 Main St."); Student s2 = new Student(801, "Jane Doe", "221 Columbus St."); Student s3 = new Student(802, "Betty Allen", "425 E. Second St."); idStudentMap.put(800, s1); idStudentMap.put(801, s2); idStudentMap.put(802, s3); printStudentSummary(801); } } |
SceneDrawing.java
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 |
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Ellipse; import javafx.scene.shape.Polygon; import javafx.scene.shape.Line; import javafx.scene.layout.StackPane; import javafx.scene.text.Text; import javafx.scene.text.TextBoundsType; import javafx.stage.Stage; public class SceneDrawing extends Application { public void addNode(double x, double y, String t, Group g){ Text text = new Text(t); Ellipse ellipse = new Ellipse(50, 20); ellipse.setStroke(Color.FORESTGREEN); ellipse.setFill(Color.AZURE); text.setBoundsType(TextBoundsType.VISUAL); StackPane stack = new StackPane(); stack.getChildren().addAll(ellipse, text); stack.relocate(x, y); g.getChildren().add(stack); } public void addEdge(double sourceX, double sourceY, double targetX, double targetY, Group g){ Line line = new Line(sourceX, sourceY, targetX, targetY); g.getChildren().add(line); } @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 500, Color.LIGHTGREY); addEdge(150, 125, 350, 325, root); addNode(100, 100, "node1", root); addNode(300, 300, "node2", root); stage.setTitle("JavaFX Scene Graph Demo"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } |