posted 16 years ago
package graphics2dframe;
import javax.swing.*;
import shapes.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import actionlisteners.*;
import actionlisteners.LineDrawListener.mouseHandler;
public class MainFrame extends JFrame {
public static Color selectedB,selectedF;
public static JColorChooser colorChooser;
private JDialog dialog;
public static JFileChooser chooser;
JMenuBar mainMenu;
JMenu file,edit,shapes,color,effects,look,help;
JMenuItem exit,select,cut,clear,text,fg,bg,motif,windows,metal;
JMenu line,polygon;
JMenuItem solid,dashl,dot,dashdot,dashdotdot;
public static JPanel canvas;
public static JFrame cFrame;
public Dimension screensize;
JToolBar topBar;
JToolBar mainBar;
public static LineDrawListener solidLineListener,dashDotLineListener,dashDotDotLineListener;
FreehandDrawListener curveListener;
RectDrawListener rectListener;
EllipseDrawListener ellipseListener;
CircleDrawListener circleListener;
PolygonDrawListener polygonListener;
TriangleDrawListener triangleListener;
HexagonDrawListener hexagonListener;
NewFileListener newFile;
OpenFileListener openFile;
SaveFileListener saveFile;
AboutListener about;
ZoominListener zoomIn;
ZoomoutListener zoomOut;
DashLineDrawListener dash;
DotLineDrawListener dotLineListener;
JButton solidb,dashb,dotb,dashdotb,dashdotdotb;
/*End of the fields declaration*/
/*---------------------------------------*/
public MainFrame()
{
cFrame = this;
cFrame.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getDefaultCursor());
}
});
initialiseComponents();//Initialize the Menu Items
addActionListeners();// Adding the action listeners to the menu items
topBar = new JToolBar();
topBar.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getDefaultCursor());
}
});
addToolBars();//adding tool bars to the frame
setLayout(new BorderLayout());
chooser = new JFileChooser();
add(topBar,BorderLayout.NORTH );
canvas = new JPanel();
canvas.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
});
canvas.setBackground(Color.white);
Toolkit kit = Toolkit.getDefaultToolkit();
screensize = kit.getScreenSize();
//Setting the size of the frame to that of the Display size
setSize(screensize.width , screensize.height );
add(canvas,BorderLayout.CENTER);
}
/*---------------------------------------*/
/*Adding all the Menus to the MainFrame*/
private void initialiseComponents()
{
mainMenu = new JMenuBar();
setJMenuBar(mainMenu);
file = new JMenu("File");
edit = new JMenu("Edit");
shapes = new JMenu("Shapes");
color= new JMenu("Color");
effects = new JMenu("Effect");
look = new JMenu("Look");
help = new JMenu("Help");
mainMenu.add(file);
mainMenu.add(edit);
mainMenu.add(shapes);
mainMenu.add(color);
mainMenu.add(effects);
mainMenu.add(look);
mainMenu.add(help);
/*Adding Menu Items to the Various menus by using AbstractAction Class*/
/*-----------------------------------*/
//Adding Menu Items to the File Menu
newFile = new NewFileListener("New",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\new16.gif"),"New File");
file.add(newFile);
openFile = new OpenFileListener("Open", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\open16.gif"),"Open File");
file.add(openFile);
saveFile = new SaveFileListener("Save",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\save16.gif"),"Save File");
file.add(saveFile);
exit = new JMenuItem("Exit");
file.addSeparator();
file.add(exit);
/*-----------------------------------*/
//Adding Menu Items to the Shapes Menu
line = new JMenu("Line");
shapes.add(line);
solid = new JMenuItem("Line",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_solid32.gif"));
solid.addActionListener(new LineDrawListener());
line.add(solid);
dashl = new JMenuItem("Dash Line", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dash32.gif"));
dashl.addActionListener(new DashLineDrawListener());
line.add(dashl);
dot= new JMenuItem("Dot Line", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dot32.gif"));
dot.addActionListener(new DotLineDrawListener());
line.add(dot);
/*-----------------------------------*/
/*Adding Menu Items to the color Menu*/
fg = new JMenuItem("Fore Ground");
bg = new JMenuItem("Back Ground");
color.add(fg);
color.add(bg);
/*-----------------------------------*/
//Adding Menu Items to the Look Menu
motif = new JMenuItem("Motif");
windows = new JMenuItem("Windows");
metal = new JMenuItem("Metal");
look.add(motif);
look.add(windows);
look.add(metal);
/*-----------------------------------*/
/*Adding Menu items to the Help Menu*/
about = new AboutListener("About", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\about16.gif"),"About the Package");
help.add(about);
about.setComponent(canvas);
}
/*---------------------------------------*/
/*Method to add action listeners to Look and Feel*/
public void addActionListeners()
{
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int selection = JOptionPane.showConfirmDialog(cFrame, "Do you want to really Quit?","Exit",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION)
{
System.exit(1);
}
else
{
}
}
});
motif.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception ex){ex.printStackTrace();}
}
}
);
windows.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception e){e.printStackTrace();}
}
});
metal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception e){e.printStackTrace();}
}
});
/*Adding action event to the Menu item Clear*/
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int selection=JOptionPane.showConfirmDialog(cFrame, "Do you really want to Clear the Area?", "Clear",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION)
{
Graphics g = canvas.getGraphics();
g.setColor(getBackground());
g.fillRect(0,0,getSize().width,getSize().height);
g.dispose();
repaint();
}
else
{
}
}
});
/*Adding action Event to the Menu item Fore Ground Color*/
fg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color defaultColor = canvas.getForeground();
selectedF = colorChooser.showDialog(canvas, "Fore Ground", defaultColor);
if(selectedF!=null)canvas.setForeground(selectedF);
}
});
/*Adding action Event to the Menu item Back Ground Color*/
bg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color defaultColor = canvas.getBackground();
selectedB = JColorChooser.showDialog(canvas, "Back Ground", defaultColor);
if(selectedB!=null)canvas.setBackground(selectedB);
}
});
}
//Method to add Tool bar to the main Frame
public void addToolBars()
{
solidb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_solid32.gif"));
dashb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dash32.gif"));
dotb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dot32.gif"));
solidb.addActionListener(new LineDrawListener());
dashb.addActionListener(new DashLineDrawListener());
dotb.addActionListener(new DotLineDrawListener());
topBar.add(newFile);
topBar.add(openFile);
topBar.add(saveFile);
topBar.addSeparator();
topBar.add(solidb);
topBar.add(dashb);
topBar.add(dotb);
/*topBar.add(solidLineListener);
topBar.add(dash);
topBar.add(dotLineListener);*/
topBar.addSeparator();
topBar.add(curveListener);
topBar.add(rectListener);
topBar.add(ellipseListener);
topBar.add(polygonListener);
topBar.add(triangleListener);
topBar.add(hexagonListener);
}
}