Thursday, August 11, 2011

Text Editor - Using Swing


Program to develop a full functional Text Editor that create and update a file.

Editor will have following features :

1. Create new Text file
2. Open existing text file and modify
3. Change font to Bold and Italic
4. Change font to Serif or Santserif or Courier
5. Change Foreground and Background colors


See http://aj.sunrays.co.in/Home/swing-1/project---text-editor









Calculator using Swing classes

Program written in Java Swing components to write live Calculator.

See : http://aj.sunrays.co.in/Home/swing-1/calculator





Tuesday, August 9, 2011

Shutdown Computer - Cross Platform

Program to Shutdown Machine.


public class ShutdownMachine {
public static void main(String[] args)throws Exception {

String shutdownCommand;

String operatingSystem = System.getProperty("os.name");

if ("Linux".equals(operatingSystem)
|| "Mac OS X".equals(operatingSystem)) {
shutdownCommand = "shutdown -h now";
} else if ("Windows".equals(operatingSystem)) {
shutdownCommand = "shutdown.exe -s -t 0";
} else {
throw new RuntimeException("Unsupported operating system.");
}

Runtime.getRuntime().exec(shutdownCommand);
System.exit(0);

}
}

Play an Audio File

Play an audio file from java program.


import java.io.FileInputStream;
import java.io.InputStream;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class PlaySoundFile {

/** Plays audio from given file names. */

public static void main(String[] args) throws Exception {

String fileName = "D:/songs/laila.wav";

InputStream in = new FileInputStream(fileName);

// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);

// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);

// Similarly, to stop the audio.
// AudioPlayer.player.stop(as);

} // playAudioStream
}

Monday, August 8, 2011

Call an Exe

Call an exe file with parameters from Java.

import java.io.IOException;

public class RunAnExe {

public static void main(String[] args) {

Runtime rt = Runtime.getRuntime();
Process p =null;
try {
p = rt.exec("notepad c:/test.xml");
} catch (IOException e) {
System.out.println("Exe does not exist or access rights issues" + e.getMessage());
}
System.out.println("Process Exit Normally" + p.exitValue());
}
}

Get System Processor, Memory, Drives

A Program to get your System Information
  • Available, Free and Maximum memory
  • Processors
  • File System ( Number of Drives )

import java.io.File;

public class GetSystemInfo {

/**
* @param args
*/
public static void main(String[] args) {
/* Total number of processors or cores available to the JVM */
System.out.println("Available processors (cores): "
+ Runtime.getRuntime().availableProcessors());

/* Total amount of free memory available to the JVM */
System.out.println("Free memory (bytes): "
+ Runtime.getRuntime().freeMemory());

/* This will return Long.MAX_VALUE if there is no preset limit */
long maxMemory = Runtime.getRuntime().maxMemory();
/* Maximum amount of memory the JVM will attempt to use */
System.out.println("Maximum memory (bytes): "
+ (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));

/* Total memory currently in use by the JVM */
System.out.println("Total memory (bytes): "
+ Runtime.getRuntime().totalMemory());

/* Get a list of all filesystem roots on this system */
File[] roots = File.listRoots();

/* For each filesystem root, print some info */
for (File root : roots) {
System.out.println("File system root: " + root.getAbsolutePath());
}
}

}


Get Data from a URL

This program will read data from a Web URL and write to Console. This program can be used to make a Custom Web browser.


import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

public class GetDataFromURL {

public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");

InputStream inStream = yahoo.openStream();
Scanner in = new Scanner(inStream);

while (in.hasNext()) {
System.out.println(in.nextLine());
}
inStream.close();
}
}