Java Banner Applet

A simple applet that displays a banner.
The text is placed inside variable msg

import java.awt.*;
import java.applet.*;

/*
<applet code="banner" width = 300 height = 50>
</applet>
*/
public class banner extends Applet implements Runnable{
String msg = " Java";
Thread t = null;
int state;
boolean stopFlag;

public void init(){
setBackground(Color.cyan);
setForeground(Color.red);
}
public void start(){
t = new Thread(this);
stopFlag = false;
t.start();
}
public void run(){
char ch;
for( ; ; ){
try {
repaint();
Thread.sleep(250);
ch = msg.charAt(0);
int j = msg.length();
msg = msg.substring(1, j);
msg += ch;
if(stopFlag)
break;
}
catch(InterruptedException e){}
}
}
public void stop(){
stopFlag = true;
t = null;
}
public void paint(Graphics g){
g.drawString(msg, 50,30);
}
}





Compile as javac banner.java
Run as appletviewer banner.java
Tags:banner in java,java banner,applet,applet in java,java banner applet

Java Program to prove that selection sort is faster than bubble sort

class bubblesort implements Runnable{
int ar[] = new int[10000];
Thread td;
bubblesort(){
int t=0;
for(int i=9999;i>0;i--){
t++;
ar[t]=i;
}
td=new Thread(this,"Gow");
td.start();
}
public void run(){
int t;
for(int i=0;i<9999;i++){
for(int j=0;j<9999-i;j++){
if(ar[j]>ar[j+1]){
t=ar[i];
ar[i]=ar[j+1];
ar[j+1]=t;
}
}
}
System.out.println("Done");
}
}

class selsort implements Runnable{
int ar[] = new int[10000];
Thread td;
selsort(){
int t=0;
for(int i=9999;i>0;i--){
t++;
ar[t]=i;
}
td=new Thread(this,"Gow1");
td.start();
}
public void run(){
int t;
int min=0;
for(int i=0;i<9999;i++){
min=i;
for(int j=i+1;j<10000;j++){
if(ar[j]<ar[min])
min=j;
}
t=ar[i];
ar[i]=ar[min];
ar[min]=t;

}
System.out.println("Done2");
}

}
class check{
public static void main(String args[]){
new bubblesort();
new selsort();
}
}

 

compile as javac  check.java

run as java check

Java Program that uses Datagram packet to send a file over LAN

In this program we have a server(noide.java) and a client(noides.java)

This is the server or the broadcaster

import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.net.*;
class noide{
public static int serverPort = 998;
public static int clientPort = 999;
public static int buffer_size = 400000;
public static DatagramSocket ds;
public static byte buffer[] = new byte[buffer_size];

public static void main(String args[])throws Exception,IOException{
FileInputStream fln;
FileChannel fchan;
BufferedReader g = new BufferedReader(new InputStreamReader(System.in));
String bh = g.readLine();
long fsize;
ByteBuffer mbuf;
ds = new DatagramSocket(serverPort);

fln = new FileInputStream(bh);
fchan = fln.getChannel();
fsize = fchan.size();
System.out.println( fsize);
mbuf = ByteBuffer.allocate((int) fsize);
fchan.read(mbuf);
int hp;
mbuf.rewind();
for( hp = 0;hp<fsize;hp++)
buffer[hp] = mbuf.get();
ds.send(new DatagramPacket(buffer,hp,InetAddress.getLocalHost(),clientPort));

fchan.close();
fln.close();
}
}

 

As you can see it sends it to the LocalHost(127.0.0.1).It can be changed to work over LAN or over the Internet(port-forwarding required).The client :

import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.net.*;
class noides{
public static int serverPort = 998;
public static int clientPort = 999;
public static int buffer_size = 400000;
public static int buffer_siz = 1024;
public static DatagramSocket ds;
public static byte buffer[] = new byte[buffer_size];

public static void main(String args[])throws java.io.IOException{
FileOutputStream fln;
FileChannel fchan;

ByteBuffer mbuf;
ds = new DatagramSocket(clientPort);

fln = new FileOutputStream("tester.txt");

fchan = fln.getChannel();

DatagramPacket  p = new DatagramPacket(buffer,buffer.length);
System.out.println("OK");
        ds.receive(p);
        int hunt = p.getLength();
        mbuf = ByteBuffer.allocate(p.getLength());
        System.out.println(new String(p.getData(),p.getOffset(),p.getLength()));
        mbuf.put(p.getData(),p.getOffset(),p.getLength());

mbuf.rewind();

fchan.write(mbuf);
fchan.close();
fln.close();
}
}

The client must be running before the server is started.

It is saved in tester.txt.

 

Tags:-java program,datagram packet,datagram socket,datagram socket over LAN,server client in Datagram,send a file with Datagram.