public class Image_Filter extends javax.swing.filechooser.FileFilter
{
/*
*Determines if the extension is of the defined types
*@param ext Extension of a file
*@return Returns true if the extension is 'jpg' or 'png'
*/
protected boolean isImageFile(String ext)
{
return (ext.equals("jpg")||ext.equals("png"));
}
/*
*Determines if the file is a directory or accepted extension
*@param f The File to run the directory/proper extension check on
*@return Returns true if the File is a directory or accepted extension
*/
public boolean accept(File f)
{
if (f.isDirectory())
{
return true;
}
String extension = getExtension(f);
if (extension.equals("jpg")||extension.equals("png"))
{
return true;
}
return false;
}
/*
*Supplies File type description
*@return Returns the String description
*/
public String getDescription()
{
return "Supported Image Files";
}
/*
*Determines the Extension
*@param f File to return the extension of
*@return Returns the String representing the extension
*/
protected static String getExtension(File f)
{
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1)
return s.substring(i+1).toLowerCase();
return "";
}
}
// Steganography_Controller.java
import java.io.File;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
/*
*Steganography_Controller Class
*/
public class Steganography_Controller
{
//Program Variables
private Steganography_View view;
private Steganography model;
//Panel Displays
private JPanel decode_panel;
private JPanel encode_panel;
//Panel Variables
private JTextArea input;
private JButton encodeButton,decodeButton;
private JLabel image_input;
//Menu Variables
private JMenuItem encode;
private JMenuItem decode;
private JMenuItem exit;
//action event classes
private Encode enc;
private Decode dec;
private EncodeButton encButton;
private DecodeButton decButton;
//decode variable
private String stat_path = "";
private String stat_name = "";
/*
*Constructor to initialize view, model and environment variables
*@param aView A GUI class, to be saved as view
*@param aModel A model class, to be saved as model
*/
public Steganography_Controller(Steganography_View aView, Steganography aModel)
{
//program variables
view = aView;
model = aModel;
//assign View Variables
//2 views
encode_panel = view.getTextPanel();
decode_panel = view.getImagePanel();
//2 data options
input = view.getText();
image_input = view.getImageInput();
//2 buttons
encodeButton = view.getEButton();
decodeButton = view.getDButton();
//menu
encode = view.getEncode();
decode = view.getDecode();
exit = view.getExit();
//assign action events
enc = new Encode();
encode.addActionListener(enc);
dec = new Decode();
decode.addActionListener(dec);
exit.addActionListener(new Exit());
encButton = new EncodeButton();
encodeButton.addActionListener(encButton);
decButton = new DecodeButton();
decodeButton.addActionListener(decButton);
//encode view as default
encode_view();
}
/*
*Updates the single panel to display the Encode View.
*/
private void encode_view()
{
update();
view.setContentPane(encode_panel);
view.setVisible(true);
}
/*
*Updates the single panel to display the Decode View.
*/
private void decode_view()
{
update();
view.setContentPane(decode_panel);
view.setVisible(true);
}
/*
*Encode Class - handles the Encode menu item
*/
private class Encode implements ActionListener
{
/*
*handles the click event
*@param e The ActionEvent Object
*/
public void actionPerformed(ActionEvent e)
{
encode_view(); //show the encode view
}
}
/*
*Decode Class - handles the Decode menu item
*/
private class Decode implements ActionListener
{
/*
*handles the click event
*@param e The ActionEvent Object
*/
public void actionPerformed(ActionEvent e)
{
decode_view(); //show the decode view
//start path of displayed File Chooser
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new Image_Filter());
int returnVal = chooser.showOpenDialog(view);
if (returnVal == JFileChooser.APPROVE_OPTION){
File directory = chooser.getSelectedFile();
try{
String image = directory.getPath();
stat_name = directory.getName();
stat_path = directory.getPath();
stat_path = stat_path.substring(0,stat_path.length()-stat_name.length()-1);
stat_name = stat_name.substring(0, stat_name.length()-4);
image_input.setIcon(new ImageIcon(ImageIO.read(new File(image))));
}
catch(Exception except) {
//msg if opening fails
JOptionPane.showMessageDialog(view, "The File cannot be opened!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
/*
*Exit Class - handles the Exit menu item
*/
private class Exit implements ActionListener
{
/*
*handles the click event
*@param e The ActionEvent Object
*/
public void actionPerformed(ActionEvent e)
{
System.exit(0); //exit the program
}
}
/*
*Encode Button Class - handles the Encode Button item
*/
private class EncodeButton implements ActionListener
{
/*
*handles the click event
*@param e The ActionEvent Object
*/
public void actionPerformed(ActionEvent e)
{
//start path of displayed File Chooser
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new Image_Filter());
int returnVal = chooser.showOpenDialog(view);
if (returnVal == JFileChooser.APPROVE_OPTION){
File directory = chooser.getSelectedFile();
try{
String text = input.getText();
String ext = Image_Filter.getExtension(directory);
String name = directory.getName();
String path = directory.getPath();
path = path.substring(0,path.length()-name.length()-1);
name = name.substring(0, name.length()-4);
String stegan = JOptionPane.showInputDialog(view,
"Enter output file name:", "File name",
JOptionPane.PLAIN_MESSAGE);
if(model.encode(path,name,ext,stegan,text))
{
JOptionPane.showMessageDialog(view, "The Image was encoded Successfully!",
"Success!", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(view, "The Image could not be encoded!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
//display the new image
decode_view();
image_input.setIcon(new ImageIcon(ImageIO.read(new File(path + "/" + stegan + ".png"))));
}
catch(Exception except) {
//msg if opening fails
JOptionPane.showMessageDialog(view, "The File cannot be opened!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
/*
*Decode Button Class - handles the Decode Button item
*/
private class DecodeButton implements ActionListener
{
/*
*handles the click event
*@param e The ActionEvent Object
*/
public void actionPerformed(ActionEvent e)
{
String message = model.decode(stat_path, stat_name);
System.out.println(stat_path + ", " + stat_name);
if(message != "")
{
encode_view();
JOptionPane.showMessageDialog(view, "The Image was decoded Successfully!",
"Success!", JOptionPane.INFORMATION_MESSAGE);
input.setText(message);
}
else
{
JOptionPane.showMessageDialog(view, "The Image could not be decoded!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
/*
*Updates the variables to an initial state
*/
public void update()
{
input.setText(""); //clear textarea
image_input.setIcon(null); //clear image
stat_path = ""; //clear path
stat_name = ""; //clear name
}
/*
*Main Method for testing
*/
public static void main(String args[])
{
new Steganography_Controller(
new Steganography_View("Steganography"),
new Steganography()
);
}
}
//Steganography_View.java
import java.awt.Color;
import java.awt.Insets;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
/*
*Class Steganography_View
*/
public class Steganography_View extends JFrame
{
//sie variables for window
private static int WIDTH = 500;
private static int HEIGHT = 400;
//elements for JPanel
private JTextArea input;
private JScrollBar scroll,scroll2;
private JButton encodeButton,decodeButton;
private JLabel image_input;
//elements for Menu
private JMenu file;
private JMenuItem encode;
private JMenuItem decode;
private JMenuItem exit;
/*
*Constructor for Steganography_View class
*@param name Used to set the title on the JFrame
*/
public Steganography_View(String name)
{
//set the title of the JFrame
super(name);
//Menubar
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File"); file.setMnemonic('F');
encode = new JMenuItem("Encode"); encode.setMnemonic('E'); file.add(encode);
decode = new JMenuItem("Decode"); decode.setMnemonic('D'); file.add(decode);
file.addSeparator();
exit = new JMenuItem("Exit"); exit.setMnemonic('x'); file.add(exit);
menu.add(file);
setJMenuBar(menu);
// display rules
setResizable(true); //allow window to be resized: true?false
setBackground(Color.lightGray); //background color of window: Color(int,int,int) or Color.name
setLocation(100,100); //location on the screen to display window
setDefaultCloseOperation(EXIT_ON_CLOSE);//what to do on close operation: exit, do_nothing, etc
setSize(WIDTH,HEIGHT); //set the size of the window
setVisible(true); //show the window: true?false
}
/*
*@return The menu item 'Encode'
*/
public JMenuItem getEncode() { return encode; }
/*
*@return The menu item 'Decode'
*/
public JMenuItem getDecode() { return decode; }
/*
*@return The menu item 'Exit'
*/
public JMenuItem getExit() { return exit; }
/*
*@return The TextArea containing the text to encode
*/
public JTextArea getText() { return input; }
/*
*@return The JLabel containing the image to decode text from
*/
public JLabel getImageInput() { return image_input; }
/*
*@return The JPanel displaying the Encode View
*/
public JPanel getTextPanel() { return new Text_Panel(); }
/*
*@return The JPanel displaying the Decode View
*/
public JPanel getImagePanel() { return new Image_Panel(); }
/*
*@return The Encode button
*/
public JButton getEButton() { return encodeButton; }
/*
*@return The Decode button
*/
public JButton getDButton() { return decodeButton; }
/*
*Class Text_Panel
*/
private class Text_Panel extends JPanel
{
/*
*Constructor to enter text to be encoded
*/
public Text_Panel()
{
//setup GridBagLayout
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
setLayout(layout);
input = new JTextArea();
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,0,0,0);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 50.0;
JScrollPane scroll = new JScrollPane(input,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
layout.setConstraints(scroll,layoutConstraints);
scroll.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
add(scroll);
encodeButton = new JButton("Encode Now");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 1;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,-5,-5,-5);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(encodeButton,layoutConstraints);
add(encodeButton);
//set basic display
setBackground(Color.lightGray);
setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
}
}
/*
*Class Image_Panel
*/
private class Image_Panel extends JPanel
{
/*
*Constructor for displaying an image to be decoded
*/
public Image_Panel()
{
//setup GridBagLayout
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
setLayout(layout);
image_input = new JLabel();
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,0,0,0);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 50.0;
JScrollPane scroll2 = new JScrollPane(image_input,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
layout.setConstraints(scroll2,layoutConstraints);
scroll2.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
image_input.setHorizontalAlignment(JLabel.CENTER);
add(scroll2);
decodeButton = new JButton("Decode Now");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 1;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(0,-5,-5,-5);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(decodeButton,layoutConstraints);
add(decodeButton);
//set basic display
setBackground(Color.lightGray);
setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
}
}
/*
*Main Method for testing
*/
public static void main(String args[])
{
new Steganography_View("Steganography");
}
}