When trying to include an external class that extends Pane in a main class in JavaFX, simply adding it may not achieve the

My primary class is as follows:

public class Digital_Analog_Clock_Beta_1 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        double outerBoxWidth = 500, outerBoxHeight = outerBoxWidth / 2.5;
        Rectangle outerBox = new Rectangle(0, 0, outerBoxWidth, outerBoxHeight);
        outerBox.setId("outer-box");

        Rectangle part1 = new Rectangle(0, 0, outerBoxWidth * .35, outerBoxHeight);
        part1.setId("partition");
        Rectangle part2 = new Rectangle(outerBoxWidth * .35, 0, outerBoxWidth * .05, outerBoxHeight);
        part2.setId("partition-alternate");
        Rectangle part3 = new Rectangle(outerBoxWidth * .4, 0, outerBoxWidth * .35, outerBoxHeight);
        part3.setId("partition");
        Rectangle part4 = new Rectangle(outerBoxWidth * .75, 0, outerBoxWidth * .35, outerBoxHeight);
        part4.setId("partition-alternate");

        double bigNumWidth = outerBoxWidth * .35;
        double digitWidth = (.9 * bigNumWidth / 2) * 0.95;
        double digitHeight = .9*outerBoxHeight;
        
                
        Digit Digit1 = new Digit(outerBoxWidth*.1,outerBoxHeight*.1,digitWidth,digitHeight);
        Digit1.bottom.setId("digits");
        Digit1.c.setFill(Color.AQUA);

        Pane pane = new Pane();
        pane.getChildren().add(outerBox);
        pane.getChildren().addAll(part1, part2, part3, part4);
        //pane.setId("background");
        
        pane.getChildren().add(Digit1);

        Scene scene = new Scene(pane, outerBoxWidth, outerBoxHeight);
        scene.getStylesheets().add(getClass().getResource("styleSheet.css").toExternalForm());

        primaryStage.setTitle("Digital Analog Clock Beta 1");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }

}

Below is my Digit class :

public class Digit extends Pane
{

    double startX, startY, digitWidth, digitHeight, lineWidth;

    public Digit(double startX, double startY, double digitWidth, double digitHeight)
    {
        this.startX = startX;
        this.startY = startY;
        this.digitWidth = digitWidth;
        this.digitHeight = digitHeight;
        lineWidth = digitHeight / 20;
        getChildren().addAll(top,middle,bottom,upperLeft,upperRight,lowerLeft,lowerRight, c);
    }

    Polygon top = new Polygon(startX, startY, startX + digitWidth, startY, startX + digitWidth - lineWidth, startY + lineWidth * .95, startX + lineWidth, startY + lineWidth * .95);
    Polygon middle = new Polygon(startX + lineWidth, startY + digitHeight / 2 - lineWidth / 2 + lineWidth * .05,
            startX + digitWidth - lineWidth, startY + digitHeight / 2 - lineWidth / 2 + lineWidth * .05,
            startX + digitWidth - lineWidth * .05, startY + digitHeight, startX + digitWidth - lineWidth, startY + digitHeight / 2 + lineWidth / 2 - lineWidth * .05,
            startX + lineWidth, startY + digitHeight / 2 + lineWidth / 2 - lineWidth * .05, startX + lineWidth * .05, startY + digitHeight / 2);
    Polygon bottom = new Polygon(startX, startY + digitHeight, startX + digitWidth, startY + digitHeight, startX + digitWidth - lineWidth, startY + digitHeight - lineWidth * .95,
            startX + lineWidth, startY + digitHeight - lineWidth * .95);
    Polygon upperLeft = new Polygon(startX, startY, startX + lineWidth * .95, startY + lineWidth, startX + lineWidth * .95, startY + digitHeight / 2 - lineWidth / 2, startX, startY + digitHeight / 2);
    Polygon lowerLeft = new Polygon(startX, startY + digitHeight / 2, startX + lineWidth * .95, startY + digitHeight / 2 + lineWidth / 2, startX + lineWidth * .95, startY + digitHeight - lineWidth,
            startX, startY + digitHeight);
    Polygon upperRight = new Polygon(startX + digitWidth, startY, startX + digitWidth, startY + digitHeight / 2, startX + digitWidth - lineWidth * .95, startY + digitHeight / 2 - lineWidth / 2,
            startX + digitWidth - lineWidth * .95, startY + lineWidth);
    Polygon lowerRight = new Polygon(startX+digitWidth,startY+digitHeight/2,startX+digitWidth,startY+digitHeight,startX+digitWidth-lineWidth*.95,startY+digitHeight-lineWidth,
            startX+digitWidth-lineWidth*.95,startY+lineWidth/2+digitHeight/2);
    
