Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, 12 November 2015

Java Code to Generate HTML form

This is the simple Java program that converts the user inputs in to HTML form (paragraph). You can use this HTML document in any web document. The basic purpose behind this code us to give a understanding to the beginners about the connectivity of Java with different web platform.

Source Code 


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lep;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author UzairYousafZai
 */
public class produceHTMLfromJava {

/**
     * @param args the command line arguments
     */
public static void main(String[] args) throws IOException {
start();
}

private static void start() throws IOException {
String html = generate_HTML_string();
generate_HTML_file(html);

}

private static String generate_HTML_string() throws IOException {
System.out.println("generateTheTicket()");
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<html>");
htmlBuilder.append("<head><title>Hello World</title></head>");
htmlBuilder.append("<body><p>"+getParagraph1()+"<br>"+ getParagraph2()+"</p></body>");
htmlBuilder.append("</html>");
String html = htmlBuilder.toString();
return html;
}

private static void generate_HTML_file(String html) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter writer = new PrintWriter("name.html", "UTF-8");
writer.println(html);
writer.close();
}

private static String getParagraph1() throws IOException {
System.out.println("Enter First Paragraph");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}

private static String getParagraph2() throws IOException {
System.out.println("Enter Second Paragraph");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}

}

Output of the Program

Java Code to Generate HTML form






Tuesday, 10 November 2015

Programming logical Questions and logic building conceptual code | Mini Games Project in Java

This is the simple but interesting game in Java for beginners to understand the programming logic. The logic behind the program is to match the same numbers in a row or column. The Simple Java program is a mini project for beginners as well to work on GUI.

Source Code 

package Game;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;

