Thursday, December 5, 2013

Clickable image gallery using JAVA

This is a sample project that gives the user a simple image gallery of 44 images those are clickable and prints the clicked image names to the prompt .This can be used as menu or any other way.Hope you would like it.

Code:
MainFrame.java

import java.awt.*;
public class JIFrameDemo{
 static createFrame f1=new createFrame();
 static createFrame f2=new createFrame();
 
 static createFrame f3=new createFrame();
 static createFrame f4=new createFrame();
 static createFrame f5=new createFrame();
 static createFrame f6=new createFrame();
 static createFrame f7=new createFrame();
 static createFrame f8=new createFrame();
 static createFrame f9=new createFrame();

 static createFrame f10=new createFrame();
 static createFrame f11=new createFrame();
 static createFrame f12=new createFrame();
 static  createFrame f13=new createFrame();
 static createFrame f14=new createFrame();
 static createFrame f15=new createFrame();
 static createFrame f16=new createFrame();
 static createFrame f17=new createFrame();
 static createFrame f18=new createFrame();
 
 static createFrame f19=new createFrame();
 static createFrame f20=new createFrame();
 static createFrame f21=new createFrame();
 static createFrame f22=new createFrame();
 static createFrame f23=new createFrame();
 static createFrame f24=new createFrame();
 static createFrame f25=new createFrame();
 static createFrame f26=new createFrame();
 static createFrame f27=new createFrame();

 static createFrame f28=new createFrame();
 static createFrame f29=new createFrame();
 static createFrame f30=new createFrame();
 static  createFrame f31=new createFrame();
 static createFrame f32=new createFrame();
 static createFrame f33=new createFrame();
 static createFrame f34=new createFrame();
 static createFrame f35=new createFrame();
 static createFrame f36=new createFrame();
 
