implementation of stack in java - prasanthmani€¦  · web viewtitle //* implementation of stack...

48
//* Implementation of Stack in Java *// import java.util.*; public class StackOperation { public static void main(String []args) { String s1="a"; String s2="b"; String s3="c"; String s4="d"; String s5="e"; Stack s=new Stack(); System.out.println("\n\nPUSHING ELEMENTS TO STACK"); System.out.println("----------------------------"); System.out.println("ELEMENT NAME POSITION"); System.out.println(s.push(s1)+"\t\t "+s.search(s1)); System.out.println(s.push(s2)+"\t\t "+s.search(s2)); System.out.println(s.push(s3)+"\t\t "+s.search(s3)); System.out.println(s.push(s4)+"\t\t "+s.search(s4)); System.out.println(s.push(s5)+"\t\t "+s.search(s5)); System.out.println("\nTop most element in the stack: "+s.peek()); System.out.println("\n\n\nNEW POSITION OF ELEMENTS"); System.out.println("----------------------------"); System.out.println("ELEMENT NAME POSITION"); System.out.println(s1+"\t\t "+s.search(s1)); System.out.println(s2+"\t\t "+s.search(s2)); System.out.println(s3+"\t\t "+s.search(s3)); System.out.println(s4+"\t\t "+s.search(s4)); System.out.println(s5+"\t\t "+s.search(s5)); System.out.println("\nTop most element in the stack: "+s.peek()); System.out.println("\n\n\nPOPING ELEMENTS FROM STACK"); System.out.println("----------------------------"); System.out.println("Element recently popped out: "+s.pop()); System.out.println("\n\n\nNEW POSITION OF ELEMENTS"); System.out.println("----------------------------"); System.out.println("ELEMENT NAME POSITION"); System.out.println(s1+"\t\t "+s.search(s1)); System.out.println(s2+"\t\t "+s.search(s2)); System.out.println(s3+"\t\t "+s.search(s3)); System.out.println(s4+"\t\t "+s.search(s4)); System.out.println(s5+"\t\t "+s.search(s5)); System.out.println("\nTop most element in the stack: "+s.peek()); if(s.empty()) {

Upload: phamquynh

Post on 04-Sep-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Implementation of Stack in Java *//

import java.util.*;

public class StackOperation {public static void main(String []args) {String s1="a";String s2="b";String s3="c";String s4="d";String s5="e";Stack s=new Stack();

System.out.println("\n\nPUSHING ELEMENTS TO STACK");System.out.println("----------------------------");System.out.println("ELEMENT NAME POSITION");System.out.println(s.push(s1)+"\t\t "+s.search(s1));System.out.println(s.push(s2)+"\t\t "+s.search(s2));System.out.println(s.push(s3)+"\t\t "+s.search(s3));System.out.println(s.push(s4)+"\t\t "+s.search(s4));System.out.println(s.push(s5)+"\t\t "+s.search(s5));System.out.println("\nTop most element in the stack: "+s.peek());System.out.println("\n\n\nNEW POSITION OF ELEMENTS");System.out.println("----------------------------");System.out.println("ELEMENT NAME POSITION");System.out.println(s1+"\t\t "+s.search(s1));System.out.println(s2+"\t\t "+s.search(s2));System.out.println(s3+"\t\t "+s.search(s3));System.out.println(s4+"\t\t "+s.search(s4));System.out.println(s5+"\t\t "+s.search(s5));System.out.println("\nTop most element in the stack: "+s.peek());System.out.println("\n\n\nPOPING ELEMENTS FROM STACK");System.out.println("----------------------------");System.out.println("Element recently popped out: "+s.pop());System.out.println("\n\n\nNEW POSITION OF ELEMENTS");System.out.println("----------------------------");System.out.println("ELEMENT NAME POSITION");System.out.println(s1+"\t\t "+s.search(s1));System.out.println(s2+"\t\t "+s.search(s2));System.out.println(s3+"\t\t "+s.search(s3));System.out.println(s4+"\t\t "+s.search(s4));System.out.println(s5+"\t\t "+s.search(s5));System.out.println("\nTop most element in the stack: "+s.peek());if(s.empty()) {System.out.println("\n\n\n\t\t\tWARNING: The stack is empty...");}}}

Page 2: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 3: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Implementation of Queue in Java *//

