Create an analogue clock interface in Java using JavaFX

The app when works fine, but people are saying that the code is not written with good code style and following OOP principles.

Can I improve on any parts of my code? Especially regarding:

  • Code comments
  • Variable naming
  • Separation of concerns
  • Single responsibility principle
  • etc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.util.Duration;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.application.Platform;

public class AnalogClockv2 extends Application {
  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {
    ClockPane clock = new ClockPane(); // Create a clock

    HBox hBox = new HBox(5);
    Button btStop = new Button("Stop");
    Button btStart = new Button("Start");
    hBox.getChildren().addAll(btStop, btStart);
    hBox.setAlignment(Pos.CENTER);

    BorderPane pane = new BorderPane();
    pane.setCenter(clock);
    pane.setBottom(hBox);

    // Create a scene and place it in the stage
    Scene scene = new Scene(pane, 250, 300);
    primaryStage.setTitle("Java Analog Clock Reloaded"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage


    clock.widthProperty().addListener(ov -> {
      clock.setW(pane.getWidth());
    });

    clock.heightProperty().addListener(ov -> {
      clock.setH(pane.getHeight());
    });
  }

  /**
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
   */
  public static void main(String() args) {
    launch(args);
  }

  class ClockPane extends Pane {
    private int hour;
    private int minute;
    private int second;

    // Clock pane's width and height
    private double w = 250, h = 250;

    private int sleepTime = 50;
    /**
     * Construct a default clock with the current time
     */
    public ClockPane() {
      setCurrentTime();

    }

    /**
     * Construct a clock with specified hour, minute, and second
     */
    public ClockPane(int hour, int minute, int second) {
      this.hour = hour;
      this.minute = minute;
      this.second = second;
      paintClock();
    }

    /**
     * Return hour
     */
    public int getHour() {
      return hour;
    }

    /**
     * Set a new hour
     */
    public void setHour(int hour) {
      this.hour = hour;
      paintClock();
    }

    /**
     * Return minute
     */
    public int getMinute() {
      return minute;
    }

    /**
     * Set a new minute
     */
    public void setMinute(int minute) {
      this.minute = minute;
      paintClock();
    }

    /**
     * Return second
     */
    public int getSecond() {
      return second;
    }

    /**
     * Set a new second
     */
    public void setSecond(int second) {
      this.second = second;
      paintClock();
    }

    /**
     * Return clock pane's width
     */
    public double getW() {
      return w;
    }

    /**
     * Set clock pane's width
     */
    public void setW(double w) {
      this.w = w;
      paintClock();
    }

    /**
     * Return clock pane's height
     */
    public double getH() {
      return h;
    }

    /**
     * Set clock pane's height
     */
    public void setH(double h) {
      this.h = h;
      paintClock();
    }

    /* Set the current time for the clock */
    public void setCurrentTime() {
      // Construct a calendar for the current date and time
      Calendar calendar = new GregorianCalendar();

      // Set current hour, minute and second
      this.hour = calendar.get(Calendar.HOUR_OF_DAY);
      this.minute = calendar.get(Calendar.MINUTE);
      this.second = calendar.get(Calendar.SECOND);

      paintClock(); // Repaint the clock
    }

    /**
     * Paint the clock
     */
    private void paintClock() {
      // Initialize clock parameters
      double clockRadius = Math.min(w, h) * 0.8 * 0.5;
      double centerX = w / 2;
      double centerY = h / 2;

      // Draw circle
      Circle circle = new Circle(centerX, centerY, clockRadius);
      circle.setFill(Color.WHITE);
      circle.setStroke(Color.BLACK);
      Text t1 = new Text(centerX - 5, centerY - clockRadius + 12, "12");
      Text t2 = new Text(centerX - clockRadius + 3, centerY + 5, "9");
      Text t3 = new Text(centerX + clockRadius - 10, centerY + 3, "3");
      Text t4 = new Text(centerX - 3, centerY + clockRadius - 3, "6");

      // Draw second hand
      double sLength = clockRadius * 0.8;
      double secondX = centerX + sLength
              * Math.sin(second * (2 * Math.PI / 60));
      double secondY = centerY - sLength
              * Math.cos(second * (2 * Math.PI / 60));
      Line sLine = new Line(centerX, centerY, secondX, secondY);
      sLine.setStroke(Color.RED);

      // Draw minute hand
      double mLength = clockRadius * 0.65;
      double xMinute = centerX + mLength
              * Math.sin(minute * (2 * Math.PI / 60));
      double minuteY = centerY - mLength
              * Math.cos(minute * (2 * Math.PI / 60));
      Line mLine = new Line(centerX, centerY, xMinute, minuteY);
      mLine.setStroke(Color.BLUE);

      // Draw hour hand
      double hLength = clockRadius * 0.5;
      double hourX = centerX + hLength
              * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
      double hourY = centerY - hLength
              * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
      Line hLine = new Line(centerX, centerY, hourX, hourY);
      hLine.setStroke(Color.GREEN);

      getChildren().clear();
      getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
    }
  }
}

For more context, the code is used to teach the fundamentals of OOP, how to use Java, and JavaFX in a high school CS course.

Please ask for clarification if there are issues running the code if needed, this is my first time on this site. Thanks!

linux – Iptables Forward All traffic to different interface except local inter-lan

I have Two LAN

Local LAN: 172.30.30.0/24 with gw 172.30.30.254

Remote LAN : 192.168.2.0/24 with gw 192.168.2.1

both lan can communicate and ping each other devices.

i have introduce new machine 172.30.30.10 and set all 172.30.30.0/24 devices gateway to this ip.

I need to to forward all traffic to 172.30.30.10’s eth0 >>> tun0 interface except there local inter-lan communication.

So what i have done till now is

