Experiment - 1 Aim: Write A Program Java To Draw Some Shape and Save It To A Raw File. Source Code
Experiment - 1 Aim: Write A Program Java To Draw Some Shape and Save It To A Raw File. Source Code
Experiment - 1 Aim: Write A Program Java To Draw Some Shape and Save It To A Raw File. Source Code
Aim: Write a program java to draw some shape and save it to a raw file.
Source Code:
import MMT_Lab_Tool_Lib.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
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);
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){}
}
Aim: Write a program java to draw shapes from saved file in previous experiment.
Source Code:
import MMT_Lab_Tool_Lib.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
public LoadImageDemo()
{
JButton b = new JButton("Load Image");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
loadImage();
}
});
imgDrg.addButton(b);
imgDrg.setVisible(true);
}
void loadImage()
{
try
{
new Thread()
{
public void run()
{
JFileChooser fc = new JFileChooser();
int rv = fc.showOpenDialog(imgDrg);
if(rv == fc.APPROVE_OPTION);
{
BufferedImage img = (BufferedImage)imgDrg.getImage();
WritableRaster r = img.getRaster();
int i=1;
try
{
FileInputStream fin = new
FileInputStream(fc.getSelectedFile());
}
catch(Exception ex){}
}
imgDrg.repaint();
imgDrg.setProgress(0);
}
}.start();
}
catch(Exception ex){}
}
import java.io.*;
import javax.swing.*;
return buffer.toString();
}
return ((b4 << 24) + (b3 << 16) + (b2 << 8) + (b1 << 0));
}
Output:
File Name : bee.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
Experiment – 4
Aim: Write a program in java to read a bmp file and display the image.
Source:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
fc.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
if(f.isDirectory() || f.getName().toLowerCase().endsWith(".bmp"))
return true;
return false;
}
if(fc.showOpenDialog(null) == fc.APPROVE_OPTION);
{
try
{
ImageIcon img = new ImageIcon(ImageIO.read(fc.getSelectedFile()));
JOptionPane.showOptionDialog(null,new
JLabel(img),fc.getSelectedFile().getName(),JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,
null,null,null);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Invalid file");
}
}
}
}
Output:
Experiment – 5
Aim: Write a program in java to read an mp3 (or any audio) file and display its header
information.
Source Code:
import java.io.*;
private final String[] channelLabels = { "Stereo", "Joint Stereo (STEREO)","Dual Channel (STEREO)","Single
Channel (MONO)" };
private final String[] versionLabels = {"MPEG Version 2.5",null,"MPEG Version 2.0","MPEG Version 1.0"};
private final String[] layerLabels = { null, "3", "2","1" };
if( raf.read( head ) != HEADER_SIZE ) throw new Exception("Error reading MP3 frame header.");
if( (ind != -1) && (bitrateIndex >= 0) && (bitrateIndex <= 15) )
bitRate = bitrateTable[bitrateIndex][ind];
}
switch( version )
{
case MPEG_V_1:
ind = 0;
break;
case MPEG_V_2:
ind = 1;
break;
case MPEG_V_25:
ind = 2;
}
if(copyrighted)
buffer.append("\nCopywrited : Yes");
else
buffer.append("\nCopywrited : No");
return buffer.toString();
}
while( loc == -1 )
{
test = raf.readByte();
if(matchPattern( test, "11111111" ) )
{
test = raf.readByte();
for( int i = 0; (i < NUM_BITS) && (i < pattern.length()) && retval; i++ )
{
if(pattern.charAt(i) == '1' )
{
retval = retval && bitSet( b, NUM_BITS - i - 1 );
}
else if( pattern.charAt(i) == '0' )
{
retval = retval && !bitSet( b, NUM_BITS - i - 1 );
}
}
return retval;
}
return retval;
}
bCount++;
}
return ret;
}
return ret;
}
return false;
}
public String getDescription()
{
return "MPEG Files";
}
});
int rv = fc.showOpenDialog(null);
if(rv == fc.APPROVE_OPTION);
{
try
{
MP3Header info = new MP3Header(fc.getSelectedFile());
javax.swing.JOptionPane.showOptionDialog(null,info.toString(),"MP3 Header
Information",javax.swing.JOptionPane.DEFAULT_OPTION,javax.swing.JOptionPane.INFORMATION_MESSAGE,null,n
ull,null);
}
catch(Exception ex)
{
javax.swing.JOptionPane.showMessageDialog(null,"Invalid file");
}
}
}
}
Output:
Output:
C :\> java –c abc.java abc.gz
File Compressed….
Experiment -7
Aim: Write a program in java to decompress the given file.
Source:
import java.io.*;
import java.util.Observable;
import java.util.zip.*;
Output:
C :\> java –d abc.gz abc.java
File Decompressed….
Experiment – 8
Aim: Create a GUI Tool to perform all the operations performed in previous experiments
under a single interface form.
Source Code:
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.io.*;
import java.util.*;
public MasterFrame()
{
super("MultiMedia Tool");
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,400);
setUp();
setVisible(true);
addOptionFrame();
}
void setUp()
{
jdp = new JDesktopPane();
add(jdp,BorderLayout.CENTER);
}
void addOptionFrame()
{
JToolBar jtb = new JToolBar();
add(jtb,BorderLayout.NORTH);
for(String s : options)
{
JButton b = new JButton(s);
b.addActionListener(this);
b.setActionCommand(s);
jtb.add(b);
}
}
if(cmd.equals("Drawing"))
{
jdp.add(new DrgTool());
}
else if(cmd.equals("BMP File Header"))
{
jdp.add(new BMPInfoFrame());
}
else if(cmd.equals("File Codec"))
{
jdp.add(new CodecFrame());
}
}
public BMPInfoFrame()
{
super("BMP Header",true,true,true,true);
setSize(250,300);
setLayout(new BorderLayout());
return false;
}
if(fc.showOpenDialog(null) == fc.APPROVE_OPTION);
{
try
{
infoBox.setText(new
BMPInfo(fc.getSelectedFile()).toString());
}
catch(Exception ex)
{
infoBox.setText("Invalid file...");
}
}
}
});
add(ld,BorderLayout.NORTH);
add(new JScrollPane(infoBox),BorderLayout.CENTER);
setVisible(true);
}
}
public CodecFrame()
{
super("BMP Header",true,true,true,true);
setSize(600,400);
setLayout(new BorderLayout());
add(new JScrollPane(srcBox),BorderLayout.CENTER);
for(String s : cmd)
{
JButton ld = new JButton(s);
tb.add(ld);
ld.setActionCommand(s);
ld.addActionListener(this);
}
tb = new JToolBar();
add(tb,BorderLayout.SOUTH);
jpb.setMaximum(100);
jpb.setStringPainted(true);
jpb.setVisible(false);
tb.add(infoLabel);
tb.add(jpb);
setVisible(true);
c.addObserver(this);
}
jpb.setValue(val);
}
if(cmd.equals("Compress"))
{
try
{
lock = true;
new Thread()
{
infoLabel.setText("Compressing File...");
c.compress(srcFile.getPath(),f.getName());
infoLabel.setText("");
}
catch(Exception ex){ex.printStackTrace();}
lock = false;
}
}.start();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
else if(cmd.equals("Decompress"))
{
try
{
lock = true;
new Thread()
{
infoLabel.setText("Decompressing File...");
c.decompress(srcFile.getPath(),f.getName());
infoLabel.setText("");
}
catch(Exception ex){ex.printStackTrace();}
lock = false;
}
}.start();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
else if(cmd.equals("Save"))
{
try
{
lock = true;
infoLabel.setText("Saving File...");
try
{
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Choose File");
int rv = fc.showSaveDialog(null);
if(rv == fc.APPROVE_OPTION);
{
srcFile = fc.getSelectedFile();
srcBox.setText("");
new Thread()
{
public void run()
{
try
{
FileInputStream fin = new
FileInputStream("codec.tmp");
FileOutputStream fout = new
FileOutputStream(srcFile);
int c = 0;
long count = 1, size = new
File("codec.tmp").length();
srcBox.append(String.valueOf((char)c));
setProgress((int)
((double)count/size * 100));
++count;
}
fin.close();
fout.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
lock = false;
infoLabel.setText("");
}
}.start();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
void loadSrcFile()
{
try
{
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Choose Source File");
int rv = fc.showOpenDialog(null);
if(rv == fc.APPROVE_OPTION);
{
srcFile = fc.getSelectedFile();
srcBox.setText("");
int c = 0;
long count = 1, size = srcFile.length();
setProgress((int)((double)count/size * 100));
++count;
}
f.close();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
public DrgTool()
{
super("Drawing",true,true,true,true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(400,400);
setUp();
setVisible(true);
}
void setUp()
{
jpb = new JProgressBar();
jpb.setStringPainted(true);
jpb.setVisible(false);
add(jpb,BorderLayout.SOUTH);
jb = new JMenuBar();
JMenu m = null;
JMenuItem mit = null;
m = new JMenu("Home");
jb.add(m);
m.addSeparator();
m.addSeparator();
m.addSeparator();
mit = new JMenuItem("Exit");
mit.addActionListener(this);
mit.setActionCommand("EXIT");
m.add(mit);
m = new JMenu("Drawing");
jb.add(m);
sp = new JSpinner();
sp.setValue(2);
sp.addChangeListener(this);
mit = new JMenuItem(" ");
mit.setLayout(new GridLayout(1,2));
mit.add(new JLabel(" Brush Size"));
mit.add(sp);
m.add(mit);
setJMenuBar(jb);
if(v == 0)
jpb.setVisible(false);
else if(v == 1)
jpb.setVisible(true);
}
if(cmd.equals("BRUSH_COLOR"))
{
Color c = JColorChooser.showDialog(this,"",null);
if(c != null)
canvas.setBrushColor(c);
}
else if(cmd.equals("New"))
{
canvas.newImage();
}
else if(cmd.equals("OPEN_RAW"))
{
canvas.loadRawImage();
}
else if(cmd.equals("SAVE_RAW"))
{
canvas.saveRawImage();
}
else if(cmd.equals("OPEN_BMP"))
{
canvas.loadBMPImage();
}
else if(cmd.equals("SAVE_BMP"))
{
canvas.saveBMPImage();
}
else if(cmd.equals("EXIT"))
{
System.exit(0);
}
}
canvas.setBrushSize(i);
}
int brushSize = 1;
Color brushColor = Color.black;
int imgXpos = 0;
int imgYpos = 0;
baseFrame = dt;
newImage();
addMouseListener(this);
addMouseMotionListener(this);
setBrushColor(Color.black);
setBrushSize(2);
}
if(image != null)
{
imgXpos = (getWidth() - image.getWidth())/2;
imgYpos = (getHeight() - image.getHeight())/2;
g.drawImage(image,imgXpos,imgYpos,null);
}
}
g.setColor(Color.white);
g.fillRect(0,0,799,599);
g.setColor(Color.black);
g.drawRect(0,0,799,599);
baseFrame.setMaxVal(w*h);
repaint();
}
g.drawRect(0,0,w-1,h-1);
baseFrame.setMaxVal(w*h);
repaint();
}
if(rv == fc.APPROVE_OPTION);
{
int[] pixValue = new int[4];
int i=1;
try
{
FileInputStream fin = new
FileInputStream(fc.getSelectedFile());
int w = readInt(fin);
int h = readInt(fin);
newImage(w,h);
raster.setPixel(x,y,pixValue);
baseFrame.setProgress(i++);
}
}
fin.close();
}
catch(Exception ex){}
}
baseFrame.repaint();
baseFrame.setProgress(0);
}
}.start();
}
catch(Exception ex){}
}
if(rv == fc.APPROVE_OPTION);
{
int[] pixValue = new int[4];
int w = image.getWidth();
int h = image.getHeight();
int i=1;
try
{
FileOutputStream fout = new
FileOutputStream(fc.getSelectedFile());
writeInt(fout,w);
writeInt(fout,h);
fout.write(pixValue[0]);
fout.write(pixValue[1]);
fout.write(pixValue[2]);
fout.write(pixValue[3]);
}
}
}
catch(Exception ex){}
}
baseFrame.setProgress(0);
JOptionPane.showMessageDialog(baseFrame,"File Saved","Raw
Image",JOptionPane.INFORMATION_MESSAGE);
}
}.start();
}
catch(Exception ex){}
}
int rv = fc.showOpenDialog(baseFrame);
if(rv == fc.APPROVE_OPTION);
{
setImage(ImageIO.read(fc.getSelectedFile()));
baseFrame.repaint();
}
}
catch(Exception ex){}
}
int rv = fc.showSaveDialog(baseFrame);
if(rv == fc.APPROVE_OPTION);
{
File f = fc.getSelectedFile();
if(!f.getName().toLowerCase().endsWith(".bmp"))
{
ext = ".bmp";
}
if(!f.exists())
f.createNewFile();
ImageIO.write(image,"bmp",f);
JOptionPane.showMessageDialog(baseFrame,"File Saved","Save
BMP",JOptionPane.INFORMATION_MESSAGE);
}
}
catch(Exception ex){}
}
int readInt(FileInputStream fin) throws IOException
{
int b1 = fin.read();
int b2 = fin.read();
return (b2 << 8) + (b1 << 0);
}
Output:
Experiment – 9