Skip to content

Instantly share code, notes, and snippets.

@samuelkobe
Created October 13, 2012 21:23
Show Gist options
  • Save samuelkobe/3886186 to your computer and use it in GitHub Desktop.
Save samuelkobe/3886186 to your computer and use it in GitHub Desktop.
dont be a wienest
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.swing.BoxLayout;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Menu {
//Variables - Static makes them referrable through any of the classes from below
static JFrame frame = new JFrame("Energy Menu");
static JFrame eFrame;
static JMenuItem viewItem1;
static JMenuItem viewItem2;
static JMenu viewMenu;
static JPanel eContent;
static JPanel lContent;
static JPanel overContent;
static JPanel cContent;
static JPanel mContent;
static JPanel bContent;
static JPanel bfContent;
static JPanel allButtonContent;
static JButton HomeButton = new JButton("Home");
static JButton WorkButton = new JButton("Work");
static JButton CommutingButton = new JButton("Commute");
static JButton TipsButton = new JButton("Tips");
static BasicArrowButton ForwardButton = new BasicArrowButton(1);
static BasicArrowButton BackButton = new BasicArrowButton(1);
static JLabel bottomLabel;
static boolean eViewState = false;
static boolean eHideState = true;
static String eViewName = "Show/Hide Energy Use";
static String viewFlip = "Annotate Calendar";
static String dMessage = "Console";
JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL);
/********* Main Method ********/
public static void main(String[] args) {
// Instantiate a new object that creates the entire JFrame and JPanels of the project through it's constructor
Menu M = new Menu();
}//Main
/********* Menu Constructor ********/
public Menu() {
/**~~~~~~~~ PANELS ~~~~~~~~**/
//JPanels Instantiations
eContent = new JPanel(); // Panel used for adding a NEW energy panel when "show energy" is clicked in view
bContent = new JPanel(); // Panel used for adding button items
lContent = new JPanel(); // Panel used for the status bar at the bottom
overContent = new JPanel(); // Main Overall Panel that integrates all other panels
mContent = new JPanel(); // Panel used for the main content
cContent = new JPanel(); // Panel used for holding Calendar and ImagePanel
bfContent = new JPanel(); // Panel used for holding back and forward buttons
allButtonContent = new JPanel(new GridLayout(2, 1)); // Panel used for adding all the button panels within this panel
ImagePanel iContent = new ImagePanel();
ImagePanel2 sContent = new ImagePanel2();
JTextArea textfield = new JTextArea(5,30);
JScrollPane scrollPane = new JScrollPane(textfield);
ForwardButton = (new BasicArrowButton(BasicArrowButton.WEST));
BackButton = (new BasicArrowButton(BasicArrowButton.EAST));
// Panel used for importing and displaying image of Calendar
//Image Panel
iContent.setPreferredSize(new Dimension(iContent.image.getWidth(), iContent.image.getHeight())); //Set the Dimension for the Image Panel
//sContent.setPreferredSize(new Dimension(sContent.image2.getWidth(), sContent.image2.getHeight())); //Set the Dimension for the Image Panel2
sContent.setPreferredSize(new Dimension(sContent.image3.getWidth(), sContent.image3.getHeight())); //Set the Dimension for the Image Panel2
//Label Panel
bottomLabel = new JLabel(dMessage);
bottomLabel.setHorizontalAlignment(JLabel.CENTER);
bottomLabel.setVerticalAlignment(JLabel.BOTTOM);
lContent.setBorder(BorderFactory.createEtchedBorder());
lContent.add(bottomLabel); //Add the JLabel to the lContent JPanel
//Calendar Panel
cContent.setPreferredSize(new Dimension(210, 100));
cContent.setBorder(BorderFactory.createEtchedBorder()); //Create an etched border
cContent.add(iContent, BorderLayout.WEST);
//BackForward Panel
addPanelItems(bfContent, (new BasicArrowButton[]{ForwardButton, BackButton}));
//Main Panel
textfield.setPreferredSize(new Dimension(300,100));
mContent.setPreferredSize(new Dimension(500, 500));
mContent.setBorder(BorderFactory.createEtchedBorder());
mContent.add(scrollPane);
mContent.add(sContent, BorderLayout.SOUTH);
mContent.addMouseListener(sContent);
/**~~~~~~~~ ITEMS ~~~~~~~~**/
//Menu Bar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
viewMenu = new JMenu("View");
//File & View Instantiations
JMenuItem fileItem1 = new JMenuItem("Import Image/Graph");
JMenuItem fileItem2 = new JMenuItem("Import Journal File");
JMenuItem fileItem3 = new JMenuItem("Delete Image/Graph");
JMenuItem fileItem4 = new JMenuItem("New Entry");
JMenuItem fileItem5 = new JMenuItem("Delete Entry");
JMenuItem fileItem6 = new JMenuItem("Save Journal");
JMenuItem fileItem7 = new JMenuItem("Exit");
viewItem1 = new JMenuItem(eViewName);
viewItem2 = new JMenuItem(viewFlip);
//Button Items
bContent.setLayout(new BoxLayout(bContent, BoxLayout.Y_AXIS));
bContent.setPreferredSize(new Dimension(100, 100));
bContent.setBorder(BorderFactory.createEtchedBorder());
bfContent.setLayout(new BoxLayout(bfContent, BoxLayout.X_AXIS));
bfContent.setPreferredSize(new Dimension(100, 100));
bfContent.setBorder(BorderFactory.createEtchedBorder());
addPanelItems(bContent, (new JButton[]{HomeButton, WorkButton, CommutingButton, TipsButton}));
//All Buttons Panel
allButtonContent.setPreferredSize(new Dimension(100, 180));
allButtonContent.setBorder(BorderFactory.createEtchedBorder());
//File & View Menu Items
addMenuItems(fileMenu, (new JMenuItem[]{fileItem1, fileItem2, fileItem3, fileItem4, fileItem5, fileItem6, fileItem7}));
viewMenu.add(viewItem1);
viewMenu.add(viewItem2);
//MenuBar & MainPane Items
menuBar.add(fileMenu);
menuBar.add(viewMenu);
allButtonContent.add(bContent);
allButtonContent.add(bfContent);
overContent.setLayout(new BorderLayout(3,3));
overContent.add(allButtonContent, BorderLayout.WEST);
overContent.add(cContent, BorderLayout.CENTER);
overContent.add(lContent, BorderLayout.SOUTH);
overContent.add(mContent, BorderLayout.EAST);
//Button Characteristics using methods
buttonProperties(HomeButton);
buttonProperties(WorkButton);
buttonProperties(CommutingButton);
buttonProperties(TipsButton);
buttonBFProperties(ForwardButton);
buttonBFProperties(BackButton);
/**~~~~~~~~ FRAMES ~~~~~~~~**/
//Main Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(0, 0);
frame.setJMenuBar(menuBar);
frame.add(overContent, BorderLayout.WEST);
frame.setSize(800,400);
frame.setResizable(false);
frame.setVisible(true);
//Energy Frame
eFrame = new JFrame ("Energy Frame");
eFrame.setContentPane(eContent);
eFrame.setSize(500, 400);
eFrame.setLocation(800, 0);
/**~~~~~~~~ LISTENERS ~~~~~~~~**/
// Listener Instantiations
ButtonFeedback buttonListener = new ButtonFeedback();
ArrowButtonFeedback buttonListener2 = new ArrowButtonFeedback();
ExitHandler exitListener = new ExitHandler();
CheckEnergy checkListener = new CheckEnergy();
ShowEnergy showListener = new ShowEnergy();
//MouseListener mouseListener = new MouseListener();
//flipside flipside = new flipside();
//ShowFlipside showflip = new ShowFlipside();
// Adding Listeners to Button & Menu Items Items within the Panel
HomeButton.addActionListener(buttonListener);
CommutingButton.addActionListener(buttonListener);
WorkButton.addActionListener(buttonListener);
TipsButton.addActionListener(buttonListener);
BackButton.addActionListener(buttonListener2);
ForwardButton.addActionListener(buttonListener2);
fileItem7.addActionListener(exitListener);
viewMenu.addMenuListener((MenuListener) checkListener);
viewItem1.addActionListener(showListener);
//viewItem2.addActionListener(showflip);
//constructor
}
//********* Button Properties Method ********//
//This method adds characteristics to the buttons, such as size, margin and font
public void buttonProperties(JButton buttonName){
buttonName.setMaximumSize(new Dimension(50, 50));
buttonName.setMargin(new Insets (1, 1, 1, 1));
buttonName.setFont(new Font("Dialog", 1, 8));
}
//********* Button Back Forward Properties Method ********//
//This method adds characteristics to the back and forward buttons, such as size, margin and font
public void buttonBFProperties(JButton buttonName){
buttonName.setMaximumSize(new Dimension(45, 45));
buttonName.setMargin(new Insets (1, 1, 1, 1));
buttonName.setFont(new Font("Dialog", 1, 8));
}
//********* Add Menu Items Method ********//
//This method adds items to menus in a faster manner
public void addMenuItems(JMenu addent, JMenuItem[]items){
for (int i = 0; i < items.length; i++){
addent.add(items[i]);
}
}
//******** Add Panel Button Method *******//
//This method adds buttons to panels in a faster manner
public void addPanelItems(JPanel addent, JButton[]items){
for (int i = 0; i < items.length; i++){
addent.add(items[i]);
}
}
/********* Exit Frame ActionListener Class ********/
// This class calls an ActionListener which listens for when users close the window upon clicking red X
private static class ExitHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}//ExitHandler
/********* CheckEnergy Class ********/
// This class calls a MenuListener which listeners for when items on menu have been selected, deselected or cancelled
public static class CheckEnergy implements MenuListener {
public void menuCanceled(MenuEvent e) {
}
public void menuDeselected(MenuEvent e) {
}
public void menuSelected(MenuEvent e) {
//If the Energy Frame is not visible change a boolean to true
if (eFrame.isVisible() == false) {
eViewState = true;
}
if (eFrame.isVisible() == true) {
eViewState = false;
}
// Change HideState to false and ViewState to false, so that second "if" gets called the next time Menu is Selected
if (eViewState == true && eHideState == true) {
eViewState = false;
eHideState = false;
eViewName = "Show Energy Use"; // Change a static variable for item in View to this String
}
// Change HideState to false and ViewState to true, so that first "if" gets called the next time Menu is Selected
if (eViewState == false & eHideState == true) {
eViewState = true;
eHideState = false;
eViewName = "Hide Energy Use";
}
// An additional Boolean to reset HideState back to true to make sure only one condition above gets called.
if (eHideState == false){
eHideState = true;
}
//Add the String Name into the View Menu Tab
viewItem1 = new JMenuItem(eViewName);
// COME BACK AND REMOVE ONLY viewItem1, not removeAll
// viewMenu.remove(viewItem1);
// Remove all the Items in the menu, and then re-add an item with a new String Name
//viewMenu.removeAll();
viewMenu.remove(viewItem1);
//viewMenu.add(viewItem1);
//Add the ShowListener to the View Menu
ShowEnergy showListener = new ShowEnergy();
viewItem1.addActionListener(showListener);
}
}//CheckEnergy
/********* ShowEnergy Class ********/
public static class ShowEnergy implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (eViewState == true && eHideState == true) {
eViewState = false;
eHideState = false;
eViewName = "Show Energy Use";
eFrame.setVisible(false);
}
if (eViewState == false & eHideState == true) {
eViewState = true;
eHideState = false;
eViewName = "Hide Energy Use";
eFrame.setVisible(true);
}
if (eHideState == false){
eHideState = true;
}
}
}//ShowEnergy
/********* Flipside Class ********/
// This class calls a MenuListener which listeners for when items on menu have been selected, deselected or cancelled
/*public static class flipside implements MenuListener {
public void menuCanceled(MenuEvent e) {
}
public void menuDeselected(MenuEvent e) {
}
public void menuSelected(MenuEvent e) {
//If the Energy Frame is not visible change a boolean to true
// change image goes here *****
if (eFrame.isVisible() == false) {
eViewState = true;
}
if (eFrame.isVisible() == true) {
eViewState = false;
}
// Change HideState to false and ViewState to false, so that second "if" gets called the next time Menu is Selected
if (eViewState == true && eHideState == true) {
eViewState = false;
eHideState = false;
viewFlip = "Annotate Calendar"; // Change a static variable for item in View to this String
}
// Change HideState to false and ViewState to true, so that first "if" gets called the next time Menu is Selected
if (eViewState == false & eHideState == true) {
eViewState = true;
eHideState = false;
viewFlip = "Display Calendar";
}
// An additional Boolean to reset HideState back to true to make sure only one condition above gets called.
if (eHideState == false){
eHideState = true;
}
//Add the String Name into the View Menu Tab
viewItem2 = new JMenuItem(viewFlip);
// COME BACK AND REMOVE ONLY viewItem1, not removeAll
// viewMenu.remove(viewItem1);
// Remove all the Items in the menu, and then re-add an item with a new String Name
viewMenu.removeAll();
viewMenu.add(viewItem2);
//Add the ShowListener to the View Menu
ShowFlipside showFlip = new ShowFlipside();
viewItem2.addActionListener(showFlip);
}
}//flipside
/********* display canvas ********/
/*public static class ShowFlipside implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (eViewState == true && eHideState == true) {
eViewState = false;
eHideState = false;
viewFlip = "Annotate Calendar";
//eFrame.setVisible(false);
}
if (eViewState == false & eHideState == true) {
eViewState = true;
eHideState = false;
viewFlip = "Display Calendar";
//eFrame.setVisible(true);
}
if (eHideState == false){
eHideState = true;
}
}
}
//display canvas
/********* ButtonFeedback Class ********/
public static class ButtonFeedback implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Change dmessage to whichever button was created from Source when the specified button was clicked.
JButton source = (JButton)e.getSource();
if (source.equals(HomeButton)){
dMessage = "Home Button is clicked.";
}
else if (source.equals(WorkButton)) {
dMessage = "Work Button is clicked.";
}
else if (source.equals(CommutingButton)) {
dMessage = "Commuting Button is clicked.";
}
else if (source.equals(TipsButton)) {
dMessage = "Tips Button is clicked.";
}
bottomLabel.setText(dMessage);
}
}//ButtonFeedback
/********* ArrowButtonFeedback Class ********/
public static class ArrowButtonFeedback implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Change dmessage to whichever button was created from Source when the specified button was clicked.
BasicArrowButton source2 = (BasicArrowButton)e.getSource();
if (source2.equals(BackButton)){
dMessage = "Forward Button is clicked.";
}
else if (source2.equals(ForwardButton)){
dMessage = "Back Button is clicked.";
}
bottomLabel.setText(dMessage);
}
}//ArrowButtonFeedback
/********* ImagePanel Class ********/
public class ImagePanel extends JPanel{
public BufferedImage image;
//Import Image file within the constructor
public ImagePanel() {
try {
image = ImageIO.read(new File("months.jpg"));
} catch (IOException ex) {
System.out.println("Error, your image did not load.");
}
}
//Paint the image within a JPanel
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); // see javadoc for more info on the parameters
}
}//ImagePanel switch class
public class ImagePanel2 extends JPanel implements MouseListener{
public BufferedImage image2;
public BufferedImage image3;
//Import Image file within the constructor
public ImagePanel2() {
try {
image2 = ImageIO.read(new File("calendar.png"));
image3 = ImageIO.read(new File("flipside.jpg"));
} catch (IOException ex) {
System.out.println("Error, your image did not load.");
}
}
//Paint the image within a JPanel
public void paintComponent(Graphics d) {
super.paintComponent(d);
d.drawImage(image2, 0, 0, image2.getWidth(), image2.getHeight(), null); // see javadoc for more info on the parameters
//setVisible(true);
d.drawImage(image3, 0, 0, image3.getWidth(), image3.getHeight(), null);
}
public void mouseClicked(MouseEvent e)
{
this.addMouseListener(this);
int x = e.getX();
int y = e.getY();
Graphics d = image3.getGraphics();
d.fillOval(x, y, 3, 3);
//d.dispose();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}//ImagePanel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment