Circle Programming
import java.awt.*;
class JStarJavaExample extends JFrame
{
public void paint(Graphics gr)
{
super.paint(gr);
int xPnts[] = {50, 54, 72, 52, 60, 40, 15, 28, 9, 32, 42};
int yPnts[] = {50, 62, 68, 80, 105, 85, 102, 75, 58, 60, 38};
gr.drawPolygon(xPnts, yPnts, xPnts.length);
}
public static void main(String[] as)
{
JStarJavaExample frm = new JStarJavaExample();
frm.setSize(90, 150);
frm.setVisible(true);
}
}
___________________________________________________________________________
import java.awt.*;
import javax.swing.*;
class star extends JPanel
{
public void paint(Graphics g){
int[] a={75,100,75,50};
int[] b={50,90,140,90};
g.fillPolygon(a,b,4);
int[] c={75,20,5,60};
int[] d={140,140,190,190};
g.fillPolygon(c,d,4);
int[] e={75,129,144,90};
int[] f={140,140,190,190};
g.fillPolygon(e,f,4);
}
public static void main(String[] args) {
JFrame frame= new JFrame();
frame.getContentPane().add(new star());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(520,500);
frame.setVisible(true);
}
}
________________________________________________________________________________
Matrix MultiPlication
__________________
class matrixmultiplication
{
public static void main(String args[])
{
int a[][]={{1,2,3},{2,5,6},{1,1,1}};
int b[][]={{1,2,3},{2,5,6},{1,1,1}};
int c[][]=new int[3][3];
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
------------------------------------------------------------------------------------------------------------------------
Menu Test
_________
import javax.swing.*;
import java.awt.event.*;
class MenuExample extends JFrame
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
JMenuBar mb=new JMenuBar();
public MenuExample()
{
super("Menu and MenuItem Example");
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
i5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null, "Item 5 clicked");
}
});
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
setJMenuBar(mb);
}
public static void main(String args[])
{
MenuExample f = new MenuExample();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
_________________________________________________________________________________
Button Test
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends JFrame implements ActionListener, TextListener
{
JLabel l1;
TextField t1;
public ButtonTest(String title)
{
super(title);
Container c = getContentPane();
setLayout(null);
/* JButton b=new JButton("Click Here");
b.setBounds(150,100,95,30);
b.addActionListener(this);
c.add(b);
*/
l1=new JLabel("");
l1.setBounds(50,100, 200,30);
c.add(l1);
t1=new TextField("");
t1.setBounds(50,70, 200,30);
t1.addActionListener(this);
c.add(t1);
/*
JTextArea ta;
ta=new JTextArea("");
ta.setBorder(BorderFactory.createLineBorder(Color.black, 2));
ta.setBounds(50,130, 200,100);
c.add(ta);
*/
}
public void actionPerformed(ActionEvent e){
// JOptionPane.showMessageDialog(null, "Test","Button clicked...",JOptionPane.WARNING_MESSAGE);
/*
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
*/
l1.setText(t1.getText());
}
public static void main(String[] args) {
ButtonTest f=new ButtonTest("Button Example");
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
________________________________________________________________________________
Array List
------------
import java.util.*;
class MyClass {
public static void main(String[] args) {
ArrayList myNumbers = new ArrayList();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
Iterator it=myNumbers.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
_________________________________________________________________________________
Link List
________
import java.util.*;
class MyClass {
public static void main(String[] args) {
LinkedList cars = new LinkedList();
cars.add("Lamborghini");
cars.add("Ferrari");
cars.add("Buggati");
cars.add("Hennesey");
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}
}
}
__________________________________________________________________________
Progress Bar
__________
import javax.swing.*;
public class ProgressBarExample extends JFrame{
JProgressBar jb;
int i=0,num=0;
ProgressBarExample(){
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null);
}
public void iterate(){
while(i<=2000){
jb.setValue(i);
i=i+20;
try{Thread.sleep(150);}catch(Exception e){}
}
}
public static void main(String[] args) {
ProgressBarExample m=new ProgressBarExample();
m.setVisible(true);
m.iterate();
}
}
__________________________________________________________________________
Radio Button
___________
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class RadioButtonTest extends JFrame implements ItemListener {
// radiobuttons
static JRadioButton b1, b2;
// create a label
static JLabel l1;
public RadioButtonTest(String title)
{
super(title);
Container c = getContentPane();
// set layout of frame
c.setLayout(new FlowLayout());
// create a new label
JLabel l = new JLabel("What is your qualification?");
l1 = new JLabel("Graduate selected");
// create Radio buttons
b1 = new JRadioButton("Graduate");
b2 = new JRadioButton("Postgraduate");
// create a button group
ButtonGroup bg = new ButtonGroup();
// add item listener
b1.addItemListener(this);
b2.addItemListener(this);
// add radio buttons to button group
bg.add(b1);
bg.add(b2);
b1.setSelected(true);
// add button and label to panel
c.add(l);
c.add(b1);
c.add(b2);
c.add(l1);
}
// main class
public static void main(String[] args)
{
// create a new frame
RadioButtonTest f = new RadioButtonTest("Radio button test...");
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == b1) {
if (e.getStateChange() == 1) {
l1.setText("Graduate selected");
}
}
else {
if (e.getStateChange() == 1) {
l1.setText("Postgraduate selected");
}
}
}
}
_________________________________________________________________________________
File
_____
import java.io.*;
class FileTest
{
public static void main(String args[]) throws IOException
{
File f = new File(args[0]);
if(f.exists())
{
System.out.println("Size:"+f.length());
if(f.isFile())
{
f.setReadOnly();
System.out.println("readable:"+f.canRead());
System.out.println("writable:"+f.canWrite());
}
else if(f.isDirectory())
{
System.out.println("Selected location is a directory");
String files[] = f.list();
for (int i=0; i< files.length;i++)
System.out.println(files[i]);
//System.out.println(f.getAbsolutePath());
}
}
else
{
f.createNewFile();
}
}
}
------------------------------------------------------------------------------------------------------------------
Ovaltest
-----------
import java.awt.*;
public class OvalTest extends Frame
{
public OvalTest()
{
setTitle("1st Program");
setSize(600, 600);
setVisible(true);
}
public void paint(Graphics g)
{
int x,w;
for (x=50,w=400;x<400;x=x+10,w=w-20)
{
if((x/10)%2==1)
{
g.setColor(Color.white);
g.fillOval(x,x,w,w);
}
else
{
g.setColor(Color.black);
g.fillOval(x,x,w,w);
}
}
}
public static void main(String args[])
{
new OvalTest();
}
}
---------------------------------------------------------------------------------------------------------------------
ComboExample
_____________
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class ComboExample extends JFrame implements ItemListener{
// label
JLabel l, l1;
// combobox
JComboBox c1;
public ComboExample(String title)
{
super(title);
Container c = getContentPane();
c.setLayout(new FlowLayout());
// array of string contating cities
String s1[] = { "Select your city","Jalpaiguri", "Mumbai", "Noida", "Kolkata", "New Delhi" };
// create checkbox
c1 = new JComboBox(s1);
// add ItemListener
c1.addItemListener(this);
// create labels
l = new JLabel("select your city ");
l1 = new JLabel("");
// set color of text
l.setForeground(Color.red);
l1.setForeground(Color.blue);
c.add(l);
// add combobox to panel
c.add(c1);
c.add(l1);
}
// main class
public static void main(String[] args)
{
// create a object
ComboExample f = new ComboExample("Combobox Example");
// set layout of frame
// set the size of frame
f.setSize(700, 200);
f.show();
}
public void itemStateChanged(ItemEvent e)
{
// if the state combobox is changed
// if (e.getSource() == c1) {
l1.setText(c1.getSelectedItem() + " selected");
// }
}
}
--------------------------------------------
import javax.swing.*;
class jframe {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setBounds(100,200,500,500);
frame.setTitle("I am .../");
ImageIcon im = new ImageIcon("bg.jpg");
frame.setIconImage(im.getImage());
frame.setResizable(false);
Container c=frame.getContentPane();
c.setLayout(null);
// c.setBackground(Color.GREEN);
JLabel label = new JLabel("USERname");
label.setText("Password");
label.setBounds(100,100,100,30);
c.add(label);
Font
}
}
----------------------------------------------------------------------
import java.awt.*;
class circle extends Frame
{
public circle(){
setTitle("Circle");
setSize(520,480);
setVisible(true);
}
public void paint(Graphics g){
//a=position,b=width,height.
int a,b;
for (a=50,b=400;a<400;a+=10,b-=20){
if((a/10)%2==1){
g.setColor(Color.white);
g.fillOval(a,a,b,b);
}
else{
g.setColor(Color.black);
g.fillOval(a,a,b,b);
}
}
}
public static void main(String args[]){
new circle();
}
}
________________________________________________________________________________
Star
|------
import javax.swing.*; import java.awt.*;
class JStarJavaExample extends JFrame
{
public void paint(Graphics gr)
{
super.paint(gr);
int xPnts[] = {50, 54, 72, 52, 60, 40, 15, 28, 9, 32, 42};
int yPnts[] = {50, 62, 68, 80, 105, 85, 102, 75, 58, 60, 38};
gr.drawPolygon(xPnts, yPnts, xPnts.length);
}
public static void main(String[] as)
{
JStarJavaExample frm = new JStarJavaExample();
frm.setSize(90, 150);
frm.setVisible(true);
}
}
___________________________________________________________________________
import java.awt.*;
import javax.swing.*;
class star extends JPanel
{
public void paint(Graphics g){
int[] a={75,100,75,50};
int[] b={50,90,140,90};
g.fillPolygon(a,b,4);
int[] c={75,20,5,60};
int[] d={140,140,190,190};
g.fillPolygon(c,d,4);
int[] e={75,129,144,90};
int[] f={140,140,190,190};
g.fillPolygon(e,f,4);
}
public static void main(String[] args) {
JFrame frame= new JFrame();
frame.getContentPane().add(new star());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(520,500);
frame.setVisible(true);
}
}
________________________________________________________________________________
Matrix MultiPlication
__________________
class matrixmultiplication
{
public static void main(String args[])
{
int a[][]={{1,2,3},{2,5,6},{1,1,1}};
int b[][]={{1,2,3},{2,5,6},{1,1,1}};
int c[][]=new int[3][3];
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
------------------------------------------------------------------------------------------------------------------------
Menu Test
_________
import javax.swing.*;
import java.awt.event.*;
class MenuExample extends JFrame
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
JMenuBar mb=new JMenuBar();
public MenuExample()
{
super("Menu and MenuItem Example");
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
i5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null, "Item 5 clicked");
}
});
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
setJMenuBar(mb);
}
public static void main(String args[])
{
MenuExample f = new MenuExample();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
_________________________________________________________________________________
Button Test
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends JFrame implements ActionListener, TextListener
{
JLabel l1;
TextField t1;
public ButtonTest(String title)
{
super(title);
Container c = getContentPane();
setLayout(null);
/* JButton b=new JButton("Click Here");
b.setBounds(150,100,95,30);
b.addActionListener(this);
c.add(b);
*/
l1=new JLabel("");
l1.setBounds(50,100, 200,30);
c.add(l1);
t1=new TextField("");
t1.setBounds(50,70, 200,30);
t1.addActionListener(this);
c.add(t1);
/*
JTextArea ta;
ta=new JTextArea("");
ta.setBorder(BorderFactory.createLineBorder(Color.black, 2));
ta.setBounds(50,130, 200,100);
c.add(ta);
*/
}
public void actionPerformed(ActionEvent e){
// JOptionPane.showMessageDialog(null, "Test","Button clicked...",JOptionPane.WARNING_MESSAGE);
/*
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
*/
l1.setText(t1.getText());
}
public static void main(String[] args) {
ButtonTest f=new ButtonTest("Button Example");
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
________________________________________________________________________________
Array List
------------
import java.util.*;
class MyClass {
public static void main(String[] args) {
ArrayList myNumbers = new ArrayList();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
Iterator it=myNumbers.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
_________________________________________________________________________________
Link List
________
import java.util.*;
class MyClass {
public static void main(String[] args) {
LinkedList cars = new LinkedList();
cars.add("Lamborghini");
cars.add("Ferrari");
cars.add("Buggati");
cars.add("Hennesey");
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}
}
}
__________________________________________________________________________
Progress Bar
__________
import javax.swing.*;
public class ProgressBarExample extends JFrame{
JProgressBar jb;
int i=0,num=0;
ProgressBarExample(){
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null);
}
public void iterate(){
while(i<=2000){
jb.setValue(i);
i=i+20;
try{Thread.sleep(150);}catch(Exception e){}
}
}
public static void main(String[] args) {
ProgressBarExample m=new ProgressBarExample();
m.setVisible(true);
m.iterate();
}
}
__________________________________________________________________________
Radio Button
___________
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class RadioButtonTest extends JFrame implements ItemListener {
// radiobuttons
static JRadioButton b1, b2;
// create a label
static JLabel l1;
public RadioButtonTest(String title)
{
super(title);
Container c = getContentPane();
// set layout of frame
c.setLayout(new FlowLayout());
// create a new label
JLabel l = new JLabel("What is your qualification?");
l1 = new JLabel("Graduate selected");
// create Radio buttons
b1 = new JRadioButton("Graduate");
b2 = new JRadioButton("Postgraduate");
// create a button group
ButtonGroup bg = new ButtonGroup();
// add item listener
b1.addItemListener(this);
b2.addItemListener(this);
// add radio buttons to button group
bg.add(b1);
bg.add(b2);
b1.setSelected(true);
// add button and label to panel
c.add(l);
c.add(b1);
c.add(b2);
c.add(l1);
}
// main class
public static void main(String[] args)
{
// create a new frame
RadioButtonTest f = new RadioButtonTest("Radio button test...");
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == b1) {
if (e.getStateChange() == 1) {
l1.setText("Graduate selected");
}
}
else {
if (e.getStateChange() == 1) {
l1.setText("Postgraduate selected");
}
}
}
}
_________________________________________________________________________________
File
_____
import java.io.*;
class FileTest
{
public static void main(String args[]) throws IOException
{
File f = new File(args[0]);
if(f.exists())
{
System.out.println("Size:"+f.length());
if(f.isFile())
{
f.setReadOnly();
System.out.println("readable:"+f.canRead());
System.out.println("writable:"+f.canWrite());
}
else if(f.isDirectory())
{
System.out.println("Selected location is a directory");
String files[] = f.list();
for (int i=0; i< files.length;i++)
System.out.println(files[i]);
//System.out.println(f.getAbsolutePath());
}
}
else
{
f.createNewFile();
}
}
}
------------------------------------------------------------------------------------------------------------------
Ovaltest
-----------
import java.awt.*;
public class OvalTest extends Frame
{
public OvalTest()
{
setTitle("1st Program");
setSize(600, 600);
setVisible(true);
}
public void paint(Graphics g)
{
int x,w;
for (x=50,w=400;x<400;x=x+10,w=w-20)
{
if((x/10)%2==1)
{
g.setColor(Color.white);
g.fillOval(x,x,w,w);
}
else
{
g.setColor(Color.black);
g.fillOval(x,x,w,w);
}
}
}
public static void main(String args[])
{
new OvalTest();
}
}
---------------------------------------------------------------------------------------------------------------------
ComboExample
_____________
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class ComboExample extends JFrame implements ItemListener{
// label
JLabel l, l1;
// combobox
JComboBox c1;
public ComboExample(String title)
{
super(title);
Container c = getContentPane();
c.setLayout(new FlowLayout());
// array of string contating cities
String s1[] = { "Select your city","Jalpaiguri", "Mumbai", "Noida", "Kolkata", "New Delhi" };
// create checkbox
c1 = new JComboBox(s1);
// add ItemListener
c1.addItemListener(this);
// create labels
l = new JLabel("select your city ");
l1 = new JLabel("");
// set color of text
l.setForeground(Color.red);
l1.setForeground(Color.blue);
c.add(l);
// add combobox to panel
c.add(c1);
c.add(l1);
}
// main class
public static void main(String[] args)
{
// create a object
ComboExample f = new ComboExample("Combobox Example");
// set layout of frame
// set the size of frame
f.setSize(700, 200);
f.show();
}
public void itemStateChanged(ItemEvent e)
{
// if the state combobox is changed
// if (e.getSource() == c1) {
l1.setText(c1.getSelectedItem() + " selected");
// }
}
}
--------------------------------------------
import javax.swing.*;
class jframe {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setBounds(100,200,500,500);
frame.setTitle("I am .../");
ImageIcon im = new ImageIcon("bg.jpg");
frame.setIconImage(im.getImage());
frame.setResizable(false);
Container c=frame.getContentPane();
c.setLayout(null);
// c.setBackground(Color.GREEN);
JLabel label = new JLabel("USERname");
label.setText("Password");
label.setBounds(100,100,100,30);
c.add(label);
Font
}
}
Comments
Post a Comment