import java.util.*;

public class Queue_program {public static void main(String []args) {Queue q=new LinkedList();

String s[]={"a","b","c","d","e"};for(int i=0;i<s.length;i++) {q.add(s);System.out.println("Element added is:"+s[i]);}for(int i=0;i<s.length;i++) {q.remove();System.out.println("Elements removed is:"+s[i]);}}}

Page 4: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 5: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Creating our own Date class in Java *//

package PRG;import java.util.*;import java.io.*;

public class MyDate {Date d=new Date();

public int min() {return d.getMinutes();}public int Month() {return d.getMonth();}public int Year() {return d.getYear();}public int Day() {return d.getDay();}public int Hours() {return d.getHours();}public int Minutes() {return d.getMinutes();}public int Seconds() {return d.getSeconds();}}

Page 6: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Implementing our own Date class in Java *//

import PRG.*;import java.util.*;

public class DateSample {public static void main (String args[]) {MyDate m=new MyDate();

System.out.print("\nThe month is: "+m.Month());System.out.print("\nThe year is: "+m.Year());System.out.print("\nThe day is: "+m.Day());System.out.print("\nThe hour is: "+m.Hours());System.out.print("\nThe minutes is: "+m.Minutes());System.out.print("\nThe second is: "+m.Seconds());}}

Page 7: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 8: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Using basic symbols in Applet *//

/*<applet code = "MyApplet.class" height="500" width="700"> </applet>*/

import java.applet.*;import java.awt.*;public class MyApplet extends Applet {public void init() {String s="Sridben applet started";}public void paint (Graphics g) {String s="INDIA";setBackground(Color.LIGHT_GRAY);g.setColor(Color.RED);g.drawString("Hello World",400,500);g.setColor(Color.cyan);g.fillRect(10,20,50,50);g.setColor(Color.GREEN);g.fillOval(50, 100, 50, 50);g.setColor(Color.PINK);g.fillRoundRect(200, 300, 40, 50,12,13);}}

Page 9: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 10: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Complie:

//* Program for implementing Dynamic Polymorphism *//

Page 11: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

class Base {int a,b;Base() {System.out.println("\nWithout parameter constructor called");}Base(int a,int b) {this.a=a;this.b=b;System.out.println("\nWith parameter constructor called");System.out.println("The values are: "+a+" "+b);}void add(int a,int b) {int c=a+b;System.out.println("\nThe add method returned originally: "+c);}}

class Derived extends Base {void add(int a,int b) {int e=a-b;System.out.println("\nThe over-ridded add() method: "+e);}public static void main(String args[]) {Base b1=new Base();Base b2 = new Base(20,50);Derived d=new Derived();b1.add(10,6);b2.add(10, 6);d.add(10,6);}}

Page 12: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 13: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Threads in complex number *//

public class Complex extends Thread {public Complex () {this.re = 0;this.im = 0;}public Complex(double re, double im) {this.re = re;this.im = im;try {th0.start();System.out.print("\nNO: of Thread active during [thread main] "+t.activeCount());th0.run();th0.sleep(2000);System.out.print("\nNO: of Thread active during [thread0] "+th0.activeCount());th0.stop();th1.start();th1.run();th2.start();th2.run();System.out.print("\nNO: of Thread active during [thread1] "+th1.activeCount());th1.stop();th2.stop();System.out.print("\nNO: of Thread active during [thread2] "+th2.activeCount());th0.start();th1.start();th2.start();th2.run();th2.stop();}catch(Exception e) {}}public Complex(Complex input) {this.re = input.getRe();this.im = input.getIm();}Thread th0=new Thread();Thread th1=new Thread();Thread th2=new Thread();Thread t=new Thread();public double getRe() {return this.re;}public double getIm() {return this.im;}public void setRe(double re) {this.re = re;}

Page 14: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

public void setIm(double im) {this.im = im;}public Complex add(Complex op) {System.out.println("");Complex result = new Complex();

result.setRe(this.re + op.getRe());result.setIm(this.im + op.getIm());t.setName("ADD THREAD called");System.out.println(t);return result;}public Complex sub(Complex op) {System.out.println("");Complex result = new Complex();

result.setRe(this.re - op.getRe());result.setIm(this.im - op.getIm());t.setName("SUBTRACTION THREAD called");System.out.println(t);return result;}public Complex mul(Complex op) {System.out.println("");Complex result = new Complex();

result.setRe(this.re * op.getRe() - this.im * op.getIm());result.setIm(this.re * op.getIm() + this.im * op.getRe());t.setName("MULTIPLICATION THREAD called");System.out.println(t);return result;}public Complex div(Complex op) {System.out.println("");Complex result = new Complex(this);

double opNormSq = op.getRe()*op.getRe()+op.getIm()*op.getIm();result.setRe(result.getRe() / opNormSq);result.setIm(result.getIm() / opNormSq);t.setName("DIVISION THREAD called");System.out.println(t);return result;}public Complex fromPolar(double magnitude, double angle) {System.out.println("");Complex result = new Complex();

result.setRe(magnitude * Math.cos(angle));result.setIm(magnitude * Math.sin(angle));t.setName("POLAR FORM THREAD called");System.out.println(t);

Page 15: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

return result;}public double getNorm() {System.out.println("");t.setName("NORMAL FORM THREAD called");System.out.print(t);return Math.sqrt(this.re * this.re + this.im * this.im);}public String toString() {if (this.re == 0) {if (this.im == 0) {return "0";} else {return (this.im + "i");}} else {if (this.im == 0) {return String.valueOf(this.re);} else if (this.im < 0) {return(this.re + "" + this.im + "i");} else {return(this.re + "+" + this.im + "i");}}}private double re;private double im;public static void main(String argv[]) {Complex c = new Complex();Thread th=new Thread();

System.out.print(c);Complex a = new Complex(3, 4);Complex b = new Complex(1, -100);th.setName("MAIN THREAD");System.out.print("\n\nInitial active object name ["+th.getName()+"] and active count is: "+th.activeCount()+"\n");System.out.print("\nThe Normal form by complex number is: "+a.getNorm()+"\n");System.out.print("The Polar form by complex number is: "+a.fromPolar(30,50)+"\n");System.out.print("The addition of complex number is: "+a.add(b)+"\n");System.out.print("The substraction of complex number is: "+a.sub(b)+"\n");System.out.print("The multiplication of complex number is: "+a.mul(b)+"\n");System.out.print("The division of complex number is: "+a.div(b)+"\n");System.out.print("\n The Overall output of complex number is: "+a.div(b).mul(b)+"\n");System.out.print("\n\nLast active object name ["+th.getName()+"] and active count is: "+th.activeCount()+"\n");}}

Page 16: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 17: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Reading and writing into file using DNA sequences *//

import java.io.*;

class Files {public static void main(String args[]) {try {File f=new File("sourcefile.txt");File f1=new File("destfile.txt");FileInputStream fis=new FileInputStream(f);DataInputStream dis=new DataInputStream(fis);String s=dis.readLine();StringBuffer sb=new StringBuffer(s);

String rev=sb.reverse().toString();String rep=null;System.out.print("\n\nOriginal data in the file 1: " +s);System.out.print("\n\nReversed data in the file 2: " +rev);System.out.print("\n\nReplaced characters: " + s.replace('a','?'));FileOutputStream fos=new FileOutputStream(f1);DataOutputStream dos=new DataOutputStream(fos);

dos.writeChars(rev);}catch(Exception e) {}}}

Page 18: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 19: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Calculator using applet/swing *//

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

//<applet code=Calculator height=500 width=250></applet>

public class Calculator extends JApplet {public void init() {CalculatorPanel calc=new CalculatorPanel();getContentPane().add(calc);}}

class CalculatorPanel extends JPanel implements ActionListener {JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;static JTextField result=new JTextField("0",45);static String lastCommand=null;JOptionPane p=new JOptionPane();double preRes=0,secVal=0,res;private static void assign(String no) {if((result.getText()).equals("0"))result.setText(no);else if(lastCommand=="=") {result.setText(no);lastCommand=null;}elseresult.setText(result.getText()+no);}public CalculatorPanel() {setLayout(new BorderLayout());result.setEditable(false);result.setSize(300,200);add(result,BorderLayout.NORTH);JPanel panel=new JPanel();panel.setLayout(new GridLayout(4,4));

n7=new JButton("7");panel.add(n7);n7.addActionListener(this);n8=new JButton("8");panel.add(n8);n8.addActionListener(this);n9=new JButton("9");panel.add(n9);n9.addActionListener(this);div=new JButton("/");panel.add(div);div.addActionListener(this);

Page 20: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

n4=new JButton("4");panel.add(n4);n4.addActionListener(this);n5=new JButton("5");panel.add(n5);n5.addActionListener(this);n6=new JButton("6");panel.add(n6);n6.addActionListener(this);mul=new JButton("*");panel.add(mul);mul.addActionListener(this);n1=new JButton("1");panel.add(n1);n1.addActionListener(this);n2=new JButton("2");panel.add(n2);n2.addActionListener(this);n3=new JButton("3");panel.add(n3);n3.addActionListener(this);minus=new JButton("-");panel.add(minus);minus.addActionListener(this);dot=new JButton(".");panel.add(dot);dot.addActionListener(this);n0=new JButton("0");panel.add(n0);n0.addActionListener(this);equal=new JButton("=");panel.add(equal);equal.addActionListener(this);plus=new JButton("+");panel.add(plus);plus.addActionListener(this);add(panel,BorderLayout.CENTER);}public void actionPerformed(ActionEvent ae) {if(ae.getSource()==n1) assign("1");else if(ae.getSource()==n2) assign("2");else if(ae.getSource()==n3) assign("3");else if(ae.getSource()==n4) assign("4");else if(ae.getSource()==n5) assign("5");else if(ae.getSource()==n6) assign("6");else if(ae.getSource()==n7) assign("7");else if(ae.getSource()==n8) assign("8");else if(ae.getSource()==n9) assign("9");else if(ae.getSource()==n0) assign("0");else if(ae.getSource()==dot) {if(((result.getText()).indexOf("."))==-1)result.setText(result.getText()+".");

Page 21: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

}else if(ae.getSource()==minus) {preRes=Double.parseDouble(result.getText());lastCommand="-";result.setText("0");}else if(ae.getSource()==div) {preRes=Double.parseDouble(result.getText());lastCommand="/";result.setText("0");}else if(ae.getSource()==equal) {secVal=Double.parseDouble(result.getText());if(lastCommand.equals("/"))res=preRes/secVal;else if(lastCommand.equals("*"))res=preRes*secVal;else if(lastCommand.equals("-"))res=preRes-secVal;else if(lastCommand.equals("+"))res=preRes+secVal;result.setText(" "+res);lastCommand="=";}else if(ae.getSource()==mul) {preRes=Double.parseDouble(result.getText());lastCommand="*";result.setText("0");}else if(ae.getSource()==plus) {preRes=Double.parseDouble(result.getText());lastCommand="+";result.setText("0");}}}

Page 22: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 23: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Creating a class template for LinkedList *//

import java.util.*;import java.io.*;

public class MyLinkedList {LinkedList l=new LinkedList();public int size() {return l.size();}public void MyAdd(int a,Object o) {l.add(a,o);}public void MyClear() {l.clear();}public Object MyPeek() {return l.peek();}public Object MyRemove() {return l.remove();}}

Page 24: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Implementing a class template of LinkedList *//

import java.util.*;import java.io.*;

class SampleLinkedList {public static void main(String agr[]) {try {MyLinkedList m=new MyLinkedList();BufferedReader br=new BufferedReader(new InputStreamReader(System.in));System.out.print("\nEnter the number of values to be inserted: ");int n=Integer.parseInt(br.readLine());for(int i=0;i<n;i++) {System.out.print("\nEnter the elements: ");String s=br.readLine();m.MyAdd(i,s);}System.out.print("\nThe size of the linkedlist: "+m.size());System.out.print("\nThe first element of the linkedlist: "+m.MyPeek());System.out.print("\nThe element removed: "+m.MyRemove());System.out.print("\nThe size of list after removing: "+m.size());}catch(Exception e) {}}}

Page 25: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 26: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Using exception handling for array and LinkedList *//

import java.io.*;import java.util.*;

class Exception_handling {public static void main(String arg[]) {LinkedList l=new LinkedList();String a[]={"a","b","g","t","i"};try {l.add(0,a[0]);l.add(1,a[2]);l.add(2,a[3]);l.add(3,a[4]);l.add(4,'k');l.add(3,a[6]);System.out.println(l);System.out.println(l.indexOf(a[2]));//int i;//i=7/0;//System.out.println(i);//ink k[]={1,2,3};//System.out.println(k[5]);}catch(ArithmeticException e) {System.out.println("cannot divide by zero");}catch(IndexOutOfBoundsException e) {System.out.println("array range exceeded");//System.out.println(e);}}}

Page 27: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 28: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Producer consumer Application using Queue classes *//

import java.util.*;

public class ProducerConsumer {public static void main(String[] args) {Check c = new Check();Producer p1 = new Producer(c, 1);Consumer c1 = new Consumer(c, 1);

p1.start();c1.start();}}

class Check {private int contents;private boolean available = false;public synchronized int get() {while (available == false) {try {wait();} catch (InterruptedException e) { }}available = false;notifyAll();return contents;}public synchronized void put(int value) {while (available == true) {try {wait();} catch (InterruptedException e) { }}contents = value;available = true;notifyAll();}}

class Consumer extends Thread {private Check check;private int number;Queue cons=new LinkedList();

public Consumer(Check c, int number) {check = c;this.number = number;

Page 29: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

}public void run() {int value = 0;for (int i = this.number; i < 5; i++) {value = check.get();cons.add(value);System.out.println("Consumer #" + this.number + " gets: " + value+ "\nConsumer Queue #"+cons+"\n\n");}}}

class Producer extends Thread {private Check check;private int number;Queue prod=new LinkedList();

public Producer(Check c, int number) {check = c;this.number = number;}public void run() {for (int i = this.number; i < 5; i++) {check.put(i);prod.add(i);System.out.println("Producer #" + this.number + " puts: " + i+ "\nProducer Queue #"+"["+ i + "]");try {sleep((2000));} catch (InterruptedException e) { }}}}

Page 30: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 31: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

//* Simple Paint Application *//

Swing

Bundle.properties

OpenIDE-Module-Name=ColorChooser

Paint

Bundle.properties

CTL_NewCanvasAction=New CanvasCTL_SaveCanvasAction=Save CanvasOpenIDE-Module-Name=PaintUnsavedImageNameFormat=Image {0}LBL_Clear=ClearLBL_Foreground=ForegroundLBL_BrushSize=Brush SizeMSG_SaveFailed=Could not write to file {0}MSG_Overwrite={0} exists. Overwrite?MSG_Saved=Saved image to {0}MSG_CannotCreateNewCanvas=Cannot create more canvases.<folder name="Actions"><folder name="Edit"><file name="org-netbeans-paint-NewCanvasAction.instance"><attr name="instanceClass" stringvalue="org.netbeans.paint.NewCanvasAction"/></file><file name="org-netbeans-paint-SaveCanvasAction.instance"><attr name="instanceClass" stringvalue="org.netbeans.paint.SaveCanvasAction"/></file></folder></folder><folder name="Menu"><folder name="File"><attr name="SeparatorNew.instance/org-netbeans-paint-separatorBefore.instance" boolvalue="true"/><file name="org-netbeans-paint-separatorBefore.instance"><attr name="instanceClass" stringvalue="javax.swing.JSeparator"/></file><attr name="org-netbeans-paint-separatorBefore.instance/org-netbeans-paint-NewCanvasAction.shadow" boolvalue="true"/><file name="org-netbeans-paint-NewCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-NewCanvasAction.instance"/></file><attr name="org-netbeans-paint-NewCanvasAction.shadow/org-netbeans-paint-SaveCanvasAction.shadow" boolvalue="true"/><file name="org-netbeans-paint-SaveCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-SaveCanvasAction.instance"/></file><attr name="org-netbeans-paint-SaveCanvasAction.shadow/org-netbeans-paint-separatorAfter.instance" boolvalue="true"/><file name="org-netbeans-paint-separatorAfter.instance">

Page 32: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

<attr name="instanceClass" stringvalue="javax.swing.JSeparator"/></file></folder><folder name="View_hidden"/><folder name="GoTo_hidden"/></folder><folder name="Toolbars"><folder name="File"><file name="org-netbeans-paint-NewCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-NewCanvasAction.instance"/></file><attr name="org-netbeans-paint-NewCanvasAction.shadow/org-netbeans-paint-SaveCanvasAction.shadow" boolvalue="true"/><file name="org-netbeans-paint-SaveCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-SaveCanvasAction.instance"/></file></folder></folder></filesystem>Build.xml<project name="org.netbeans.paint" default="netbeans" basedir="."><description>Builds, tests, and runs the project org.netbeans.paint.</description><import file="nbproject/build-impl.xml"/></project>BrandingBundle propertiescurrentVersion=Paint Application {0}SPLASH_HEIGHT=357SPLASH_WIDTH=496SplashProgressBarBounds=1,326,494,4SplashProgressBarColor=0xFF3300SplashRunningTextBounds=33,310,300,11SplashRunningTextColor=0xFFFF00SplashRunningTextFontSize=11SplashShowProgressBar=trueModuleBundle propertiesCTL_CloseWindowAction=&Close WindowCTL_CloneDocumentAction=C&lone CanvasLBL_CloneDocumentAction=Clone CanvasLBL_SaveDocumentAction=Save CanvasCTL_RecentViewListAction=Go to P&revious CanvasCTL_CloseAllDocumentsAction=Close &All Canvas# ActionUtils.CloseAllDocumentsActionLBL_CloseAllDocumentsAction=Close All Canvas# ActionUtils.AutoHideWindowActionLBL_AutoHideWindowAction=Minimize Window# ActionUtils.CloseWindowActionLBL_CloseWindowAction=Close Window# ActionUtils.CloseAllButThisActionBuildBundle properties

Page 33: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

currentVersion=Paint Application {0}SPLASH_HEIGHT=357SPLASH_WIDTH=496SplashProgressBarBounds=1,326,494,4SplashProgressBarColor=0xFF3300SplashRunningTextBounds=33,310,300,11SplashRunningTextColor=0xFFFF00SplashRunningTextFontSize=11SplashShowProgressBar=trueModulesBundle propertiesbasic=Primary colorssvg=SVG/X11 color constantsswing=Swing constantsturquoise=Turquoise# 238,130,238violet=Violet# 245,222,179wheat=Wheat# 255,255,255white=White# 245,245,245whitesmoke=White smoke# 255,255,0yellow=Yellow# 154,205,50yellowgreen=Yellow green# Continuous palette namessatSmall=Saturated colors#SMALL_SPEC_WIDTH, SMALL_SPEC_HEIGHT, 1f, false),unsatSmall=Desaturated colors#SMALL_SPEC_WIDTH, SMALL_SPEC_HEIGHT, 0.4f, false),satSmallHoriz=Saturated colors#SMALL_SPEC_WIDTH, SMALL_SPEC_HEIGHT, 1f, true),unsatSmallHoriz=Desaturated colors#SMALL_SPEC_WIDTH, SMALL_SPEC_HEIGHT, 0.4f, true),satLarge=Saturated colors#LARGE_SPEC_WIDTH, LARGE_SPEC_HEIGHT, 1f, false),unsatLarge=Desaturated colors#LARGE_SPEC_WIDTH, LARGE_SPEC_HEIGHT, 0.4f, false),satLargeHoriz=Saturated colors#LARGE_SPEC_WIDTH, LARGE_SPEC_HEIGHT, 1f, true),unsatLargeHoriz=Desaturated colors#LARGE_SPEC_WIDTH, LARGE_SPEC_HEIGHT, 0.4f, true)chooseColor="Choose a Color"Bundle propertiesCTL_NewCanvasAction=New CanvasCTL_SaveCanvasAction=Save CanvasOpenIDE-Module-Name=PaintUnsavedImageNameFormat=Image {0}LBL_Clear=ClearLBL_Foreground=Foreground

Page 34: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

LBL_BrushSize=Brush SizeMSG_SaveFailed=Could not write to file {0}MSG_Overwrite={0} exists. Overwrite?MSG_Saved=Saved image to {0}MSG_CannotCreateNewCanvas=Cannot create more canvases.

Layer.xml<filesystem><folder name="Actions"><folder name="Edit"><file name="org-netbeans-paint-NewCanvasAction.instance"><attr name="instanceClass" stringvalue="org.netbeans.paint.NewCanvasAction"/></file><file name="org-netbeans-paint-SaveCanvasAction.instance"><attr name="instanceClass" stringvalue="org.netbeans.paint.SaveCanvasAction"/></file></folder></folder><folder name="Menu"><folder name="File"><attr name="SeparatorNew.instance/org-netbeans-paint-separatorBefore.instance" boolvalue="true"/><file name="org-netbeans-paint-separatorBefore.instance"><attr name="instanceClass" stringvalue="javax.swing.JSeparator"/></file><attr name="org-netbeans-paint-separatorBefore.instance/org-netbeans-paint-NewCanvasAction.shadow" boolvalue="true"/><file name="org-netbeans-paint-NewCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-NewCanvasAction.instance"/></file><attr name="org-netbeans-paint-NewCanvasAction.shadow/org-netbeans-paint-SaveCanvasAction.shadow" boolvalue="true"/><file name="org-netbeans-paint-SaveCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-SaveCanvasAction.instance"/></file><attr name="org-netbeans-paint-SaveCanvasAction.shadow/org-netbeans-paint-separatorAfter.instance" boolvalue="true"/><file name="org-netbeans-paint-separatorAfter.instance"><attr name="instanceClass" stringvalue="javax.swing.JSeparator"/></file></folder><folder name="View_hidden"/><folder name="GoTo_hidden"/></folder><folder name="Toolbars"><folder name="File"><file name="org-netbeans-paint-NewCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-NewCanvasAction.instance"/></file><attr name="org-netbeans-paint-NewCanvasAction.shadow/org-netbeans-paint-SaveCanvasAction.shadow" boolvalue="true"/><file name="org-netbeans-paint-SaveCanvasAction.shadow"><attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-paint-SaveCanvasAction.instance"/>

Page 35: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

</file></folder></folder>Update Trackingorg-netbeans-paint.xmlmodule codename="org.netbeans.paint"><module_version install_time="1284358495156" last="true" origin="installer" specification_version="1.0"><file crc="2006666799" name="config/Modules/org-netbeans-paint.xml"/><file crc="935566473" name="modules/org-netbeans-paint.jar"/></module_version></module>

org-netbeans-swing-colorchooser.xml

<module codename="org.netbeans.swing.colorchooser"><module_version install_time="1284358494921" last="true" origin="installer" specification_version="1.0"><file crc="935133932" name="config/Modules/org-netbeans-swing-colorchooser.xml"/><file crc="2804938730" name="modules/ext/ColorChooser-license.txt"/><file crc="210958135" name="modules/ext/ColorChooser.jar"/><file crc="2118653640" name="modules/org-netbeans-swing-colorchooser.jar"/></module_version></module\

PaintCanvas.java

package org.netbeans.paint;

import java.awt.BasicStroke;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.Point;import java.awt.RenderingHints;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import javax.swing.JComponent;

PaintTopComponent.java

package org.netbeans.paint;

import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.File;

Page 36: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JSlider;import javax.swing.JToolBar;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import org.netbeans.swing.colorchooser.ColorChooser;import org.openide.awt.StatusDisplayer;import org.openide.util.NbBundle;import org.openide.windows.TopComponent;

public class PaintTopComponent extends TopComponent implements ActionListener, ChangeListener {

private static int tcCount = 0; //A counter to limit number of simultaneously existing imagesprivate static int ct = 0; //A counter we use to provide names for new images

static int getPaintTCCount() {return tcCount;}

private final PaintCanvas canvas = new PaintCanvas(); //The component the user draws onprivate JComponent preview; //A component in the toolbar that shows the paintbrush size

/** Creates a new instance of PaintTopComponent */public PaintTopComponent() {initComponents();String displayName = NbBundle.getMessage( PaintTopComponent.class, "UnsavedImageNameFormat", new Object[] { new Integer(ct++) } );tcCount++;setName(displayName);setDisplayName(displayName);}

public void actionPerformed(ActionEvent e) {if (e.getSource() instanceof JButton) {canvas.clear();} else if (e.getSource() instanceof ColorChooser) {ColorChooser cc = (ColorChooser) e.getSource();canvas.setPaint(cc.getColor());}preview.paintImmediately(0, 0, preview.getWidth(), preview.getHeight());}

Page 37: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00

Output:

Page 38: Implementation of Stack in Java - prasanthmani€¦  · Web viewTitle //* Implementation of Stack in Java *// Author: Hanifa Last modified by: Hanifa Created Date: 9/19/2010 5:59:00