 static createFrame f37=new createFrame();
 static createFrame f38=new createFrame();
 static  createFrame f39=new createFrame();
 static createFrame f40=new createFrame();
 static createFrame f41=new createFrame();
 static createFrame f42=new createFrame();
 static createFrame f43=new createFrame();
 static createFrame f44=new createFrame();
 static lastframe f45=new lastframe();
 String std="f45";
 public static void main(String[] args) {
  Dimension gh=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  int i=0,j=0;
  f1.frame(20,20,"samosa");
  f18.frame(900,130,"aloo_bharta");
  f17.frame(790,130,"vada 3");
  f16.frame(680,130,"tea");
  f15.frame(570,130,"roti");
  f14.frame(460,130,"rice");
  f3.frame(240,20,"biscuit");f2.frame(130,20,"aluchup");
  f13.frame(350,130,"puri");
  f12.frame(240,130,"paratha");
  f11.frame(130,130,"paneer_masala");
  f10.frame(20,130,"mudhi");
  f9.frame(900,20,"mixture");
  f8.frame(790,20,"dosa");
  f7.frame(680,20,"dal");
  f6.frame(570,20,"cold_drink");
  f5.frame(460,20,"cofee");
  f4.frame(350,20,"chowmin");

  f44.frame(790,460,"1");
  f43.frame(680,460,"1");
  f42.frame(570,460,"1");
  f41.frame(460,460,"1");
  f40.frame(350,460,"1");
  f39.frame(240,460,"1");
  f38.frame(130,460,"1");
  f37.frame(20,460,"1");
  f36.frame(900,350,"1");
  f35.frame(790,350,"1");
  f34.frame(680,350,"1");
  f33.frame(570,350,"tomatto_khatta");
  f32.frame(460,350,"alu_beans");
  f31.frame(350,350,"alu_potal");
  f30.frame(240,350,"dahi_baigan");
  f29.frame(130,350,"saga");
  f28.frame(20,350,"salad");
  f27.frame(900,240,"ghnta_curry_fry");
  f26.frame(790,240,"dalma");
  f25.frame(680,240,"ghanta_curry_masala");
  f24.frame(570,240,"gaja_muga");
  f23.frame(460,240,"chilly_paneer");
  f22.frame(350,240,"dahi_salad");
  f21.frame(240,240,"dahi");
  f20.frame(130,240,"baigan_bharta");
  f19.frame(20,240,"alu_govi");
  f45.frame(900,460);
 }
 void allDispose()
  {
  f1.dispose().dispose();
  f2.dispose().dispose();
  f3.dispose().dispose();
  f4.dispose().dispose();
  f5.dispose().dispose();
  f6.dispose().dispose();
  f7.dispose().dispose();
  f8.dispose().dispose();
  f9.dispose().dispose();
  f10.dispose().dispose();
  f11.dispose().dispose();
  f12.dispose().dispose();
  f13.dispose().dispose();
  f14.dispose().dispose();
  f15.dispose().dispose();
  f16.dispose().dispose();
  f17.dispose().dispose();
  f18.dispose().dispose();
  f19.dispose().dispose();
  f20.dispose().dispose();
  f21.dispose().dispose();
  f22.dispose().dispose();
  f23.dispose().dispose();
  f24.dispose().dispose();
  f25.dispose().dispose();
  f26.dispose().dispose();
  f27.dispose().dispose();
  f28.dispose().dispose();
  f29.dispose().dispose();
  f30.dispose().dispose();
  f31.dispose().dispose();
  f32.dispose().dispose();
  f33.dispose().dispose();
  f34.dispose().dispose();
  f35.dispose().dispose();
  f36.dispose().dispose();
  f37.dispose().dispose();
  f38.dispose().dispose();
  f39.dispose().dispose();
  f40.dispose().dispose();
  f41.dispose().dispose();
  f42.dispose().dispose();
  f43.dispose().dispose();
  f44.dispose().dispose();
  f45.dispose().dispose();
  
  
  }
}
Createframe.java//To create small frames
 
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class createFrame {
    final JFrame jf = new JFrame("JIFrameDemo Main Window");
	public JFrame dispose(){
		return jf;
	}
	public void frame(int xPos,int yPos,final String img){
    jf.setUndecorated(true);
    jf.setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),0));
    jf.setSize(100,100);
    jf.setLocation(xPos, yPos);
    JDesktopPane dtp = new JDesktopPane();
    dtp. setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),100));
   // dtp.setBackground(Color.GREEN);
    jf.setContentPane(dtp);  
    jf.setType(javax.swing.JFrame.Type.UTILITY);
    
	((JComponent)jf.getContentPane()).setBorder(    
	        BorderFactory.createMatteBorder( 2, 2, 2, 2, Color.black ) );
	
	final JLabel lblX = new JLabel("X");
	lblX.setLocation(89,5);
	lblX.setForeground(new Color(255, 255,255));
	lblX.setToolTipText("Close\r\n");
	lblX.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent arg0) {
			jf.dispose();
		}
		@Override
		public void mouseEntered(MouseEvent e) {
			lblX.setForeground(new Color(255, 0, 0));
		}
		public void mouseExited(MouseEvent e) {
			lblX.setForeground(new Color(255, 255,255));
		}
	});
	lblX.setSize(10,10);
	lblX.setFocusable(false);	
	jf.add(lblX);
	
	final JLabel lblX1 = new JLabel("");
	lblX1.setLocation(0,0);
	lblX1.setForeground(new Color(255, 255,255));
	final addFood fd=new addFood();
	lblX1.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) 
		{
			//jf.getName();
			//System.out.println(img);
			String list;
			String bef=fd.returnlast();
			if (bef=="")
				list=bef+""+img;
			else 
			  {
				list=bef+","+img;
			  }
			fd.addnew(list);
		}
	});
	lblX1.setSize(110,110);
	lblX1.setToolTipText(img);
	lblX1.setFocusable(false);
	jf.add(lblX1);
    jf.setVisible(true);
    }
}
LastFrame.java//Added to close all the opened frames when clicked.
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class lastframe {

    final JFrame jf = new JFrame("JIFrameDemo Main Window");
	public JFrame dispose(){
	return jf;
}
	public void frame(int xPos,int yPos){
    jf.setUndecorated(true);
    jf.setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),0));
    jf.setSize(100,100);
    jf.setLocation(xPos, yPos);
    JDesktopPane dtp = new JDesktopPane();
    dtp. setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),100));
   // dtp.setBackground(Color.GREEN);
    jf.setContentPane(dtp);  
    jf.setType(javax.swing.JFrame.Type.UTILITY);
    
	((JComponent)jf.getContentPane()).setBorder(    
	        BorderFactory.createMatteBorder( 2, 2, 2, 2, Color.black ) );
	
	final JLabel lblX = new JLabel("X");
	lblX.setLocation(89,5);
	lblX.setForeground(new Color(255, 255,255));
	lblX.setToolTipText("Close\r\n");
	lblX.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent arg0) {
			jf.dispose();
		}
		@Override
		public void mouseEntered(MouseEvent e) {
			lblX.setForeground(new Color(255, 0, 0));
		}
		public void mouseExited(MouseEvent e) {
			lblX.setForeground(new Color(255, 255,255));
		}
	});
	lblX.setSize(10,10);
	lblX.setFocusable(false);	
	jf.add(lblX);
	
	final JLabel lblX1 = new JLabel("");
	lblX1.setLocation(0,0);
	lblX1.setForeground(new Color(255, 255,255));
	lblX1.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) 
		{
			//jf.getName();
			System.out.println("completed");
			JIFrameDemo xy=new JIFrameDemo();
			xy.allDispose();
		}
	});
	lblX1.setSize(110,110);
	lblX1.setToolTipText("Completed");
	lblX1.setFocusable(false);
	jf.add(lblX1);
    jf.setVisible(true);
    }
}
addFood.java//To make a string of clicked images
 

public class addFood { 
	static String before="" ;
	public String returnlast(){
		return before;
	}
public void addnew(String food)
    {
	  before=food;
	  System.out.println(before);
	}
}