class Alphabets { public static void main(String args[]) { char ch; for( ch = 'a' ; ch <= 'z' ; ch++ ) System.out.println(ch); } }
Saturday, 29 June 2013
Print alphabets in java
First program in java
class First
{
public static void main(String[] arguments)
{
System.out.println("Welcome to Java technology.");
}
}
Friday, 28 June 2013
Changing the Cursor in Java awt
/**
* @(#)Text3.java
*
*
* @author
* @version 1.00 2013/6/28
*/
import java.awt.*;
import java.awt.event.*;
class Cursorch extends Frame implements ActionListener
{
Button btn_1,btn_2;
public Cursorch()
{
setLayout(new FlowLayout());
setTitle("Cursor");
setSize(500,500);
setLocation(100,100);
btn_1=new Button("Get Cursor 1");
btn_1.addActionListener(this);
add(btn_1);
btn_2=new Button("Get Cursor 1");
btn_2.addActionListener(this);
add(btn_2);
Cursor cur = btn_1.getCursor();
Cursor cur1 = btn_2.getCursor();
btn_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn_2.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
}
}
class ChangeCursor
{
public static void main(String []args)
{
new Cursorch().setVisible(true);
}
}
Image on Frame in Java AWT
/**
* @(#)DisplayImage.java
*
*
* @author - Anant Mahale
* @version 1.00 2013/6/28
*/
import java.awt.*;
import java.awt.event.*;
class image extends Frame
{
Image img;
public image()
{
setTitle("Image");
setSize(300,300);
setLocation(100,100);
MediaTracker mt=new MediaTracker(this);
img = Toolkit.getDefaultToolkit().getImage("post.jpg");
mt.addImage(img,0);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void paint(Graphics g)
{
if(img != null)
g.drawImage(img, 0, 0, this);
else
g.clearRect(0, 0, getSize().width, getSize().height);
}
}
class DisplayImage
{
public static void main(String []args)
{
new image().setVisible(true);
}
}
Create java .bat OR bat file
1. type java program in your java editor like notepad,textpad or jcreator ...
2. Save that java program with .java. Example "Example.java"
3. Open run then type "cmd" prem ok OR open command prompt set you directory
if you save the program in another drive then type like this ...
C:\Users\Swati>f:
here "C:\Users\Swati>" is defoult path and with "f:" it will change the driver ...
Now you will get your destination driver like
F:\>
As show in image ...
4. Then set the path of java bin folder as show in image ...
5. Then complie program like "javac first.java" ... as show in image
6. In the last run program like "java first" ... as show in image
7. Now you get the out put of your java program ...
8. Now for creating you java bat file, click on the left-top conrer of command prompt and then click on edit section and then click on the "select all"... it will display as show in image
9. Then again , click on the left-top conrer of command prompt and then click on edit section and then click on the "copy" ...
10. Now open "notepad" and paste that matter in note pad ... it will seen like this as show in image ...
11. Now Delete unwanted data and make new look of data as show in image ...
12. now type "pause" in last of data as shown in image ...
13. If you save your java file with any name like "Example.java" so take that name like "example" ... Here I'm save my file with "first.java" so I'll take name "first" and save file as "fist.bat" ... it means you have to save that notepad file like "Example.bat" ... as show in image
14. Your .bat file is ready ... double clicking on .bat file java program will compile and run ...
2. Save that java program with .java. Example "Example.java"
3. Open run then type "cmd" prem ok OR open command prompt set you directory
if you save the program in another drive then type like this ...
C:\Users\Swati>f:
here "C:\Users\Swati>" is defoult path and with "f:" it will change the driver ...
Now you will get your destination driver like
F:\>
As show in image ...
4. Then set the path of java bin folder as show in image ...
5. Then complie program like "javac first.java" ... as show in image
6. In the last run program like "java first" ... as show in image
7. Now you get the out put of your java program ...
8. Now for creating you java bat file, click on the left-top conrer of command prompt and then click on edit section and then click on the "select all"... it will display as show in image
9. Then again , click on the left-top conrer of command prompt and then click on edit section and then click on the "copy" ...
10. Now open "notepad" and paste that matter in note pad ... it will seen like this as show in image ...
11. Now Delete unwanted data and make new look of data as show in image ...
12. now type "pause" in last of data as shown in image ...
13. If you save your java file with any name like "Example.java" so take that name like "example" ... Here I'm save my file with "first.java" so I'll take name "first" and save file as "fist.bat" ... it means you have to save that notepad file like "Example.bat" ... as show in image
14. Your .bat file is ready ... double clicking on .bat file java program will compile and run ...
Sunday, 23 June 2013
How to handle String in switch case of JAVA
public class Test{
public enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
public static void main(String args[]) {
String str="SUNDAY";
switch (Day.valueOf(str))
{
case MONDAY:
System.out.println("Day is Monday");
break;
case TUESDAY:
System.out.println("Day is Tuesday");
break;
case WEDNESDAY:
System.out.println("Day is Wednesday");
break;
case THURSDAY:
System.out.println("Day is Thursday");
break;
case FRIDAY:
System.out.println("Day is Friday");
break;
case SATURDAY:
System.out.println("Day is Saturday");
break;
case SUNDAY:
System.out.println("Day is Sunday");
break;
}
}
}
public enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
public static void main(String args[]) {
String str="SUNDAY";
switch (Day.valueOf(str))
{
case MONDAY:
System.out.println("Day is Monday");
break;
case TUESDAY:
System.out.println("Day is Tuesday");
break;
case WEDNESDAY:
System.out.println("Day is Wednesday");
break;
case THURSDAY:
System.out.println("Day is Thursday");
break;
case FRIDAY:
System.out.println("Day is Friday");
break;
case SATURDAY:
System.out.println("Day is Saturday");
break;
case SUNDAY:
System.out.println("Day is Sunday");
break;
}
}
}
Saturday, 22 June 2013
Draw rectangle in java frame using Mouse Even
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mousedrag extends JFrame implements MouseListener,MouseMotionListener
{
int x,y,x1,y1;
mousedrag()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseEntered(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseMoved(MouseEvent me)
{
}
public void mouseDragged(MouseEvent me)
{
x1=me.getX();
y1=me.getY();
repaint();
}
public void mouseExited(MouseEvent me)
{
}
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(x,y,Math.abs(x-x1),Math.abs(y-y1));
}
public static void main(String args[])
{
mousedrag f=new mousedrag();
f.setTitle("demo of mouse event");
f.setSize(400,400);
f.setVisible(true);
}
}
Subscribe to:
Posts (Atom)