public class Game extends JFrame implements ActionListener {

/**
     *
     */
private static final long sVUID = 1L;
private JButton exitPressed, replayPressed;
private JButton[] allButtons = new JButton[16];
private ArrayList<Integer> list = new ArrayList<Integer>();
private int counter = 0;
private int[] id = new int[2];
private int[] value = new int[2];

public Game() {
initComponents();
initPanel();
setArrayListText();
setTitle("Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}

public void initComponents() {
for (int i = 0; i < allButtons.length; i++) {
allButtons[i] = new JButton();
allButtons[i].setFont(new Font("Serif", Font.BOLD, 28));
allButtons[i].addActionListener(this);
}
exitPressed = new JButton("Exit");
exitPressed.addActionListener(this);
replayPressed = new JButton("Replay");
replayPressed.addActionListener(this);
}

public void initPanel() {
Panel gamePnl = new Panel();
gamePnl.setLayout(new GridLayout(4, 4));
for (int i = 0; i < allButtons.length; i++) {
gamePnl.add(allButtons[i]);
}

Panel buttonPnl = new Panel();
buttonPnl.add(replayPressed);
buttonPnl.add(exitPressed);
buttonPnl.setLayout(new GridLayout(1, 0));

add(gamePnl, BorderLayout.CENTER);
add(buttonPnl, BorderLayout.SOUTH);

}

public void setArrayListText() {
for (int i = 0; i < 2; i++) {
for (int ii = 1; ii < (allButtons.length / 2) + 1; ii++) {
list.add(ii);
}
}
shuffle(list);

int newLine = 0;
for (int a = 0; a < list.size(); a++) {
newLine++;
System.out.print(" " + list.get(a));
if (newLine == 4) {
System.out.println();
newLine = 0;
}
}
}

public boolean sameValues() {
if (value[0] == value[1]) {
return true;
}
return false;
}

@Override
public void actionPerformed(ActionEvent e) {
if (exitPressed == e.getSource()) {
System.exit(0);
}
if (replayPressed == e.getSource()) {
this.dispose();
new Game();
}
for (int i = 0; i < allButtons.length; i++) {
if (allButtons[i] == e.getSource()) {
allButtons[i].setText("" + list.get(i));
allButtons[i].setEnabled(false);
counter++;
if (counter == 3) {
if (sameValues()) {
allButtons[id[0]].setEnabled(false);
allButtons[id[1]].setEnabled(false);
} else {
allButtons[id[0]].setEnabled(true);
allButtons[id[0]].setText("");
allButtons[id[1]].setEnabled(true);
allButtons[id[1]].setText("");
}
counter = 1;
}
if (counter == 1) {
id[0] = i;
value[0] = list.get(i);
}
if (counter == 2) {
id[1] = i;
value[1] = list.get(i);
}
}
}
}

public static void main(String[] args) {
new Game();
}
}

Output of the Program


Mini Games Project in Java


Tuesday, 31 March 2015

How to convert a string from array list to array of char in Java | Array List to Array Java | Array list to Array string | Array List to Array Conversion in Java | Array List to Array example

The Program is about Array List and array in Java with GUI feature. The convert a string from array list to array of char and display the result in array of char. The array list is built-in data structure Java and very beneficial when we want to store data linearly or or data need indexing.

Source Code     

package lep;

import java.util.ArrayList;
/**
 * @author Uzair
 */
public class ArrayListToStringArray {

public static void main(String args[]) {
System.out.println("\n\n\n"
+"\t*******************************************************\n"
+"\t** **\n"
+"\t** Welcome To **\n"
+"\t** **\n"
+"\t** ArrayList<String> To String[] **\n"
+"\t** **\n"
+"\t** Conversion Program **\n"
+"\t** **\n"
+"\t*******************************************************\n");
System.out.println("\t\t\t");

//ArrayList<String> to store list of strings
ArrayList<String> all_types = new <String>ArrayList();
all_types.add("One");
all_types.add("Two");
all_types.add("Three");
all_types.add("Other");

//String[] is an array of string
String[] itemArr = new String[all_types.size()];
itemArr = all_types.toArray(itemArr);

System.out.println(" Displaying String : ");
for(String s : itemArr)
System.out.println(s);
}

}

Output of the Program

How to convert a string from array list to array of char in Java

Monday, 30 March 2015

A New way to do the for Loop in Java | advanced for loop in Java | Advanced for Loop in Java ArrayList | advanced for loop in java for list |

This is the new type of for loop which was added to the Java in 2004. The advantage of this enhanced for loop is that you just iterate linearly through a collection, or linearly through an array, and there is no need to modify the array or collection. The biggest advantages of this advanced for loop is that you no need to deal with the index numbers of array or array list and that's why this loop is used only in specific cases.

Source Code  

package temp;

import java.util.ArrayList;
/**
 * @author Uzair
 */
public class ForEach {

public static void main(String args[]) {
System.out.println("\n\n\n"
+"\t*******************************************************\n"
+"\t** Welcome To **\n"
+"\t** For Each **\n"
+"\t** Demo **\n"
+"\t*******************************************************\n");
System.out.println("\t\t\t");

ArrayList<String> list= new <String>ArrayList();
System.out.println("Inserting By Simple For Loop");
for (int i=0;i<10;i++){
list.add(Integer.toString(i+1));
System.out.println("Inserting : "+Integer.toString(i+1));
}
System.out.println("**********************************\n");
System.out.println("Retrieving By Simple For-Each Loop");
for(Object s : list)
System.out.println("Retrieving : "+s);
}

}

Output of the Program

A New way to do the for Loop in Java

How to Display two label in one area in Java GUI | Multiple label in one place in GUI Java | Java GUI Tutorials | Java GUI example

This is the simple Java GUI base program in which we display multiple labels in one area. This type of application or programs used where we want to display multiple label at one place. Each time when we click on the button the label will be change.

Source Code 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package TempClass;

/**
 *
 * @author UzairYousafZai
 */
public class LayeredPane extends javax.swing.JFrame {
boolean toggle=true;
public LayeredPane() {
initComponents();

}

/**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLayeredPane1 = new javax.swing.JLayeredPane();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jToggleButton1 = new javax.swing.JToggleButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel2.setText("This is lable 2");

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(18, Short.MAX_VALUE)
.addComponent(jLabel2)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel1.setText("This is lable 1");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);
jLayeredPane1.setLayout(jLayeredPane1Layout);
jLayeredPane1Layout.setHorizontalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jLayeredPane1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(47, 47, 47)))
);
jLayeredPane1Layout.setVerticalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
);
jLayeredPane1.setLayer(jPanel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

jToggleButton1.setText("ToggleButton");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton1)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton1))
);

pack();
}// </editor-fold>

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (toggle==false){
jPanel1.setVisible(true);
jPanel2.setVisible(false);
toggle=true;
}else if (toggle==true){
jPanel1.setVisible(false);
jPanel2.setVisible(true);
toggle=false;
}
}

/**
     * @param args the command line arguments
     */
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LayeredPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LayeredPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LayeredPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LayeredPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LayeredPane().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}

Output of the Program

 java gui development