    echo 1 > /proc/sys/net/ipv4/ip_forward
    iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
    iptables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
    ip route add -net 192.168.2.0/24 gw 172.30.30.254 dev eth0 metric 101

So after this i am able to ping from 172.30.30.0/24 network to 192.168.2.0/24 and vice versa

but when the tun0 interface create after connecting to the vpn only one way ping happen 172.30.30.0/24 to 192.168.2.0/24 NOT vice versa.

What else can i do so that it can ping each other network even after connecting to vpn(tun0 interface)?

How to look for Spring’s Framework @interface implementation?

My goal is to understand how @MessageMapping("/hotels/{hotel}") and @DestinationVariable("hotel") works. I wish to implement the matcher somewhere else.

I use free IntelliJ IDEA on Windows, What I’ve tried:

  1. CTRL + B over @MessageMapping leads me to org.springframework.messaging.handler.annotation.MessageMapping
    1. This leads me to org.springframework.util.AntPathMatcher.

However the source code is complex. I am not sure which part of the code that is used by @MessageMapping and @DestinationVariable.

Please feel free to leave any guidance on how to use IntelliJ IDEA to answer this question.

networking – HP ComWare – Detecting mac/hardware address of CONNECTED interface

Alright, so I’m adding an IRF member to our switch stack.
They are 5130’s, JG937A.

Only problem is the IRF cables are BURIED in the rack channel (I mean buried buried), and there’s no way for me to look at the switches and tell which 10GBX interfaces are connected to which, thus I can’t tell how the switches are stacked (daisy chain, ring, mesh etc).

So, wondering if I can do this by viewing the mac addresses of the interfaces, and seeing where they match up.

Question- is there a way to see the mac address of the CONNECTED SWITCH INTERFACE (on the other end of the cable)

For instance, I can view the mac of an interface by using

show current-configuration interface Ten-Gigabitethernet 4/0/49

I would then want to view the mac address of whatever interface is connected to 4/0/49 (probably 5/0/50).

Any other way to confirm this? To figure out how my switches are stacked, and which interfaces currently connect to which?

ubuntu – added vlan interface, main host not reachable

I have a VM server running Ubuntu on the 192.168.1.0/24 network. I am trying to add a vlan tagged subinterface that will be used for a VM that will be in a DMZ on my network. I added the subinterface first with sudo vconfig add enp2s0 30 then with the following in the /etc/network/interfaces config

auto enp2s0.30
iface enp2s0.30 inet dhcp
    vlan-raw-device enp1s0

After resetting the network service the interface comes up and gets an IP address. However, now I can’t reach any of the VMs running on the parent interface enp2s0. Do I need to add some routing to the parent enp2s0 interface?

user interface – Laggy Android. I install above 100 apps at LineageOS. How to make Inteface more responsive?

I have phone with 128GB, 6GB RAM, Quad-core, but after install over 100 apps it became laggy. Especially I would like to improve speed of user interface. Phone is rooted, unlocked and encrypted.

I use OS Monitor App and most of the time there is like a 30% of CPU utilisation.
I get rid of some apps that startup with OS boot using Autostarts app but still I am not happy with results.

How to speed interface of Android (LineageOS 17) ? Any other software that could help me?
I realize that I have a some background services like GlassWire and Vigilante, but I don’t think it could get as much as 30% CPU utilisation. I had before Android(MIUI 11) at other phone and I think it became laggy after installing many apps.

custom attributes – Cannot instantiate interface CompanyModuleApiQuoteDataAddressInterface

Iam using Magento 2.3.5-p2. I add custom field in the cart and checkout page which is successfully showing there.

In appcodeCompanyModuleetcdi.xml

<preference for="MagentoQuoteApiDataAddressInterface" type="CompanyModuleApiQuoteDataAddressInterface"/>

In appcodeCompanyModuleApiQuoteDataAddressInterface.php

<?php
namespace CompanyModuleApiQuoteData;

use MagentoQuoteApiDataAddressInterface as MagentoDataAddressInterface;

interface AddressInterface extends MagentoDataAddressInterface
{
    const KEY_SUBDISTRICT = 'subdistrict_id';

    public function getSubdistrictId();

    public function setSubdistrictId($subdistrictId);
}

But while getting shipping estimate, it was error

Uncaught Error: Cannot instantiate interface CompanyModuleApiQuoteDataAddressInterface in C:wamp64wwwmagento2-2vendormagentoframeworkObjectManagerFactoryDynamicDeveloper.php:50

I already

  • Delete /generated/code
  • Delete /static/frontend
  • php binmagento cache:clean

design – Creating an interface that connects to different data sources

I am working on a project which connects to different data sources and fetches data. The problem is each of these data source needs different parameters to fetch the data

s3 = S3(ACCESS_KEY, SECRET_KEY, BUCKET, HOST)
db = DB(HOST, USERNAME, PASSWORD, DB_NAME, SCHEMA)
sftp = SFTP(HOST, USERNAME, PASSWORD)
     

The fetch data function also a different signature

s3.fetch_data(folder_path, filename)
db.fetch_data(table_name, filter_args)
sftp.fetch_data(file_path)

How to design a common interface that can stream data from and to any of the above data sources(defined dynamically via a config). Is there a design pattern that addresses this problem.

I have looked into strategy pattern but I assume that it applies to cases where the behavior changes but the is-a relationship prevails.

Incase of repository pattern there needs to be a common object on multiple storage

Both cases doesn’t apply here

Best approach for developing a stateful computation-heavy application with a rest-api interface using python?

I want to develop an end-to-end machine learning application where data will be in GPU-memory and computations will run on the GPU. A stateless RESTfull service with a database is not desirable since the traffic between GPU-memory and database will destroy the “purpose” of it being fast.

The way I see it is that I need a way to “serve” the class (let’s call it as experiment class) which has the data and the methods, then call them using rest APIs.

Right now I am using FastApi and initialize the experiment class in it which I believe is not optimal. My class (as well as the data) lives in FastAPI runtime. Kinda like,

import experiment_class
import FastApi

app = FastAPI()
my_experiment = expertiment_class()

@app.get("/load_csv")
my_experiment.load_csv("some_file_path")

// do some more on the data
...

There are two problems I am having a hard time with,

One of them is the terminology:

  • Is this really a stateful application?
  • Is there a word to describe what I am doing? Is this a “Model, View, Controller” design, can it be a simple “Server-Client” or is it something completely different?
  • Do I need a “Web-server”, a “Web-framework” or a “Web-service” for this?

Another one is what technology I can use for this :

  • Is it okay to use FastAPI like this?
  • Do I set up an RPC server (Remote Procedure Call) and call it using Rest API?
  • Is WSGI or an ASGI server suitable for this task?
  • Are Django, Flask, Tornado like web frameworks only used for stateless apps? Because nearly all of the examples are.
  • Do I stick to bare bone Python where I use threads or BaseManager servers?

P.S. What I meant with end-to-end machine learning is that I should be able to load data, process it, and give it to the model for training all the while without leaving the GPU-memory. You can think of a Jupyter-notebook, but we call the cells with rest API.

unity – How can I call Schedule() on interface that inherits from IJob

So I have a voxel terrain engine, and I have an interface that all meshing jobs should inherit from. That looks like this (simplified from the actual):

public interface IMesherJob : IJob
{
    VoxelDataVolume<byte> VoxelData { get; set; }

    NativeArray<MeshingVertexData> OutputVertices { get; set; }
    NativeArray<ushort> OutputTriangles { get; set; }
}

One example of a meshing job is marching cubes, which looks like this: public struct MarchingCubesJob : IMesherJob

So here’s my question: If I have a reference to an IMesherJob, can I somehow call Schedule on it? If I have a reference to MarchingCubesJob, I can call Schedule on it:

This works:

MarchingCubesJob myMeshingJob = GetMeshingJob();
JobHandle handle = myMeshingJob.Schedule(voxelCount, 64)

But this does not work:

IMesherJob myMeshingJob = GetMeshingJob();
JobHandle handle = myMeshingJob.Schedule(voxelCount, 64);

I noticed that Schedule() is an extension method for T where T : struct, IJob so that might be interfering with it, but it should still work because IMesherJob inherits from IJob