Wednesday, 4 September 2013

Playing with virtual magnets on a virtual whiteboard

public class JavaFXAssessDecideDo extends Application {
   
    @Override
    public void start(Stage primaryStage) {
       
        Circle magnet = createCircle(50,50,Color.RED);
       
        StackPane root = new StackPane();
        root.getChildren().add(magnet);
       
        Scene scene = new Scene(root, 300, 250);
       
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
       
}
   
    private Circle createCircle(double x, double y, Color color) {
    final Circle c = new Circle(x, y, 25);
    c.setFill(color);

    c.setOnMouseDragged(new EventHandler() {

        @Override
        public void handle(MouseEvent event) {
            c.relocate(event.getSceneX() - c.getRadius(), event.getSceneY() - c.getRadius());
        }
    });

    return c;
}




References

http://stackoverflow.com/questions/10530829/javafx-drag-and-drop-moving-icon

Tuesday, 6 August 2013

My First JavaFX Radial Gauge


public class MyFirstRadialGauge extends Application {

    @Override
    public void start(Stage primaryStage) {
//primaryStage.initStyle(StageStyle.TRANSPARENT);
Marker temperature = new Marker(50, Color.MAGENTA);
Marker[] markerList = new Marker[]{temperature};

Gauge radial = GaugeBuilder.create()
                           .prefWidth(500)
                           .prefHeight(500)
                           .gaugeType(GaugeBuilder.GaugeType.RADIAL)
                           .frameDesign(Gauge.FrameDesign.STEEL)
                             

...
                           .title("Temperature")
                           .unit("°C")
                           .build();
       
        StackPane root = new StackPane();
        root.getChildren().add(radial);
        radial.setLcdValue(10.00);
        radial.setLcdDecimals(2);
        radial.setLcdUnitVisible(false);    
       
        temperature.setValue(30);
       
        Scene scene = new Scene(root, 500, 500);
       
        primaryStage.setTitle("My First Radial Gauge");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


References

http://harmoniccode.blogspot.ro/2012/06/jfxtras-series-radial-gauge.html 

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.jfxtras/jfxtras-labs/2.2-r5/ 

Monday, 5 August 2013

Programming a JavaFX Web Service Client using NetBeans IDE 7.3

1. File -> New Project -> JavaFX -> JavaFX Application
    Project Name: JavaFXWebClient
    Finish

2. Right click JavaFXWebClient in Projects window  and select New -> Web Service Client
   (You need to have Java EE plugin installed. This is available in Tools -> Plugins.)
   WSDL URL: http://www.webservicex.net/stockquote.asmx?WSDL

3. Right click in source editor window -> Insert  Code -> Call Web Service Operation
    This automatically adds method definitions corresponding to Web service calls, for example the following piece of code:

private static String getQuote(java.lang.String symbol) {
        net.webservicex.StockQuote service = new net.webservicex.StockQuote();
        net.webservicex.StockQuoteSoap port = service.getStockQuoteSoap();
        return port.getQuote(symbol);
    }


4. Write your JavaFX Web service client using the methods defined in the previous step.

public class JavaFXWebClient extends Application {
   
    Label quoteLbl = new Label();
    TextField textFld = new TextField();
    Button btn = new Button();
   
    @Override
    public void start(Stage primaryStage) {
        btn.setText("Get IBM Quote");
        btn.setOnAction(new EventHandler() {
            @Override
            public void handle(ActionEvent event) {
                quoteLbl.setText(getQuote("IBM"));
                textFld.setText(getQuote("IBM"));
            }
        });
       
        Group root = new Group();
        root.getChildren().add(0,btn);
        root.getChildren().add(1,quoteLbl);
        root.getChildren().add(2,textFld);
       
        root.getChildren().get(0).setLayoutX(50);              
        root.getChildren().get(0).setLayoutY(70);      
        root.getChildren().get(1).setLayoutX(50);      
        root.getChildren().get(1).setLayoutY(10);
        root.getChildren().get(2).setLayoutX(50);      
        root.getChildren().get(2).setLayoutY(40);
       
        Scene scene = new Scene(root, 700, 250);
       
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


References

http://lehelsipos.blogspot.ro/2012/09/javafx-22-webservice-example.html

http://www.actionscript.org/forums/showthread.php3?t=70742

https://netbeans.org/kb/docs/websvc/jax-ws.html

https://netbeans.org/kb/docs/websvc/intro-ws.html

Tuesday, 2 April 2013

SSTF (Shortest Seek Time First) Disk Scheduling Algorithm with Java

class SSTF implements DiskScheduler{
    int[] queue;
    int initialCylinder;
   
public SSTF(int[] queue, int initialCylinder){
        this.queue = queue;
        this.initialCylinder = initialCylinder;
    }

    public int serviceRequests(){
        int headMovement = 0;
        int prev = initialCylinder;
        int [] rpath = path();
        for (int i=0; i < rpath.length; i++) {
            headMovement += Math.abs(rpath[i]-prev);
            prev = rpath[i];
        }
        return headMovement;
    }
   
    public int[] path(){
       int [] resultPath = new int[queue.length];
       int now = initialCylinder;
       int [] requests = new int[queue.length];
       for (int i = 0; i < queue.length; i++){
        requests[i] = queue[i];
    }
       for (int i = 0; i < resultPath.length; i++){
        int closest = closest(now, requests);
        resultPath[i] = closest;
        now = closest;
    }
       return resultPath;
    }
   
    int closest(int k, int[] requests){
        int min = 5000000;
        int minPos = -1;
        for (int i = 0; i < requests.length; i++){
            if (requests[i] == -1) continue;
            else if  (Math.abs(k-queue[i]) < min) {
                minPos = i;
                min = Math.abs(k-queue[i++]);           
            }
        }
        int nearestCylinder = requests[minPos];
        requests[minPos] = -1;
        return nearestCylinder;
    }
   
    public void println(){
    System.out.println("SSTF head movement = " + serviceRequests());
       
        System.out.print("SSTF Path = ");
        for(int i: path()){
            System.out.print(i + " "); 
        }
        System.out.println("");
}
}

Bibliography

Avi Silberschatz, Peter Baer Galvin, Greg Gagne: Operating System Concepts with Java

Sunday, 24 March 2013

Disk Scheduling Simulator with JavaFX



import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

/**
 *
 * @author Cristina
 */
public class JavaFXApplication1 extends Application {
   
    @Override
    public void start(Stage primaryStage) {
      primaryStage.setTitle("Disk Scheduling Simulator");
      Group root = new Group();
      Scene scene = new Scene(root, 750, 500);
     
      int[] queue = {98, 183, 37, 122, 14, 124, 65, 67};
      int headStart = 53; 
       
      DiskScheduler fcfs = new FCFS(queue, headStart);
     
      int [] result = fcfs.path();
    
      Path path = new Path(); 
   
      MoveTo moveTo = new MoveTo();
      moveTo.setX(headStart);
      moveTo.setY(50);
      path.getElements().add(moveTo); 
     
  for(int i = 0; i < result.length; i++){      
          LineTo lineTo = new LineTo();
          lineTo.setX(4*result[i]);
          lineTo.setY(50 + i*50); 
          path.getElements().add(lineTo);
      }
     
      path.setStrokeWidth(3);
      path.setStroke(Color.BLUE);
      root.getChildren().add(path);  
      primaryStage.setScene(scene);
      primaryStage.show();
    }

Bibliography

[1] http://java-buddy.blogspot.ro/2012/02/javafx-20-exercise-draw-basic-line.html
[2] Avi Silberschatz, Peter Baer Galvin, Greg Gagne: Operating System Concepts with Java

Saturday, 23 March 2013

FCFS Disk Scheduling Algorithm with Java

package javaos12;

/**
 *
 * @author Cristina
 */
public class JavaOS12 {

    public static void main(String[] args) {
   
        int[] queue = {98, 183, 37, 122, 14, 124, 65, 67};
        int headStart = 53; 
       
        DiskScheduler fcfs = new FCFS(queue, headStart);
       
        System.out.println("Head starts at: " + headStart);
        System.out.print("Queue = ");
        for(int i: queue){
            System.out.print(i + " ");
        }
        System.out.println("");
       
        fcfs.println();
       
    }
}

interface DiskScheduler
{
    public int serviceRequests();
    public int[] path();
    public void println();
}


class FCFS implements DiskScheduler{
    int[] queue;
    int initialCylinder;
    public FCFS(int[] queue, int initialCylinder){
        this.queue = queue;
        this.initialCylinder = initialCylinder;
    }
    public int serviceRequests(){
        int headMovement = 0;
        int prev = initialCylinder;
        int [] rpath = path();
        System.out.println(rpath.length);
        for (int i=0; i < rpath.length; i++) {
           headMovement += Math.abs(rpath[i]-prev);
            prev = rpath[i];
        }
        return headMovement;
    }
   
    public int[] path(){
       int [] resultPath = new int[queue.length];
       for (int i = 0; i < queue.length; i++){
        resultPath[i] = queue[i];
    }
       return resultPath;
    }
   
    public void println(){
    System.out.println("FCFS head movement = " + serviceRequests());
       
        System.out.print("FCFS Path = ");
        for(int i: path()){
            System.out.print(i + " "); 
        }
        System.out.println("");
}
}


Bibliography

Operating System Concepts with Java, Avi Silberschatz, Peter Baer Galvin, Greg Gagne

Wednesday, 20 February 2013

Moving files in Java

Assignment

Design an algorithm for moving one file, that is copying a file from a source (a directory) to another source, and deleting the original. Consider that folders are files, too.

 

 Step 1: Build a simple working program ([1])

 

import java.io.*;

public class mv{
public static void main(String args[]){
         try{
         // File (or directory) to be moved
         File file = new File("C:\\ProgrameJava\\file.txt");
       
         // Destination directory
         File dir = new File("C:\\ProgrameJava\\myDir");
       
         // Move file to new directory
         boolean success = file.renameTo(new File(dir, file.getName()));
             if (!success) {
             System.out.println("File not moved");
         }
     }
         catch (Exception ioe){
         ioe.printStackTrace();
     }
}
}

Step 2: Add code to check if source and destination exist, and print appropriate messages on the application console

 

Step 3: Give the names of the source and the destination as command line arguments

 

Step 4: Use FileInputStream and FileOutputStream to move the file ([4])

 

Bibliography

  1. http://www.java.happycodings.com/Core_Java/code50.html
  2. http://docs.oracle.com/javase/tutorial/essential/io/move.html
  3. http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#move%28java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...%29
  4. http://myflex.org/yf/java/slides/Unit8.pdf
  5. http://stackoverflow.com/questions/300559/move-copy-file-operations-in-java