I have this code that creates a Java window that displays web pages, however I want the user to be able to go to other web pages with their own free will. So I was wondering if it is possible to put in an address bar so user can input a site address and go there, as well as putting in forward and backward buttons. Here is my Java code:
package stallionbrowser;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* #author seanguy4
*/
public class StallionBrowser extends Application {
private Scene scene;
#Override public void start(Stage stage) {
// create the scene
stage.setTitle("Web View");
scene = new Scene(new Browser(),750,500, Color.web("#666970"));
stage.setScene(scene);
scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
stage.show();
}
public static void main(String[] args){
launch(args);
}
}
class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
public Browser() {
//apply the styles
getStyleClass().add("browser");
// load the web page
webEngine.load("http://www.stallionware.weebly.com");
//add the web view to the scene
getChildren().add(browser);
}
private Node createSpacer() {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}
#Override protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
}
#Override protected double computePrefWidth(double height) {
return 750;
}
#Override protected double computePrefHeight(double width) {
return 500;
}
}
Related
I want a cursor that displays the coordinates of the place the mouse is pointing to, besides the pointer itself (it should look like a crosshair with little numbers is bottom right corner, that change when the mouse is moving). How to achive such effect efficiently? I tried to use the tooltip mechanism, but then the text lags behind the cursor a lot...
You could attach a label, and change the text inside the mouse moved event everytime that the mouse coordinates changes.
You can create a Text object with the current coordinates, snapshot it to an Image, and then create a WritableImage from that and any other cursor decoration you need. Then wrap the WritableImage in an ImageCursor:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.ImageCursor;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class MouseCoordinateCursor extends Application {
#Override
public void start(Stage primaryStage) {
Pane root = new Pane();
root.setOnMouseExited(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
root.setCursor(Cursor.DEFAULT);
}
});
root.setOnMouseMoved(new EventHandler<MouseEvent>() {
final int padding = 9 ;
final int offset = 6 ;
#Override
public void handle(MouseEvent e) {
Text text = new Text(String.format("[%.1f,%.1f]", e.getX(), e.getY()));
Image textImage = text.snapshot(null, null);
int width = (int)textImage.getWidth();
int height = (int)textImage.getHeight();
WritableImage cursorImage = new WritableImage(width + offset, height + offset);
cursorImage.getPixelWriter().setPixels(offset, offset, width, height, textImage.getPixelReader(), 0, 0);
for (int i = 0; i < padding; i++) {
cursorImage.getPixelWriter().setColor(i, padding/2, Color.BLACK);
cursorImage.getPixelWriter().setColor(padding/2, i, Color.BLACK);
}
root.setCursor(new ImageCursor(cursorImage));
}
});
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I have this programming working for the most part. The circle seems to only be able to move in a small area. Then it hits a border. Why is this? Do I need to make circlePane larger?
MoveTheCircle:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class MoveTheCircle extends Application {
private CirclePane circlePane = new CirclePane();
public void start(Stage primaryStage) {
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btLeft = new Button("Left");
Button btRight = new Button("Right");
Button btUp = new Button("Up");
Button btDown = new Button("Down");
hBox.getChildren().add(btLeft);
hBox.getChildren().add(btRight);
hBox.getChildren().add(btUp);
hBox.getChildren().add(btDown);
btLeft.setOnAction(e -> circlePane.left());
btRight.setOnAction(e -> circlePane.right());
btUp.setOnAction(e -> circlePane.up());
btDown.setOnAction(e -> circlePane.down());
circlePane.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
circlePane.left();
} else if (e.getButton() == MouseButton.PRIMARY) {
circlePane.right();
} else if (e.getButton() == MouseButton.PRIMARY) {
circlePane.up();
} else if (e.getButton() == MouseButton.PRIMARY) {
circlePane.down();
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(circlePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
Scene scene = new Scene(borderPane, 350, 250);
primaryStage.setTitle("Move The Ball");
primaryStage.setScene(scene);
primaryStage.show();
}
class CirclePane extends StackPane {
private Circle circle = new Circle(25);
public CirclePane() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public void left() {
circlePane.getTranslateX();
circlePane.setTranslateX(-10);
}
public void right() {
circlePane.getTranslateX();
circlePane.setTranslateX(+10);
}
public void up() {
circlePane.getTranslateY();
circlePane.setTranslateY(-10);
}
public void down() {
circlePane.getTranslateY();
circlePane.setTranslateY(+10);
}
}
public static void main(String[] args) {
launch(args);
}
}
Any and all help is appreciated.
You need to have
public void left() {
circle.setTranslateX(circle.getTranslateX()-10);
}
instead of
public void left() {
circle.getTranslateX();
circle.setTranslateX(-10);
}
Any similarly for all the other methods as well.
I create a new Stage in the Code and it's needed, that the Stage is undecorated! In this case the new created Stage loses the Drag-Function.
I created the following code, now i can drag the Stage, but the handling is incorrect and the Stage follows not in a correct way the Mouse.
I hope you can tell me my mistake and fix my error.
Thanks for your help.
EXAMPLE - CODE:
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class DragStuff extends Application{
VBox mainContainer;
Stage mainSt;
#Override
public void start(Stage mainStage) throws Exception {
// TODO Auto-generated method stub
mainStage.initStyle(StageStyle.UNDECORATED);
mainSt = mainStage;
mainContainer = new VBox();
mainContainer.setStyle("-fx-background-color:red");
Label headlineInformation = new Label("Testing");
headlineInformation.getStyleClass().addAll("popup-label-name");
headlineInformation.setMaxWidth(Double.MAX_VALUE);
Button closeButton = new Button("X");
closeButton.setVisible(true);
closeButton.getStyleClass().addAll("popup-button",
"popup-button-color");
HBox headContainer = new HBox();
HBox.setHgrow(headlineInformation, Priority.ALWAYS);
headContainer.setPadding(new Insets(5, 5, 5, 5));
headContainer.getChildren().addAll(headlineInformation, closeButton);
Pane pane = new Pane();
pane.getChildren().addAll(new Label("Text Stuff"));
mainContainer.getChildren().addAll(headContainer,pane);
Scene sc = new Scene(mainContainer);
mainStage.setScene(sc);
mainStage.show();
dragHandling();
}
public void dragHandling()
{
final ObjectProperty<Point2D> mouseKoordinates = new SimpleObjectProperty<>();
mainContainer.setOnMousePressed(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
mouseKoordinates.set(new Point2D(event.getSceneX(), event.getSceneY()));
mainContainer.getScene().setCursor(Cursor.HAND);
};
});
mainContainer.setOnMouseReleased(new EventHandler<MouseEvent>()
{
#Override
public void handle(final MouseEvent arg0)
{
mouseKoordinates.set(null);
mainContainer.getScene().setCursor(Cursor.DEFAULT);
}
});
mainContainer.setOnMouseDragged(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
if (mouseKoordinates.get() != null)
{
double x = event.getScreenX();
double deltaX = x - mouseKoordinates.get().getX();
double y = event.getScreenY();
double deltaY = y - mouseKoordinates.get().getY();
mainSt.setX(mainSt.getX() + deltaX);
mainSt.setY(mainSt.getY() + deltaY);
mouseKoordinates.set(new Point2D(x, y));
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}
You must not use getScreenX() and getScreenY() in your code as they return the absolute positions. In place of them try using getX() and getY().
Moreover, I don't understand why are you using mouseKoordinates.set(new Point2D(x, y)); as well.
Try using the following code
mainContainer.setOnMouseDragged(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
if (mouseKoordinates.get() != null)
{
double x = event.getX();
double deltaX = x - mouseKoordinates.get().getX();
double y = event.getY();
double deltaY = y - mouseKoordinates.get().getY();
mainSt.setX(mainSt.getX() + deltaX);
mainSt.setY(mainSt.getY() + deltaY);
}
}
});
I'm trying to create a JavaFx gui that displays 2 maps side by side. One map is of the city while the other is the same city's transportation system. The goal of this application is to do the following:
When the user clicks on a certain location in the the first map (Normal City Map) the second map (Trans Map) should be highlighted in the same exact location or corresponding location.
i have already created the interface where it displays both the same maps and was able to make the mouseEvent in order to extract the coordinates.
The question is, How can i make the 2nd map highlight the same location when clicked?
ِAlso, is it possible to add a Zoom Feature on the interface?
Here is the Code:
package hcimetromap;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
/**
*
* #author tharwat
*/
public class HciMetroMap extends Application {
final int xDiff = 675;
double x = 0;
double y = 0;
#Override public void start(Stage stage) throws Exception{
stage.setTitle("Baseline");
StackPane layout = new StackPane();
layout.getStylesheets().add("mainClass.css");
layout.getStyleClass().add("main-class");
Scene scene = new Scene(layout, 1200, 1200);
scene.getStylesheets().add(
getClass().getResource("mainClass.css").toExternalForm()
);
stage.setScene(scene);
stage.setResizable(false);
stage.centerOnScreen();
scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
//log mouse move to console, method listed below
x = me.getX();
y = me.getY();
System.out.println("Mouse moved, x: " +x +", y: " + y);
}
});
stage.show();
}
public static void main(String[] args) { launch(args); }
}
Associated Css:
.main-class {
-fx-background-image: url('TransMap.jpg'), url('map1.jpg');
-fx-background-repeat: stretch;
-fx-background-size: 50% 100%, 50% 100%;
-fx-background-position: right , left ;
-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
-fx-background-insets: 30;
}
I am trying to make a group which contains Rectangle and Text objects to scale up and down using the ScrollEvent. I am having trouble getting the correct scale to use from the delta of the scroll. The following code is what I have at the moment. Any help will be appreciated.
Thanks
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
// demonstrates scaling a test pane with content in it.
public class ScaleTest extends Application {
Group page;
double textScale =1.0;
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("scale test");
Group root = new Group();
page= new Group();
Rectangle r = new Rectangle(300,300);
Text t = new Text("asodjoijdjiasjdoijaisjdijasijdiajsidjoiasjiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs");
t.setFill(Color.FLORALWHITE);
t.setWrappingWidth(280);
t.setX(10);
t.setY(20);
page.getChildren().addAll(r,t);
root.getChildren().add(page);
Scene scene = new Scene(root,800,600);
this.setSceneEvents(scene);
stage.setScene(scene);
stage.show();
}
private void setSceneEvents(final Scene scene) {
//handles mouse scrolling
scene.setOnScroll(
new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
double xscale = page.getScaleX() * event.getDeltaY()/35;
double yscale= page.getScaleY() * event.getDeltaY()/35;
System.out.println("xscale: "+ xscale);
System.out.println("yscale: "+ yscale);
//page.setScaleX(page.getScaleX() * event.getDeltaY()/35);
// page.setScaleY(page.getScaleY() * event.getDeltaY()/35);
event.consume();
}
});
}
}
|deltaY| is for all scroll events the same. Also note that deltaY is negative for scrolling down, and the scaling always is normalized at 1.0. So i suggest to use a "zoom factor" which determines the scaling. Try something like:
private void setSceneEvents(final Scene scene) {
//handles mouse scrolling
scene.setOnScroll(
new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
double zoomFactor = 1.05;
double deltaY = event.getDeltaY();
if (deltaY < 0){
zoomFactor = 2.0 - zoomFactor;
}
System.out.println(zoomFactor);
page.setScaleX(page.getScaleX() * zoomFactor);
page.setScaleY(page.getScaleY() * zoomFactor);
event.consume();
}
});
}