Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Dragging Undercoated Window Causes Glitching

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Dragging Undercoated Window Causes Glitching

    Hey could use a little help on a Post-It-Note project I'm just finishing up. It's a small program that only has one other class than Main.

    The PostIt class creates a new Undercoated JavaFX Stage and calls stage.show() on creation, making a new post-it. A button in the main class creates objects from this PostIt class and adds them to an ArrayList also in the Main class.

    As the window is Undercoated I needed a way to drag the PostIt windows around and I found some code online. This code is commented with "***" in the PostIt class.

    So the problem is it dose kind of work... but it mainly just glitches the window around the screen as you try to drag it to a new position. I have no I idea why this is happening...

    Main Class:
    package com.company;
     
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.control.ComboBox;
    import javafx.stage.Stage;
    import javafx.scene.control.Button;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.Scene;
     
    import java.util.*;
     
    public class Main extends Application{
     
        //Cpntains PostIt Objects used to create new note window
        ArrayList<PostIt> notes = new ArrayList<PostIt>();
     
        public static void main(String [] args){
            launch(args);
        }
     
        @Override
        public void start(Stage primaryStage) throws Exception {
     
            primaryStage.setTitle("Winodw Title!!!");
     
            //Button for creating new note and adding it to notes array
            Button b1 = new Button();
            b1.setText("New Note");
     
            //drop down box for selecting color of nate
            ComboBox comboBox = new ComboBox();
            comboBox.getItems().addAll(
                    "Random",
                    "Blue",
                    "Green",
                    "Purple",
                    "Yellow"
            );
            comboBox.getSelectionModel().selectFirst();
     
            //Make new note logic
            b1.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent e) {
                    //holds new note
                    PostIt newpost = null;
     
                    //note id
                    int id = 0;
     
                    //get color from combobox
                    String color;
                    color = (String) comboBox.getValue();
                    //System.out.println(color);
     
                    //checks notes array length to calculate ID of new note
                    if (notes.size() != 0){
                        int temp = notes.size() - 1;
                        id = notes.get(temp).getId() + 1;
                    }
     
                    try {
                        newpost = new PostIt(primaryStage, id, color);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
     
                    //add note to notes list
                    notes.add(newpost);
                }
            });
     
            //layout and stage
            AnchorPane layout = new AnchorPane();
            layout.getChildren().add(b1);
            layout.getChildren().add(comboBox);
            AnchorPane.setTopAnchor(comboBox, 40.0);
     
            Scene scene = new Scene(layout, 300, 250);
            primaryStage.setScene(scene);
            primaryStage.show();
     
        }
     
     
     
    }

    PostIt Class:
    package com.company;
     
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.TextArea;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Font;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;
     
    import java.util.Random;
     
     
    public class PostIt extends Application {
        //post-it stage
        Stage stage;
        //ID
        int id;
     
        //used for drag window logic
        Double xOffset = 0.00;
        Double yOffset = 0.00;
     
        //color logic veriables
        String color;
        String hexcolor1;
        String hexcolor2;
     
        public PostIt(Stage primaryStage, int id, String color) throws Exception {
            //set ID and color
            this.id = id;
            this.color = color;
     
            //if color is random - roll and set new color
            if (this.color.equals("Random")){
                int rand = getRandomNumberInRange(1,4);
                System.out.println(rand);
                if (rand == 1) this.color = "Blue";
                else if (rand == 2) this.color = "Green";
                else if (rand == 3) this.color = "Purple";
                else if (rand == 4) this.color = "Yellow";
            }
     
            //set color values
            if (this.color.equals("Blue")){
                hexcolor1 = "#73ADEC";
                hexcolor2 = "#A0CCF9";
            }
            else if (this.color.equals("Green")){
                hexcolor1 = "#C6E951";
                hexcolor2 = "#DDF681";
            }
            else if (this.color.equals("Purple")){
                hexcolor1 = "#E86BCF";
                hexcolor2 = "#FA94E8";
            }
            else if (this.color.equals("Yellow")){
                hexcolor1 = "#F0CC40";
                hexcolor2 = "#FAE278";
            }
     
            //start stage
            stage = new Stage();
            stage.initOwner(primaryStage);
            start(stage);
     
        }
     
        @Override
        public void start(Stage stage) throws Exception {
            stage.initStyle(StageStyle.UNDECORATED);
     
            //fix stage size
            stage.setMaxHeight(400);
            stage.setMaxWidth(400);
            stage.setMinHeight(400);
            stage.setMinWidth(400);
     
            stage.setResizable(false);
     
            //create textbox and set color - hexcolor2
            TextArea textArea = new TextArea();
            textArea.setFont(Font.font("Comic Sans MS", 22));
            textArea.setWrapText(true);
            textArea.setStyle("-fx-focus-color: transparent; -fx-text-box-border: transparent;");
            textArea.setStyle("-fx-background-color: green");
            textArea.setStyle("-fx-control-inner-background:" + hexcolor2 + ";");
     
            //Set layout and color - hexcolor1
            AnchorPane pane = new AnchorPane(textArea);
            pane.styleProperty().set("-fx-background-color: " + hexcolor1);
     
            //set textArea location
            AnchorPane.setTopAnchor(textArea, 80.0);
            AnchorPane.setLeftAnchor(textArea, 0.0);
     
     
            //*****************Code for dragging window*****************
            pane.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                        xOffset = stage.getX() - event.getSceneX();
                        yOffset = stage.getY() - event.getScreenY();
     
                }
            });
     
            pane.setOnMouseDragged(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
     
                        stage.setX(event.getSceneX() - xOffset);
                        stage.setY(event.getSceneY() - yOffset);
     
                }
            });
            //*****************End of code for Dragging window*****************
     
            Scene scene = new Scene(pane, 400, 400, Color.BLACK);
     
            stage.setScene(scene);
            stage.show();
            //System.out.println("" + getId());
        }
     
        public int getId() {
            return id;
        }
     
        public void setId(int id) {
            this.id = id;
        }
     
        private static int getRandomNumberInRange(int min, int max) {
     
            if (min >= max) {
                throw new IllegalArgumentException("max must be greater than min");
            }
     
            Random r = new Random();
            return r.nextInt((max - min) + 1) + min;
        }
    }


    --- Update ---

    Fixed it. I was using getSceanX/Y and not getScreenX/Y. Also there was a small mistake in the math.

    pane.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                        xOffset = event.getScreenX() - stage.getX();
                        yOffset = event.getScreenY() - stage.getY();
     
                        System.out.println(xOffset);
                        System.out.println(yOffset);
     
                }
            });
     
            pane.setOnMouseDragged(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
     
                        stage.setX(event.getScreenX() - xOffset);
                        stage.setY(event.getScreenY() - yOffset);
     
                }
            });
    Last edited by Blick; March 19th, 2020 at 04:21 PM.

Similar Threads

  1. Applet viewer window is diplaying in fron of the current window every time
    By jsreddy99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 24th, 2013, 07:16 AM
  2. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM
  3. Not dragging and dropping as expected
    By Josheh in forum AWT / Java Swing
    Replies: 0
    Last Post: October 7th, 2011, 04:30 PM
  4. Dragging and Dropping Rectangle - GUI
    By cjct927 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 8th, 2011, 09:59 AM
  5. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM