You are on page 1of 39

Experiment - 1 Aim: Write a program java to draw some shape and save it to a raw file.

Source Code:
import import import import import import MMT_Lab_Tool_Lib.*; java.awt.*; java.awt.event.*; java.awt.image.*; javax.swing.*; java.io.*;

public class SaveImageDemo extends Thread { ImageDrg imgDrg = new ImageDrg(); public SaveImageDemo() { JButton b = new JButton("Save"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { saveImage(); } }); imgDrg.addButton(b); imgDrg.setVisible(true); } void saveImage() { try { new Thread() { public void run() { JFileChooser fc = new JFileChooser(); int rv = fc.showOpenDialog(imgDrg); BufferedImage img = (BufferedImage)imgDrg.getImage(); Raster r = img.getRaster(); int[] pixValue = new int[4]; int w = img.getWidth(); int h = img.getHeight(); int i=1; try {

FileOutputStream fout = new FileOutputStream(fc.getSelectedFile()); for(int x =0; x<w; ++x) { for(int y =0; y<h; ++y) { imgDrg.setProgress(i++); r.getPixel(x,y,pixValue); fout.write(intToByte(pixValue)); } } } catch(Exception ex){} imgDrg.setProgress(0); }.start(); } catch(Exception ex){} } byte[] intToByte(int[] iArr) { byte[] bArr = new byte[iArr.length]; for(int i=0;i<iArr.length; ++i) bArr[i] = (byte)iArr[i]; return bArr; } public static void main(String[] arg) { new SaveImageDemo(); } }

Output:

Experiment – 2

setVisible(true).addButton(b).getWidth().APPROVE_OPTION). byte[] pixValue = new byte[4]. java.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { loadImage().*. { BufferedImage img = (BufferedImage)imgDrg. int rv = fc.*. Source Code: import import import import import import MMT_Lab_Tool_Lib.awt. java.showOpenDialog(imgDrg). java.image.swing. if(rv == fc.getRaster(). int w = img. public LoadImageDemo() { JButton b = new JButton("Load Image").awt. javax. imgDrg. imgDrg.*.awt.*. } void loadImage() { try { new Thread() { public void run() { JFileChooser fc = new JFileChooser(). .event. java.*.*.getImage(). public class LoadImageDemo { ImageDrg imgDrg = new ImageDrg().io. b.Aim: Write a program java to draw shapes from saved file in previous experiment. WritableRaster r = img. } }).

for(int i=0. y<h. } } } catch(Exception ex){} } imgDrg. r. ++i) iArr[i] = bArr[i]. } }.y.start(). ++x) { for(int y =0. imgDrg. } } Output: . } catch(Exception ex){} } int[] byteToInt(byte[] bArr) { int[] iArr = new int[bArr.i<iArr.byteToInt(pixValue)).repaint().setProgress(0). ++y) { fin. imgDrg. int i=1.length].setPixel(x. } public static void main(String[] arg) { new LoadImageDemo().setProgress(i++).getSelectedFile()).length. x<w.getHeight().read(pixValue).int h = img. for(int x =0. try { FileInputStream fin = new FileInputStream(fc. return iArr.

Experiment – 3 .

short reserved2 = 0. height = readInt(). import javax. readHeader(). int bitmapOffset = 0. } void getBitmapHeader() throws Exception { dibHeaderSize = readInt(). int horzResolution = 0. //===== header field ======== short fileType = 0. getBitmapHeader(). planes = readShort(). short reserved1 = 0. int colorsImportant = 0.*. public class BMPInfo { File bmpFile = null. Source Code: import java.Aim: Write a program in java to read a bmp file and display header and other information except pixel data.swing.*. bitsPerPixel = readShort(). } void readHeader() throws Exception { fileType = readShort(). reserved2 = readShort(). int width = 0. int fileSize = 0. width = readInt(). int sizeOfBitmap = 0. int dibHeaderSize = 0. int vertResolution = 0. public BMPInfo(File file) throws Exception { bmpFile = file. int height = 0.io. short planes = 0. short bitsPerPixel = 0. fin = new FileInputStream(file). reserved1 = readShort(). . bitmapOffset = readInt(). int colorsUsed = 0. FileInputStream fin = null. if (fileType != 0x4d42) throw new Exception("Not a BMP file"). fileSize = readInt(). int compression = 0.

append("\nCompression Type : " + compression).read(). fin.append("\nFile Size : " + fileSize). colorsImportant = readInt().read().append("\nImage Offset : " + bitmapOffset). buffer. return ((b4 << 24) + (b3 << 16) + (b2 << 8) + (b1 << 0)). buffer. buffer.append("File Type : " + fileType). sizeOfBitmap = readInt(). buffer. buffer.swing.append("\nNo Of Color Plane : " + planes).read(). return (short) ((b2 << 8) + b1).read(). colorsUsed = readInt(). buffer.toString(). buffer. buffer.append("\nVertical Resolution : " + vertResolution). } public String toString() { StringBuffer buffer = new StringBuffer(). int b2 = fin. } public static void main(String[] arg) { JFileChooser fc = new JFileChooser().append("\nBitmap size : " + sizeOfBitmap). fin. fin. vertResolution = readInt().append("\nImage Height : " + height). buffer.append("\nNumber Of Important Colors : " + colorsImportant). buffer. buffer.append("\nHorizontal Resolution : " + horzResolution). horzResolution = readInt(). buffer. fc. buffer. .append("\nNumber Of Colors : " + colorsUsed).append("\nDIB Header Size : " + dibHeaderSize). return buffer.append("\nBits Per Pixel : " + bitsPerPixel).append("\nImage Width : " + width).compression = readInt().setFileFilter(new javax. } int readInt() throws IOException { int int int int } short readShort() throws IOException { int b1 = fin.read().filechooser.FileFilter() { public boolean accept(File f) { b1 b2 b3 b4 = = = = fin. buffer.read().

JOptionPane.DEFAULT_OPTION.if(f. } public String getDescription() { return "BMP Image Files". } catch(Exception ex) { JOptionPane.nu ll). } }).null. .event. Source: Experiment – 4 Write a program in java to read a bmp file and display the import java. { try { BMPInfo info = new BMPInfo(fc.info. return false. } } } } Output: File Name : bee. if(fc.*.showOpenDialog(null) == fc."Invalid file").awt.isDirectory() || f.JOptionPane.INFORMATION_MESSAGE.null. import java.toLowerCase().showMessageDialog(null.showOptionDialog(null.endsWith(".getSelectedFile()).toString().awt.*.bmp")) return true.JOptionPane.getName()."BMP Header Information".APPROVE_OPTION).bmp File Type : 19778 File Size : 178578 Image Offset : 1078 DIB Header Size : 40 Image Width : 500 Image Height : 355 No Of Color Plane : 1 Bits Per Pixel : 8 Compression Type : 0 Bitmap size : 177500 Horizontal Resolution : 0 Vertical Resolution : 0 Number Of Colors : 256 Number Of Important Colors : 256 Aim: image.

new JLabel(img). } catch(Exception ex) { JOptionPane. { try { ImageIcon img = new ImageIcon(ImageIO. } } } } Output: .bmp")) return true.import import import import java. if(fc.isDirectory() || f. javax."Invalid file").swing.JOptionPane.showOptionDialog(null.read(fc.*.filechooser.fc.getName().getName().awt.toLowerCase().swing.DEFAULT_OPTION.showOpenDialog(null) == fc.getSelectedFile())).setFileFilter(new javax. } public String getDescription() { return "BMP Image Files".showMessageDialog(null.INFOR MATION_MESSAGE.JOptionPane.null.endsWith(".File.APPROVE_OPTION).image.*. fc.null).imageio. java. return false. JOptionPane. } }).*.io. public class DisplayBMP { public static void main(String[] arg) { JFileChooser fc = new JFileChooser().null.FileFilter() { public boolean accept(File f) { if(f. javax.getSelectedFile().

.Experiment – 5 Aim: Write a program in java to read an mp3 (or any audio) file and display its header information.

private final int NUM_BYTES = 4. 12000 }. 80 }. { 48000. 40. 192. private final String[] layerLabels = { null. private final String[] versionLabels = {"MPEG Version 2. { -1. -1. -1. { 64. 96 }. -1 } }. private final int MPEG_L_1 = 3. -1. 128 }. 192. 22050. 256. private int version. { 416. 144 }. 48. "Joint Stereo (STEREO)". 160. 56. 11025 }. 320. 64. 32."1" }. 224."MPEG Version 1. 64. { -1. 64. { 160. 64 }. -1. private int sampleRate. . 160 }. -1 } }. 56.io. private int channelMode. private final int MPEG_V_1 = 3. 112 }. { 128. private final int MPEG_L_3 = 1."MPEG Version 2. "3". 80. 112. private final int NUM_BITS = 8. -1. 128. 80. 224. private final int[][] sampleTable = { { 44100. 32. private File mp3File = null. 56 }. private boolean copyrighted. { 32000. 40 }.5". 176. { 224. 8000 }. 24 }. public class MP3Header { private final int HEADER_SIZE = 4. 16 }. 384. "2".0"}. 56. 48. { 32.0". 8 }. private final int MPEG_V_25 = 0.null. private int layer. 48 }. private final int MPEG_L_2 = 2. 24000.*. 224. 160. 160. -1. 112. 144. { 96. 320. { 448. 128. { 192. 32. private final int MPEG_V_2 = 2. 96. { 256. -1 }. 48. -1. { 352."Single Channel (MONO)" }. private final String[] channelLabels = { "Stereo". 112. { 288. 256. 80. 96. 96. private final int[][] bitrateTable = { { -1. 256."Dual Channel (STEREO)". 192. { 384. private int bitRate. { 320. 16000. 128.Source Code: import java. 32 }.

findBitRate( convertToDecimal( head[2]. } if( (ind != -1) && (bitrateIndex >= 0) && (bitrateIndex <= 15) ) bitRate = bitrateTable[bitrateIndex][ind]. if( raf. 4. switch( version ) { .public MP3Header(File f) throws Exception { mp3File = f. if( version == MPEG_V_1 ) { if( layer == MPEG_L_1 ) ind = 0. else if( layer == MPEG_L_3 ) ind = 2. 4 ). 6. 7 ) ). 7 ). raf. 2 ). byte[] head = new byte[HEADER_SIZE]. version = convertToDecimal( head[1]. layer = convertToDecimal( head[1]."). findSampleRate( convertToDecimal( head[2]. } private void readHeader(long location )throws Exception { RandomAccessFile raf = new RandomAccessFile(mp3File.read( head ) != HEADER_SIZE ) throw new Exception("Error reading MP3 frame header. 3 ) ). 1. channelMode = convertToDecimal( head[3].seek( location ). 3 ). } else if( (version == MPEG_V_2) || (version == MPEG_V_25) ) { if( layer == MPEG_L_1 ) ind = 3. } private void findSampleRate( int sampleIndex ) { int ind = -1. copyrighted = bitSet( head[3]. readHeader(findHeaderLocation()). "r" ). 2. 3. else if( layer == MPEG_L_2 ) ind = 1. } private void findBitRate( int bitrateIndex ) { int ind = -1. else if( (layer == MPEG_L_2) || (layer == MPEG_L_3) ) ind = 4.

buffer. case MPEG_V_2: ind = 1. buffer. case MPEG_V_25: ind = 2. long loc = -1. raf.toString(). .append("\nCopywrited : Yes"). break.append("File Name : " + mp3File. if(matchPattern( test.2.append("\nSample Rate : " + sampleRate + "Hz"). if(matchPattern( test. break.getFilePointer() . else buffer. } } } return loc. } private long findHeaderLocation()throws Exception { RandomAccessFile raf = new RandomAccessFile(mp3File.case MPEG_V_1: ind = 0.seek(0). } if( (ind != -1) && (sampleIndex >= 0) && (sampleIndex <= 3) ) sampleRate = sampleTable[sampleIndex][ind]. "r" ). buffer.readByte(). buffer.append("\nCopywrited : No"). while( loc == -1 ) { test = raf. if(copyrighted) buffer. buffer.append("\nVersion : " + versionLabels[version]). byte test. "11111111" ) ) { test = raf.getName()).readByte(). return buffer. "111xxxxx" ) ) { loc = raf.append("\nChannel Mode : " + channelLabels[channelMode]). } public String toString() { StringBuffer buffer = new StringBuffer().append("\nLayer : " + layerLabels[layer]).

int start. NUM_BITS . . } public boolean bitSet( byte b.1 ).i .length()) && retval. } if( start > end ) { s = end.charAt(i) == '1' ) { retval = retval && bitSet( b. int end ) { byte ret = 0. } public int convertToDecimal( byte b.} public boolean matchPattern( byte b. int s = start. } else if( pattern. int bCount = 0. NUM_BITS . int e = end. String pattern ) { boolean retval = true.1 ). } } return retval.i . int pos ) { boolean retval = false. if( (start < 0) || (start >= NUM_BITS) ) { s = 0. } if( (end < 0) || (end >= NUM_BITS) ) { e = NUM_BITS . if( (pos >= 0) && (pos < NUM_BITS) ) { retval = ((b & (byte)(1 << pos)) != 0). for( int i = 0. } return retval.charAt(i) == '0' ) { retval = retval && !bitSet( b.1. (i < NUM_BITS) && (i < pattern. i++ ) { if(pattern.

JOptionPane.null).JFileChooser().swing. bCount ).DEFAULT_OPTION. } }). i <= e. int location ) { byte ret = 0.swing.null.JOptionPane.toLowerCase(). javax.isDirectory() || f.endsWith(".APPROVE_OPTION).INFORMATI ON_MESSAGE. if(rv == fc.setFileFilter(new javax.swing. } public static void main(String[] arg) throws Exception { javax.JFileChooser fc = new javax.info.e = start. } catch(Exception ex) . { try { MP3Header info = new MP3Header(fc.showOpenDialog(null). fc. return false. int rv = fc.null.swing.mp3")) return true.JOptionPane."MP3 Header Information".FileFilter() { public boolean accept(File f) { if(f. } public String getDescription() { return "MPEG Files". } for( int i = s.filechooser.javax. return ret. i++ ) { if( bitSet( b.showOptionDialog(null.getSelectedFile()).javax.swing.toString(). if( (location >= 0) && (location < NUM_BITS) ) ret = (byte)(b | (byte)(1 << location)). } return ret.getName(). i ) ) ret = setBit( ret. bCount++. } public byte setBit( byte b.swing.

public class MyCodec extends Observable { public void compress(String src. .JOptionPane. import java.swing.0 Layer : 3 Sample Rate : 44100Hz Channel Mode : Joint Stereo (STEREO) Copywrited : No 2) File Name : Kid_Story. Source Code: import java.zip. long size = f.*."Invalid file"). import java.io.*.{ javax.5 Layer : null Sample Rate : 11025Hz Channel Mode : Single Channel (MONO) Copywrited : No Experiment – 6 Aim: Write a program in java to compress a given text file.Observable.mpg Version : MPEG Version 2.util. } } } } Output: 1) File Name : Masti ki pathshala.showMessageDialog(null.length(). long count = 1. String dest) throws IOException { File f = new File(src).mp3 Version : MPEG Version 1.util.

long count = 1. } public static void main(String[] arg) throws Exception { if(arg. int c = 0.zip.io.println("Use\n…For compressing file : MyCodec -c < src file> <dest file>"). Experiment -7 Aim: Write a program in java to decompress the given file.toLowerCase(). import java.length == 3) { MyCodec c = new MyCodec(). String dest) throws IOException { File f = new File(src). notifyObservers((int)((double)count++/size * 100)). GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(dest)). if(arg[0].out.equals("-c")) { c.*. FileOutputStream out = new FileOutputStream(dest).write(c).FileInputStream in = new FileInputStream(f). while ((c = in.util. long size = f. } } else System.java abc. } in. setChanged(). import java.compress(arg[1].gz File Compressed…. arg[2]).close().*. } } Output: C :\> java –c abc. GZIPInputStream in = new GZIPInputStream(new FileInputStream(src)). .Observable.length(). out.util. public class MyCodec extends Observable { public void decompress(String src.read()) != -1) { out. Source: import java.close().

.decompress(arg[1]. arg[2])..read()) != -1) { out..awt. Source Code: import import import import import import import import import import java. out.length != 3) { System. notifyObservers(=(int)((double)count++/size * 100)). } in.imageio.event.*. } } } Output: C :\> java –d abc.event. } } if(arg.out.*. javax.equals("-d")) { c.image.*... .close().toLowerCase().. if(arg[0]. java.awt. setChanged().*.event.swing.close()..geom.length == 3) { MyCodec c = new MyCodec().*. javax..ChangeEvent.write(c).").*.int c = 0.swing. java.stream. System. javax. Experiment – 8 Aim: Create a GUI Tool to perform all the operations performed in previous experiments under a single interface form.ChangeListener.. javax.io.imageio. } public static void main(String[] arg) throws Exception { if(arg.. while ((c = in.java File Decompressed….gz abc. java.swing..*. java.awt.out..*.println("Use\n. javax.awt.println("use…\nFor decompressing file : MyCodec -d <src file> <dest file>")..

add(new BMPInfoFrame()).addActionListener(this). if(cmd. setVisible(true). setUp().equals("File Codec")) .equals("BMP File Header")) { jdp.*. setSize(400.400). } else if(cmd. } else if(cmd. public class MasterFrame extends JFrame implements ActionListener { JDesktopPane jdp = null.CENTER). b."File Codec"}. setExtendedState(MAXIMIZED_BOTH). add(jtb.add(b). String[] options = {"Drawing".setActionCommand(s).BorderLayout.add(new DrgTool()). for(String s : options) { JButton b = new JButton(s).BorderLayout. addOptionFrame().NORTH).getActionCommand().import java.util. } void setUp() { jdp = new JDesktopPane(). setDefaultCloseOperation(EXIT_ON_CLOSE). } void addOptionFrame() { JToolBar jtb = new JToolBar()."BMP File Header". b. } } public void actionPerformed(ActionEvent ae) { String cmd = ae. jtb. add(jdp.equals("Drawing")) { jdp. public MasterFrame() { super("MultiMedia Tool").

bmp")) return true.FileFilter() { public boolean accept(File f) { if(f.setFileFilter(new javax. return false.filechooser.true.toString())."). { try { infoBox.setText("Invalid file. } class BMPInfoFrame extends JInternalFrame { JTextArea infoBox = new JTextArea(). ld. fc. public BMPInfoFrame() { super("BMP Header". } } public static void main(String[] arg) { new MasterFrame(). JButton ld = new JButton("Choose BMP File"). setSize(250.toLowerCase().getName()..isDirectory() || f. } .true). } catch(Exception ex) { infoBox. setLayout(new BorderLayout()).addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser fc = new JFileChooser()..endsWith(".getSelectedFile()).{ jdp. } }).300).swing.setText(new BMPInfo(fc. } public String getDescription() { return "BMP Image Files".APPROVE_OPTION).true.showOpenDialog(null) == fc.true. if(fc.add(new CodecFrame()).

add(tb."Save"}.BorderLayout. boolean lock = false.true.400). ld. jpb. File srcFile = null.true. tb.setActionCommand(s).add(infoLabel). JProgressBar jpb = new JProgressBar(). } tb = new JToolBar().true). for(String s : cmd) { JButton ld = new JButton(s). String[] cmd = {"Compress". add(tb. } } class CodecFrame extends JInternalFrame implements ActionListener. MyCodec c = new MyCodec().Observer { JTextArea srcBox = new JTextArea(5.add(ld). add(new JScrollPane(srcBox). ld.setStringPainted(true).BorderLayout. add(new JScrollPane(infoBox). setVisible(true).setMaximum(100). setLayout(new BorderLayout()).true.CENTER).setVisible(false). . setSize(600. jpb. JToolBar tb = new JToolBar().NORTH). JLabel infoLabel = new JLabel().addActionListener(this). public CodecFrame() { super("BMP Header".SOUTH). add(ld.CENTER)."Decompress".BorderLayout.BorderLayout.NORTH).5). jpb.BorderLayout. tb.add(jpb).} } }). tb.

f. } catch(Exception ex) { . infoLabel.setText("").").getPath()..").setVisible(true).tmp").createNewFile().printStackTrace(). } }. c. infoLabel.setText("Compressing File. } public void actionPerformed(ActionEvent ae) { if(lock)return.equals("Compress")) { try { lock = true. loadSrcFile()..setVisible(false). f. File f = new File("codec. jpb.} lock = false.compress(srcFile.getName()). } catch(Exception ex){ex. } public void setProgress(int val) { if(val == 0 || val == 100) jpb.setValue(val). String cmd = ae. c.start(). if(cmd..addObserver(this).setText("Loading File. new Thread() { public void run() { try { infoLabel.. else if(val == 1) jpb.setVisible(true).getActionCommand().

setDialogTitle("Choose File"). infoLabel.decompress(srcFile. infoLabel.").getName()).printStackTrace().ex.. } } else if(cmd. .tmp").equals("Save")) { try { lock = true. infoLabel. File f = new File("codec..getPath(). new Thread() { public void run() { try { infoLabel..printStackTrace(). int rv = fc.setText("Saving File.printStackTrace(). fc. } } else if(cmd.equals("Decompress")) { try { lock = true.")..showSaveDialog(null). c. loadSrcFile().setText("Decompressing File.."). } catch(Exception ex){ex.setText("Loading File.f. try { JFileChooser fc = new JFileChooser().start().. f.} lock = false. } }.setText("").createNewFile(). } catch(Exception ex) { ex.

srcBox.length().printStackTrace(). while((c = fin.append(String. new Thread() { public void run() { try { FileInputStream fin = new FileInputStream("codec. size = new File("codec.printStackTrace(). } }.tmp"). } } catch(Exception ex) { ex. fout.setText(""). ++count.close().write(c).getSelectedFile().printStackTrace().start().if(rv == fc.read()) != -1) { fout.tmp"). } catch(Exception ex) { ex. setProgress((int) ((double)count/size * 100)). } lock = false. } fin.close(). long count = 1.APPROVE_OPTION). infoLabel.setText(""). srcBox. { srcFile = fc.valueOf((char)c)). FileOutputStream fout = new FileOutputStream(srcFile). } } catch(Exception ex) { ex. int c = 0. } .

{ srcFile = fc. srcBox. JToolBar tb = null. long count = 1. } } } } class DrgTool extends JInternalFrame implements ActionListener. int rv = fc. if(rv == fc. } f.printStackTrace().valueOf(arg. Object arg) { setProgress(Integer. } void loadSrcFile() { try { JFileChooser fc = new JFileChooser().setText("").toString())). .valueOf((char)c)).ChangeListener { MyCanvas canvas = null.length().close(). while((c = f.read()) != -1) { srcBox.} } public void update(Observable o.getSelectedFile(). JMenuBar jb = null.append(String. JSpinner sp = null. int c = 0.APPROVE_OPTION). FileInputStream f = new FileInputStream(srcFile). fc.setDialogTitle("Choose Source File"). setProgress((int)((double)count/size * 100)).showOpenDialog(null). size = srcFile. ++count. } } catch(Exception ex) { ex.

add(jpb. public DrgTool() { super("Drawing". jb = new JMenuBar().setActionCommand("SAVE_RAW").add(mit). } void setUp() { jpb = new JProgressBar().CENTER).add(mit). mit = new JMenuItem("Open BMP File"). .addActionListener(this).setVisible(false). m. setUp(). mit. setSize(400. setVisible(true).addActionListener(this). canvas = new MyCanvas(this). m.add(m). mit. setDefaultCloseOperation(DISPOSE_ON_CLOSE). mit = new JMenuItem("Save Raw File"). mit.addActionListener(this). add(new JScrollPane(canvas). jpb.SOUTH). mit. JMenuItem mit = null.setActionCommand("New").400). m = new JMenu("Home"). m.setActionCommand("OPEN_BMP").addSeparator(). m.true.add(mit).BorderLayout. jb.add(mit).setActionCommand("OPEN_RAW").BorderLayout. m. mit. JMenu m = null.true). mit = new JMenuItem("New").setStringPainted(true).JProgressBar jpb = null.true. mit. m. jpb. mit.true.addSeparator(). mit.addActionListener(this). mit = new JMenuItem("Open Raw File").

mit. mit. mit.setActionCommand("BRUSH_COLOR").add(mit).setValue(2). mit = new JMenuItem("Exit"). sp. mit. if(cmd. else if(v == 1) jpb.add(new JLabel(" Brush Size")). } public void setMaxVal(int mv) { jpb.2)).addActionListener(this).addSeparator().add(mit). mit = new JMenuItem("Brush Color mit.setValue(v). mit = new JMenuItem(" "). } public void actionPerformed(ActionEvent ae) { String cmd = ae.add(m). m. sp = new JSpinner().setActionCommand("EXIT"). m = new JMenu("Drawing").equals("BRUSH_COLOR")) { ").add(sp). mit. m. if(v == 0) jpb.setActionCommand("SAVE_BMP").setMaximum(mv). mit. mit.setLayout(new GridLayout(1. sp.addChangeListener(this). jb. setJMenuBar(jb).setVisible(false).addActionListener(this).getActionCommand(). } public void setProgress(int v) { jpb.add(mit). m. m. m. .addActionListener(this).mit = new JMenuItem("Save BMP File").setVisible(true).add(mit). mit.

int brushSize = 1.equals("OPEN_BMP")) { canvas.equals("OPEN_RAW")) { canvas.exit(0).equals("New")) { canvas.equals("SAVE_BMP")) { canvas. } canvas. } } class MyCanvas extends JPanel implements MouseListener.setValue(1). } else if(cmd.equals("EXIT")) { System.saveRawImage().parseInt(sp.equals("SAVE_RAW")) { canvas. if(i == 0) { sp.saveBMPImage().setBrushColor(c). if(c != null) canvas.null).loadBMPImage(). } else if(cmd. i++. } else if(cmd.toString()).Color c = JColorChooser.setBrushSize(i). } else if(cmd. .getValue()."".newImage(). } else if(cmd. } } public void stateChanged(ChangeEvent ce) { int i = Integer. } public static void main(String[] arg) { new DrgTool().MouseMotionListener { DrgTool baseFrame = null. } else if(cmd.loadRawImage().showDialog(this.

imgXpos.getY() . addMouseListener(this). g. WritableRaster raster = null. public MyCanvas(DrgTool dt) { super(). addMouseMotionListener(this).getWidth())/2. boolean drawFlag = false. } } public void mousePressed(MouseEvent me) { if(me.image. imgYpos = (getHeight() .getY() >= imgYpos) { drawFlag = true.getX()-imgXpos. lastPoint = new Point(me.drawImage(image.getButton() == 1) { if(me. if(image != null) { imgXpos = (getWidth() . setBackground(Color.null).black). setBrushSize(2). int imgYpos = 0. setBrushColor(Color.white). } } } public void mouseReleased(MouseEvent me) .getHeight())/2. me. Graphics2D g = null.Color brushColor = Color. baseFrame = dt.imgYpos). } public void paint(Graphics g) { super.paint(g).image.getX() >= imgXpos && me.black. newImage(). int imgXpos = 0. Point lastPoint = null.imgYpos. BufferedImage image = null.

g = (Graphics2D)image. me. g.setColor(Color. raster = image. g.getRaster().h.799. } public void newImage(int w.w-1.h-1). } public void setImage(BufferedImage img) { image = img.int h) { image = new BufferedImage(w. .getGraphics().black). repaint(). repaint(). g.Double(lastPoint.getX()-imgXpos.p)). baseFrame.BufferedImage.drawRect(0. raster = image.599). g = (Graphics2D)image.black). g. } public void mouseDragged(MouseEvent me) { if(drawFlag) { Point p = new Point(me.white). g.draw(new Line2D.setMaxVal(w*h). baseFrame.drawRect(0.getRaster(). } } public public public public void void void void mouseMoved(MouseEvent me){} mouseEntered(MouseEvent me){} mouseClicked(MouseEvent me){} mouseExited(MouseEvent me){} public void newImage() { newImage(800.0.0.getHeight().setColor(Color. g. int w = img.getY() .799.0.fillRect(0.600). g.getWidth(). lastPoint = p.getGraphics().599).{ drawFlag = false.setColor(Color.imgYpos).setMaxVal(w*h). int h = img.TYPE_3BYTE_BGR).

int h = readInt(fin). int i=1. .h).setColor(c).APPROVE_OPTION). } public void loadRawImage() { try { new Thread() { public void run() { JFileChooser fc = new JFileChooser(). g. for(int x =0. int rv = fc.read().getSelectedFile()). } public void setBrushColor(Color c) { brushColor = c. { int[] pixValue = new int[4]. int w = readInt(fin).repaint(). y<h. } public void setBrushSize(int s) { brushSize = s. g. ++x) { for(int y =0.read(). try { FileInputStream fin = new FileInputStream(fc. ++y) { pixValue[0] = fin.setStroke(new BasicStroke(s)). if(rv == fc. pixValue[1] = fin.showOpenDialog(baseFrame). newImage(w. x<w. } public Image getImage() { return image.

read(). pixValue[3] = fin.showSaveDialog(baseFrame).start(). } catch(Exception ex){} } public void saveRawImage() { try { new Thread() { public void run() { JFileChooser fc = new JFileChooser().setProgress(i++).APPROVE_OPTION). int rv = fc.close().pixValue).setProgress(0). { int[] pixValue = new int[4]. raster.h). } } fin. int w = image. y<h. } }.w). ++x) { for(int y =0.read().getSelectedFile()). } catch(Exception ex){} } baseFrame. writeInt(fout.getHeight(). writeInt(fout. ++y) . baseFrame. int h = image. int i=1. if(rv == fc.pixValue[2] = fin. x<w.setPixel(x.repaint(). for(int x =0.y. try { FileOutputStream fout = new FileOutputStream(fc. baseFrame.getWidth().

write(pixValue[1]).showOpenDialog(baseFrame).INFORMATION_MESSAGE). } public String getDescription() { return "BMP Image Files".swing. baseFrame.write(pixValue[2]).{ baseFrame. } } catch(Exception ex){} } . raster. fc.endsWith(". } } } catch(Exception ex){} } baseFrame."File Saved".write(pixValue[3]).isDirectory() || f. fout.APPROVE_OPTION).y. fout.write(pixValue[0]).setFileFilter(new javax."Raw Image".FileFilter() { public boolean accept(File f) { if(f. } }).getName().start(). } catch(Exception ex){} } public void loadBMPImage() { try { JFileChooser fc = new JFileChooser(). if(rv == fc.JOptionPane.toLowerCase().showMessageDialog(baseFrame. { setImage(ImageIO.filechooser. JOptionPane. } }.repaint(). return false.getPixel(x.read(fc. int rv = fc.setProgress(i++). fout.pixValue).bmp")) return true. fout.setProgress(0).getSelectedFile())).

isDirectory() || f. int rv = fc."File Saved". } }).read(). JOptionPane. return false.endsWith(".swing. ImageIO. String ext = "".endsWith("."bmp".f). } void writeInt(FileOutputStream fout.public void saveBMPImage() { try { JFileChooser fc = new JFileChooser().showMessageDialog(baseFrame.JOptionPane.write(image.toLowerCase(). } } catch(Exception ex){} } int readInt(FileInputStream fin) throws IOException { int b1 = fin."Save BMP".read(). } public String getDescription() { return "BMP Image Files". int val) throws IOException { .setFileFilter(new javax.getName().createNewFile().FileFilter() { public boolean accept(File f) { if(f.bmp")) { ext = ".getName().APPROVE_OPTION).showSaveDialog(baseFrame). { File f = fc. } if(!f.bmp")) return true. fc. return (b2 << 8) + (b1 << 0). if(rv == fc.bmp". if(!f.exists()) f.filechooser. int b2 = fin.INFORMATION_MESSAGE).getSelectedFile().toLowerCase().

write((byte)(val>>8)).write((byte)val).fout. fout. } } Output: .

Experiment – 9 Aim: Study about Flash. .

Experiment – 10 Aim: Study about Dreamweaver. .

.Experiment – 11 Aim: Design a poster using Photoshop.