   Circle c = new Circle(100.0f,100.0f,1000.0f);
    
}

And here's the styleSheet.css :

#outer-box
{
     -fx-fill: #353839; /* Onyx */
}

#outer-box-t
{
    -fx-fill: rgba(0,0,0,0); /* transparent */
}

#partition
{
    -fx-fill: rgba(200,200,205,0.25)  /* #C8C8CD Blue Grey a : 0.5*/
}

#partition-alternate
{
    -fx-fill: rgba(0,0,0,0); /* transparent i.e. Onyx */
}

#background
{
    -fx-background-color: #C53151; /* Dingy Dungeon */
}

#digits
{
    -fx-fill : #66FF66; /* Screamin' Green */   
     -fx-stroke: #C53151; /* Dingy Dungeon */
    -fx-stroke-width : 3;
}

Output window can be seen https://i.sstatic.net/VEh2E.jpg

The digits are meant to appear over the background I have created. The numerous rectangles in the main class serve the purpose of spacing out the digits accordingly. Although I added polygons and even a circle at the bottom of my Digit.java class, they do not show up in the output window. Having asked a similar question on Stack Overflow previously, it worked for a test case but not in this actual program.

Update: Everything except for the circle is missing from the output window.

Answer №1

When initializing instance variables inline, they are set before the constructor is called. For example, when initializing top, all other variables like startx, starty, digitWidth, digitHeight, and lineWidth will be set to zero.

For more information refer to JLS, section 12.5:

Before returning the newly created object, the constructor goes through a process to initialize it:

  1. Assign arguments of the constructor to parameter variables.

  2. If there is an explicit invocation of another constructor using this, evaluate the arguments and repeat these steps recursively. If the recursive invocation fails, the procedure stops; otherwise, continue to step 5.

  3. If there is no explicit invocation using this in the constructor for a non-Object class, invoke a superclass constructor using super and repeat these steps recursively. If the invocation fails, stop the procedure with an exception. Otherwise, move to step 4.

  4. Execute instance initializers and variable initializers for the class, setting the values of initializers to corresponding instance variables in order of appearance in source code. Any exceptions while executing initializers halt the process. Move on to step 5 if successful.

  5. Finish executing the rest of the constructor's body. If there are any abrupt terminations, the procedure halts accordingly; otherwise, completes normally.

Note that assigning instance variable initializers precedes executing the constructor body.

To improve organization, move polygon initialization to the constructor:

public class digit extends Pane {

    double startX, startY, digitWidth, digitHeight, lineWidth;

    Polygon top ;
    Polygon middle ;
    Polygon bottom ;
    Polygon upperLeft ;
    Polygon upperRight ;
    Polygon upperLeft ;
    Polygon lowerLeft ;

    public digit(double startX, double startY, double digitWidth, double digitHeight)
    {
        this.startX = startX;
        this.startY = startY;
        this.digitWidth = digitWidth;
        this.digitHeight = digitHeight;
        lineWidth = digitHeight / 20;
        getChildren().addAll(top,middle,bottom,upperLeft,upperRight,lowerLeft,lowerRight, c);
        this.top = new Polygon(startX, startY, startX + digitWidth, startY, startX + digitWidth - lineWidth, startY + lineWidth * .95, startX + lineWidth, startY + lineWidth * .95);

        // etc...
    }

}

    
  

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determining the dimensions of form elements in an inline layout using Bootstrap 3.1

Currently in the process of upgrading my application from version 3.0 to 3.1. The application is still in its initial phase, resembling more of a prototype with only a few pages created. Therefore, I didn't anticipate encountering many issues during t ...

A div set to absolute positioning and a width of 100% will exceed the boundaries of a Bootstrap 4

I am attempting to position a div with 100 percent width within a Bootstrap 4.1 col using absolute positioning in order to attach it to the bottom. However, I am encountering an issue where it overflows on the right side of the screen. I have tried assigni ...

Is it advantageous to employ several wrapper-divs inside section elements when working with HTML5 and CSS?

Back in the day, I learned how to create a website by enclosing all content below the body tag within a div with the class "wrapper." This approach made sense as it allowed for easy vertical and horizontal alignment of the entire content. Just yesterday, ...

