Showing posts with label source code. Show all posts
Showing posts with label source code. Show all posts

Source code to get class object ???

*******************************************************
COMPLETE SOURCE CODE FOR : GetClassObject.java
*******************************************************


public class GetClassObject
{
public static void main(String[]args)
{
try
{
//This program will get class object for Object class
Class a=Class.forName("java.lang.Object");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

Source code to know how long a java application is running

*********************************************************
COMPLETE SOURCE CODE FOR : HowLongApplicationRunning.java
*********************************************************


public class HowLongApplicationRunning
{
public static void main(String[]args)
{
long startTime=System.currentTimeMillis();

try
{
//MAKE YOUR APPLICATION SLEEP IN 10 SECONDS
Thread.sleep(10000);
}
catch(Exception exception)
{
exception.printStackTrace();
}

long endTime=System.currentTimeMillis();

//GET DURATION IN SECOND
long timeInSecond=(endTime-startTime)/1000;

System.out.println("Duration For Your Application Running Is : "+timeInSecond+" seconds");
}
}


*********************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************

Java source code to determine when application exit by print a line of text on command prompt

*************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenApplicationExit.java
*************************************************************


public class DetermineWhenApplicationExit
{
public static void main(String[]args)
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
//Application will print "BYE" when it close
System.out.println("BYE");
}
});

//Make this application terminate
System.exit(0);
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Java source code for application terminate (Exit application)

*****************************************************************
COMPLETE SOURCE CODE FOR : ExitApplication.java
*****************************************************************


public class ExitApplication
{
public static void main(String[]args)
{
//System.exit(0) will make your application exit
System.exit(0);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

PART 3 Java Source Code : Draw String In Java

This part will show you why when you set g.drawString("HI EVERYONE",10,50); to
g.drawString("HI EVERYONE",10,10); in PART 1, you can't see the wording in frame. This is because wording "HI EVERYONE " is hide at back of frame title. So if you want to see it, you just call method setUndecorated for frame and set it's argument to true. This will make your frame title hide and you can see the wording.

************************************************************
COMPLETE SOURCE CODE FOR : DrawingText3.java
************************************************************


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText3 extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText3()
{
super("Drawing Text");
addWindowListener(wl);
setUndecorated(true);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);

g.drawString("HI EVERYONE",10,10);
}

public static void main(String[]args)
{
DrawingText3 dt=new DrawingText3();
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

PART 2 Java Source Code : Draw String In Java

This part of draw string in java is not much different to the previous part. In this part, we just use FontMetrics class to get information about Font like it's height. You can see it at g.drawString("HI EVERYONE",10,fm.getHeight()*3);

************************************************************
COMPLETE SOURCE CODE FOR : DrawingText2.java
************************************************************


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.FontMetrics;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText2 extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText2()
{
super("Drawing Text");
addWindowListener(wl);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);

FontMetrics fm=g.getFontMetrics();
g.drawString("HI EVERYONE",10,fm.getHeight()*3);
}

public static void main(String[]args)
{
DrawingText2 dt=new DrawingText2();
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

PART 1 Java Source Code : Draw String In Java

This is part one of draw string in java. If you change g.drawString("HI
EVERYONE",10,50); in source code below to g.drawString("HI EVERYONE",10,10); you can't see wording. If you want to see it, you can see part three of draw string in
java.So you can know why you can't see it in this part.

*************************************************************
COMPLETE SOURCE CODE FOR : DrawingText.java
*************************************************************


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText()
{
super("Drawing Text");
addWindowListener(wl);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);
g.drawString("HI EVERYONE",10,50);
}

public static void main(String[]args)
{
DrawingText dt=new DrawingText();
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Source code to convert integer to string in java


*****************************************************
COMPLETE SOURCE CODE FOR : ConvertIntegerToString.java
*****************************************************


public class ConvertIntegerToString
{
public static void main(String[]args)
{
int sampleNumber=123;

//Convert integer to string
String stringForNumber=Integer.toString(sampleNumber);

System.out.println(stringForNumber);
}
}


*****************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************

Source code to determine display mode for current screen can change or not using java


***********************************************************
COMPLETE SOURCE CODE FOR : DetermineDisplayModeCanChangeOrNot.java
***********************************************************


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;

public class DetermineDisplayModeCanChangeOrNot
{
public static void main(String[]args)
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();

//METHOD isDisplayChangeSupported() returns true if this GraphicsDevice supports low-level display changes.
boolean change=gd.isDisplayChangeSupported();

if(change==true)
{
System.out.println("DISPLAY MODE CAN CHANGE");
}

else
{
System.out.println("DISPLAY MODE CANNOT CHANGE");
}
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

Current refresh rate


*****************************************************************
COMPLETE SOURCE CODE FOR : RefreshRateForCurrentScreen.java
*****************************************************************


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;

public class RefreshRateForCurrentScreen
{
public static void main(String[]args)
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();
DisplayMode dm=gd.getDisplayMode();

//Print refresh rate for current screen
System.out.println("REFRESH RATE FOR CURRENT SCREEN : "+dm.getRefreshRate());
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

RELAXING NATURE VIDEO