Wednesday 29 January 2014

Notepad Application using Swing in JAVA

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.*;

public class Notepad {
    JFrame frame;
    JMenuBar menuBar;
    JMenu file;
    JMenuItem open, save, exit;
    JFileChooser fileChooser;
    JTextArea textArea;
  
    Notepad() {
        frame = new JFrame("Notepad Application");
        file = new JMenu("File");
        open = new JMenuItem("Open");
        save = new JMenuItem("Save");
        exit = new JMenuItem("Exit");
        textArea = new JTextArea();
        fileChooser = new JFileChooser();
        menuBar = new JMenuBar();
      
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        frame.add(textArea);
        file.add(open);
        file.add(save);
        file.add(exit);
        menuBar.add(file);
        frame.setJMenuBar(menuBar);
      
        OpenListener openL = new OpenListener();
        SaveListener saveL = new SaveListener();
        ExitListener exitL = new ExitListener();
        open.addActionListener(openL);
        save.addActionListener(saveL);
        exit.addActionListener(exitL);
      
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
  
    class OpenListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog(frame)) {
                File file = fileChooser.getSelectedFile();
                textArea.setText("");
                Scanner in = null;
                try {
                    in = new Scanner(file);
                    while(in.hasNext()) {
                        String line = in.nextLine();
                        textArea.append(line+"\n");
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    in.close();
                }
            }
        }
    }
  
    class SaveListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (JFileChooser.APPROVE_OPTION == fileChooser.showSaveDialog(frame)) {
                File file = fileChooser.getSelectedFile();
                PrintWriter out = null;
                try {
                    out = new PrintWriter(file);
                    String output = textArea.getText();
                    System.out.println(output);
                    out.println(output);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    try {out.flush();} catch(Exception ex1) {}
                    try {out.close();} catch(Exception ex1) {}
                }
            }
        }
    }
  
    class ExitListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
  
    public static void main(String args[]) {
        Notepad n = new Notepad();
    }
}

Friday 24 January 2014

Splash Screen Program in Java using JWindow

import javax.swing.*;
import java.awt.*;

public class SplashScreen extends JWindow {
    private static final long serialVersionUID = 1L;

    Image bi;
    ImageIcon ii;

    boolean loaded = false;