Styling text on top of an image using CSS

I am attempting to overlay text on top of an image using the following code: .gallery-image { position: relative; } h2 { position: absolute; top: 200px; left: 0; width: 100%; } h2 span { color: white; font: bold 24px/45px Helvetica, Sans-S ...

Steps to show submenus upon hovering over the main menu items

I am trying to create a vertical menu with multiple levels using HTML and CSS. Here is the code I have written so far: <ul> <li>Level 1 menu <ul> <li>Level 2 item</li> <li>Level 2 item</li&g ...

Backgrounds filled by nested <li> elements inside another <li> element

My website features a sidebar menu with nested lists. When hovering over a list item, a sub-list appears. However, I am having an issue where the background color fills the entire sub-list instead of just the first list item. See the images below for clari ...

CSS: Revert INPUT Element Back to Default Width

Although it may seem impossible, I am still going to ask the question. Suppose I have the following HTML: <div style="width: 500px;"> <input class="full" /> </div> And the corresponding CSS: input { width: 100%; } .full { ...

Create widget that is resistant to inherited traits

In our development process, we are crafting a widget designed to be seamlessly integrated into external websites. This integration involves the dynamic injection of a div element within the body of the page using Javascript. While the widget appears satis ...

Find elements within an array that include a specific string

I've got several URLs listed below Reach out What's the best way to extract only "www.example.com" ? Can this be done easily with jsoup? I have both relative and absolute URLs and I only need to extract them without the tags. ...

Transforming JSON Structure into BasicDBObject

Looking to transform the JSON structure shown below into BasicDBObject in Java and then insert it into MongoDB. The JSON structure is as follows: { "it": { "batch": "2013", "students": [ { "name": "joe" ...

Tips for adding color to the <td> element in an ejs file using nodeJS

I am looking to incorporate a color into my .ejs file. Here is the code snippet I am working with: <% resultList.forEach(function(item, index){ %> <tr> <td><%= item.function %></td> <td><%= item.value %>< ...

Struggling to align the push menu properly within the Bootstrap framework

I am currently utilizing Bootstrap as my UI framework and attempting to create a push menu on the left side of the page. While I have made progress towards achieving this goal, there are some bugs in the system that I am encountering. Specifically, I am ha ...

Uncaught exceptions and throwables

I am currently executing Selenium tests and I want to ensure that the thread remains active throughout by catching any exceptions thrown. Below is an outline of my program: Main.java for (int i = 0; i < numberOfThreads; i++) { System.out.println(" ...

Is there a way to transform this Java code into Node.js syntax?

I'm struggling to translate this Java code into Node.js: int data = Float.floatToIntBits(4.2); sendCommand(0x50, data); public void sendCommand(byte type, int data) { byte[] cmd = new byte[FRAME_LENGTH]; cmd[0] = type; cmd[1] = (byte)(data); ...

Issues with PHP Form Email DeliveryIt seems that there is a

I currently have a ready-made form that is constructed using html, css, jquery, and ajax. It's basically copied and pasted onto my contact page for now. This is the code I include before the html tag on the contact.php page. <?php session_name(" ...

What could be causing the java.lang.NullPointerException to occur in my code?

I am encountering a java.lang.NullPointerException error when trying to execute objUserName.sendKeys(uname); @FindBy(how=How.XPATH,using="//input[@placeholder='Username']") static WebElement objUserName; public LoginFeature(){ PageFactory.i ...

Expand the div to fit the rest of the screen following the fixed-size navigation bar

I am looking to create a webpage layout that includes a header, mid section, and footer. In the mid section, I want to have a fixed width navigation bar of 250px, with another div holding the content that stretches to fill the remaining space in the browse ...

Manipulate the timing of css animations using javascript

I am currently working with a progress bar that I need to manipulate using JavaScript. The demo of the progress bar has a smooth animation, but when I try to adjust its width using jQuery $($0).css({'width': '80%'}), the animation disap ...

Is it feasible to block indent the second line of a URL?

Is there a way to indent a second line of a URL so that it aligns perfectly with the first letter of the sentence? I attempted using <ul>, but encountered issues as it inherited small text and bullet points from the main .css file. Check out this li ...

Error: FileUriExposedException encountered while attempting to share a screenshot of a layout

Hello, I am encountering an issue when trying to share a screenshot of a layout. The sharing function works fine on versions earlier than Oreo, but on Oreo it is throwing a FileUriExposedException error. If anyone has insights on how to resolve this proble ...