    public SplashScreen(String path) {
        try {
            bi = Toolkit.getDefaultToolkit().getImage(path);
            ii = new ImageIcon(bi);
            setSize(ii.getIconWidth(), ii.getIconHeight());
            setLocationRelativeTo(null);
            loaded = true;
            this.setBackground(new Color(0, 255, 0, 0));
            showSplashScreen(true);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public void showSplashScreen(boolean flag) {
        if (!loaded) {
            System.err.println("Splash screen image isn't loaded.");
            return;
        }
        setVisible(flag);
    }

    public void dispose() {
        dispose();
    }

    public void paint(Graphics g) {
        g.drawImage(bi, 0, 0, null);
    }
    public static void main(String[] s)
    {
        new SplashScreen("IOPins_logo.png");
    }
}

Write a program to create Progress Bar using Swing in JAVA

import javax.swing.*;
import java.awt.*;

class progDemo extends JFrame
{
    JProgressBar jpb = new JProgressBar(0,100);
    progDemo()
    {
        setVisible(true);
        setSize(400,400);
        setLayout(new FlowLayout());
        add(jpb);
        
        Thread t = new Thread(new Runnable(){
                public void run()
                {
                    try
                    {
                        for(int i=0;i<=100;i++)
                        {
                            jpb.setValue(i);
                            Thread.sleep(100);
                        }
                    }
                    catch(Exception e){System.out.println(e);}
                } 
        });
        t.start();
        
    }
    public static void main(String s[])
    {
        new progDemo();
    } 
}


OUTPUT 

Simple Layout Demo Program using Applet

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

public class layoutDemo extends Applet
{
    Button b1, b2, b3;
    public void init()
    {
        b1 = new Button("One");
        b2 = new Button("Two");
        b3 = new Button("Three");
        
        setLayout(new GridLayout(2,2));
        setLayout(new BorderLayout());
        setLayout(null);
        add(b1);      
        add(b2);
        add(b3);
        
        b1.setBounds(100,100, 100, 30);
        b2.setBounds(200,100, 100, 30);
        b3.setBounds(100,200, 100, 30);
        
    }
        
}

/*
<applet code="layoutDemo" width="500" height="500"></applet>
*/

OUTPUT



Simple Create a Frame using JAVA

import java.awt.*;

class frameDemo extends Frame
{
    Button b = new Button("Click Me!");
    
    frameDemo()
    {
        setVisible(true);
        setSize(400,400);
        setTitle("My First App");
        setLayout(new FlowLayout());
        add(b);
    }
    
    public static void main(String[] s)
    {
        new frameDemo();      
    } 
}


OUTPUT


Event Handling Mouse Motion Program

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

public class eventDemo extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
    Button b = new Button("+");
    Button b2 = new Button("-");
    TextField t1 = new TextField(20);
    TextField t2 = new TextField(20);
    Label ans = new Label("Ans goes here");
    public void init()
    {
        b.addActionListener(this);
        b2.addActionListener(this);
        ans.addMouseListener(this);
        this.addMouseMotionListener(this);
        add(t1);
        add(t2);
        add(b);
        add(b2);
        add(ans);
    }
    public void actionPerformed(ActionEvent e)
    {
        int a = Integer.parseInt(t1.getText());
        int bt = Integer.parseInt(t2.getText());
        if(e.getSource()==b)
        {
            int an = a+bt;
            ans.setText(an+"");
        }
        else if(e.getSource()==b2)
        {
            int an = a-bt;
            ans.setText(an+"");
            
        }
        
    }
    
    public void mouseClicked(MouseEvent e)
    { 
        t1.setText("Clicked");
    }
    public void mouseEntered(MouseEvent e)
    {
        t1.setText("Entered");
    }
    public void mouseExited(MouseEvent e)
    {
        t1.setText("Exited");
    }
    public void mousePressed(MouseEvent e)
    {
        t1.setText("Pressed");
    }
    public void mouseReleased(MouseEvent e)
    {
        t1.setText("Released");
    }
    
    public void mouseMoved(MouseEvent e)
    {
        t2.setText(e.getX() + "," + e.getY());
    }
    public void mouseDragged(MouseEvent e)
    {
        
    }
}

/*<applet code="eventDemo" width="400" height="400"></applet>*/

OUTPUT


Write a program to make Calculator in User Interface

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

public class calc extends Applet
{
    Button[] b = new Button[16];
    TextField t = new TextField(20);
    Panel p = new Panel();
    String[] lbl= {"1","2","3","+","4","5","6","-","7","8","9","*","CE","0","/","="};
    public void init()
    {
        setLayout(new BorderLayout());
        add(t, BorderLayout.NORTH);
        
        p.setLayout(new GridLayout(4,4));
        for(int i=0;i<=15;i++)
        {
            b[i] = new Button(lbl[i]);
            p.add(b[i]);  
        }
        add(p, BorderLayout.CENTER);
        
    } 
}


/*<applet code="calc" width="200" height="400"></applet>*/


OUTPUT



















Write a Program to make AX-Ball Game

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class game extends Applet implements MouseMotionListener, Runnable
{
    int x = 0;
    int boxx = 0;
    int boxy = 0;
    Thread t;
    public void init()
    {
        t = new Thread(this);
        t.start();
        this.addMouseMotionListener(this);
    }
    
    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillRect(x, 380, 100, 20);  
        g.setColor(Color.BLACK);
        g.fillRect(boxx, boxy, 50, 50);

    }
    public void mouseMoved(MouseEvent e)
    {
        x = e.getX();
        repaint();
    }
    public void mouseDragged(MouseEvent e){}
    public void run()
    {
        try
        {
            while(true)
            {
                boxx = new Random().nextInt(350);                 
                for(int i=0;i<=330;i+=5)
                {
                    boxy = i; 
                    repaint();
                    t.sleep(10);
                }
                if((x<boxx && x+100 > boxx) || (boxx<x && boxx+50>x) || (x<boxx && x+100 >boxx))
                {
                    System.out.println("Yippiee");
                }
                                           
                else
                {
                    setBackground(Color.red);
                    break;
                }
            }
        }
        catch(Exception e){System.out.println(e);}
    }
}
 

/*<applet code="game" width="400" height="400"></applet>*/


OUTPUT



Simple Random Color Program using Applets in JAVA

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

public class abc extends Applet implements Runnable
{
    Thread t;
    Button btn;
    Checkbox cb, cb1;
    CheckboxGroup cbg;
    TextField tf = new TextField("Ankit",30);
    public void run()
    {
        try
        {
            while(true)
            {
                Random rnd = new Random();
                int r = rnd.nextInt(254);
                int g = rnd.nextInt(254);
                int b = rnd.nextInt(254);
                setBackground(new Color(r,g,b));
                t.sleep(1000);
            } 
        }
catch(Exception e)
        {
            System.out.println(e);
        }
    }
    
    public void init()
    {
        t = new Thread(this);
        t.start();
        cbg = new CheckboxGroup();
        btn = new Button("Click Me!");
        cb = new Checkbox("Hindi", false, cbg);
        cb1 = new Checkbox("English", false, cbg);
        
        setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        add(btn);
        add(cb);
        add(cb1);
        add(new Checkbox("French",true, cbg));
        add(tf);
    }
        
      public void paint(Graphics g)
    {
      Color redc = new Color(255, 0, 0);
        g.fillRect(100,200,100,100);  
        g.setColor(Color.blue);

        g.fillRect(300,300,50,50);
        g.drawOval(0, 0, 200, 100);
    g.fillOval(255,255,255,255);
    }
}

/*
<applet code="abc" width="500" height="500"></applet>


*/

 OutPut













Followers