Java palindrome checker

What is palindrome ?
-Palindrome is a word or sequence that reads the same backwards as forwards.


For example :
MADAM

***************************************************************
COMPLETE SOURCE CODE FOR : JavaPalindromeChecker.java
***************************************************************


import java.util.Scanner;
import java.util.StringTokenizer;

public class JavaPalindromeChecker
{
public static void main(String[]args)
{
System.out.println("Put your text below");
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
StringTokenizer st=new StringTokenizer(a);
String b="";
while(st.hasMoreTokens())
{
b=b+st.nextToken();
}
int stringLength=b.length();
String benchMark="Palindrome";
if(stringLength%2!=0)
{
int c=((stringLength-1)/2);
for(int i=0;i<c;i++)
{
char before=b.charAt(c-(i+1));
char after=b.charAt(c+(i+1));
if(before!=after)
{
benchMark=new String("Not Palindrome");
}
}
}
else
{
int c=stringLength/2;
for(int i=0;i<c;i++)
{
char before=b.charAt(i);
char after=b.charAt(stringLength-(i+1));
if(before!=after)
{
benchMark=new String("Not Palindrome");
}
}
}
System.out.println(benchMark);
}
}


***************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************

Java transparent splash screen



Sample


**********************************************************************
TRANSPARENT IMAGE THAT WILL BE USE IN SOURCE CODE (batman.png)
**********************************************************************
Note : Make sure batman.png locate at same location with java file below.You can use other location, but for this simple tutorial, we will use same location.





*******************************************************************
COMPLETE SOURCE CODE FOR : TransparentSplashScreen.java
*******************************************************************

import javax.swing.JWindow;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;


public class TransparentSplashScreen extends JWindow
{
//Get transparent image that will be use as splash screen image.
Image bi=Toolkit.getDefaultToolkit().getImage("batman.png");

ImageIcon ii=new ImageIcon(bi);

public TransparentSplashScreen()
{
try
{
setSize(ii.getIconWidth(),ii.getIconHeight());
setLocationRelativeTo(null);
show();
Thread.sleep(10000);
dispose();
JOptionPane.showMessageDialog(null,"This program will exit !!!","<>",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}

//Paint transparent image onto JWindow
public void paint(Graphics g)
{
g.drawImage(bi,0,0,this);
}

public static void main(String[]args)
{
TransparentSplashScreen tss=new TransparentSplashScreen();
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Java print screen

Complete source code below will show you, how to implement print screen or capture screen image in java. Program below will capture screen during it's executing time and after that, it will paint the image on a canvas before add it into a JFrame to display it's result.

*************************************************************
COMPLETE SOURCE CODE FOR : CaptureScreen.java
*************************************************************


import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Canvas;
import java.awt.Graphics;

import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class CaptureScreen extends Canvas
{
Rectangle screenRectangle=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

Robot myRobot;

BufferedImage screenImage;

public CaptureScreen()
{
try
{
myRobot=new Robot();
}
catch(Exception exception)
{
exception.printStackTrace();
}

screenImage=myRobot.createScreenCapture(screenRectangle);

JFrame myFrame=new JFrame("Capture Screen");

myFrame.add(this);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height);
myFrame.setVisible(true);
}

public void paint(Graphics g)
{
g.drawImage(screenImage,0,0,this);
}

public static void main(String[]args)
{
CaptureScreen cs=new CaptureScreen();
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Change default metal look and feel color



*********************************************************************
COMPLETE SOURCE CODE FOR : ChangeMetalLookAndFeelColor.java
*********************************************************************


import javax.swing.plaf.metal.DefaultMetalTheme;

import javax.swing.plaf.metal.MetalLookAndFeel;

import javax.swing.plaf.ColorUIResource;

import javax.swing.*;

import java.awt.FlowLayout;

public class ChangeMetalLookAndFeelColor
{
public ChangeMetalLookAndFeelColor()
{
JFrame.setDefaultLookAndFeelDecorated(true);

MetalLookAndFeel.setCurrentTheme(new OrangeTheme());

JFrame frame=new JFrame("Orange Theme");

frame.getContentPane().setLayout(new FlowLayout());

frame.getContentPane().add(new JButton("JButton"));
frame.getContentPane().add(new JCheckBox("JCheckBox"));
frame.getContentPane().add(new JTextField("JTextField"));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
ChangeMetalLookAndFeelColor cmlafm=new ChangeMetalLookAndFeelColor();
}

class OrangeTheme extends DefaultMetalTheme
{
//NEW COLOR FOR METAL LOOK AND FEEL
ColorUIResource primary1=new ColorUIResource(255,215,76);
ColorUIResource primary2=new ColorUIResource(255,198,0);
ColorUIResource primary3=new ColorUIResource(205,162,11);

ColorUIResource secondary1=new ColorUIResource(255,187,57);
ColorUIResource secondary2=new ColorUIResource(255,168,0);
ColorUIResource secondary3=new ColorUIResource(255,214,104);
//NEW COLOR FOR METAL LOOK AND FEEL

protected ColorUIResource getPrimary1()
{
return primary1;
}

protected ColorUIResource getPrimary2()
{
return primary2;
}

protected ColorUIResource getPrimary3()
{
return primary3;
}

protected ColorUIResource getSecondary1()
{
return secondary1;
}

protected ColorUIResource getSecondary2()
{
return secondary2;
}

protected ColorUIResource getSecondary3()
{
return secondary3;
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Get last selected JRadioButton

Complete source code below will show you, how to get last selected JRadioButton when we click on other JRadioButton. I hope you will understand what i want to tell you, or you can try to compile and execute source code below.

******************************************************************
COMPLETE SOURCE CODE FOR : GetLastSelectedJRadioButton.java
******************************************************************


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GetLastSelectedJRadioButton implements ItemListener
{
JRadioButton jrd1=new JRadioButton("a");
JRadioButton jrd2=new JRadioButton("b");
JRadioButton jrd3=new JRadioButton("c");
JRadioButton jrd4=new JRadioButton("d");

JRadioButton last=null;

ButtonGroup bg=new ButtonGroup();

JFrame myFrame=new JFrame("Get last selected JRadioButton");

public GetLastSelectedJRadioButton()
{
jrd1.addItemListener(this);
jrd2.addItemListener(this);
jrd3.addItemListener(this);
jrd4.addItemListener(this);

bg.add(jrd1);
bg.add(jrd2);
bg.add(jrd3);
bg.add(jrd4);

myFrame.getContentPane().setLayout(new GridLayout(4,1));

myFrame.getContentPane().add(jrd1);
myFrame.getContentPane().add(jrd2);
myFrame.getContentPane().add(jrd3);
myFrame.getContentPane().add(jrd4);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}

public void itemStateChanged(ItemEvent event)
{
JRadioButton temp=(JRadioButton)event.getItem();

if(event.getStateChange()==ItemEvent.DESELECTED)
{
last=temp;
}
else if(event.getStateChange()==ItemEvent.SELECTED)
{
if(last!=null)
{
JOptionPane.showMessageDialog(myFrame,"LAST : "+last.getText(),"Last",JOptionPane.INFORMATION_MESSAGE);
}
}
}

public static void main(String[]args)
{
GetLastSelectedJRadioButton glsjrb=new GetLastSelectedJRadioButton();
}
}


******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************

Set JFrame decoration to MetalLookAndFeel decoration



************************************************************************
COMPLETE SOURCE CODE FOR : SetJFrameDecorationToMetalLookAndFeel.java
************************************************************************


import javax.swing.*;
import java.awt.FlowLayout;

public class SetJFrameDecorationToMetalLookAndFeel
{
public static void main(String[]args)
{
//Set JFrame decoration to current look and feel decoration
//You can try to make this line as comment and
//after that try compile and execute it to see it's differential
JFrame.setDefaultLookAndFeelDecorated(true);

JButton myButton=new JButton("Click On Me");

JFrame myFrame=new JFrame("Set JFrame window decoration to current look and feel");
myFrame.setLayout(new FlowLayout());

myFrame.add(myButton);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Check word in String

Complete source code below will show you how to check word if exist or not in a String. It will compare a word that we want to check with all word that separate by white space in targeted String. Comparison will ignore case.

**************************************************************
COMPLETE SOURCE CODE FOR : CheckWordInString.java
**************************************************************


import java.util.StringTokenizer;

public class CheckWordInString
{
public static void main(String[]args)
{
String a="Hello World !";

/*
*Word(hello) that we want
*to check if exist
*or not in String a
*/
String b="hello";

/*
*Get all word that
*separate by white space
*in String a
*/
StringTokenizer st
=new StringTokenizer(a);

while(st.hasMoreTokens())
{
String temp=st.nextToken();

/*
*Compare with
*ignore case
*/
if(temp.equalsIgnoreCase(b))
{
System.out.println
("b exist in a");
break;
}
}
}
}


**************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************

Java simple timer

Complete source code below will show you, how to create simple timer using java. Actually it has a problem. The problem that i mean is, it seem more slow than real timer.If you find how to solve it, i hope you can share with me by put a comment.

*******************************************************************
COMPLETE SOURCE CODE FOR : JavaSimpleTimer.java
*******************************************************************


import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class JavaSimpleTimer extends JPanel implements ActionListener
{
int miliseconds=0;
int seconds=0;
int minutes=0;

Timer myTimer;

Font timerFont=new Font("Verdana",Font.BOLD,24);

public JavaSimpleTimer()
{
myTimer=new Timer(10,this);

JFrame myFrame=new JFrame("Simple Timer");

myFrame.add(this);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(500,500);
myFrame.setVisible(true);

myTimer.setInitialDelay(0);
myTimer.start();
}

public void paint(Graphics g)
{
super.paint(g);
String mili=Integer.toString(miliseconds);
String sec=Integer.toString(seconds);
String min=Integer.toString(minutes);

if(mili.length()==1)
{
mili="0"+mili;
}
if(sec.length()==1)
{
sec="0"+sec;
}
if(min.length()==1)
{
min="0"+min;
}

g.setFont(timerFont);

g.drawString(min+" :",20,20);
g.drawString(sec+" :",80,20);
g.drawString(mili,140,20);
}

public void actionPerformed(ActionEvent event)
{
miliseconds=miliseconds+1;
if(miliseconds==100)
{
miliseconds=0;
seconds=seconds+1;
}
if(seconds==60)
{
seconds=0;
minutes=minutes+1;
}
repaint();
}

public static void main(String[]args)
{
JavaSimpleTimer myTest=new JavaSimpleTimer();
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Set JSlider tick color

Complete source code below will show you, how to set JSlider tick color. I have tried to find method in JSlider class to set JSlider tick color...but, i can't find it. So i tried to create UI Delegate only for SliderUI that change JSlider tick color. You can try to compile both source code below. Make sure both java file below locate at the same location. JSlider's tick color is set in MyNewMetalSliderUI.java.You can try to change it's color follow what color that you want base on RGB value. After you compile SetJSliderTickColor.java and MyNewMetalSliderUI.java, you try execute SetJSliderTickColor.java. This is because, main method contain in this file.Oooo...before i forget, it's only handle horizontal slider. If you want to make it can use for vertical slider, you can try overwrite method paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) and paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) in MetalSliderUI class. You can review it in Java SE API for MetalSliderUI class.

**********************************************************************
COMPLETE SOURCE CODE FOR : MyNewMetalSliderUI.java
**********************************************************************


import javax.swing.*;

import java.awt.*;

import javax.swing.plaf.ColorUIResource;

import javax.swing.plaf.metal.MetalSliderUI;

import javax.swing.plaf.ComponentUI;

public class MyNewMetalSliderUI extends MetalSliderUI
{
// Create our own slider UI
public static ComponentUI createUI(JComponent c )
{
return new MyNewMetalSliderUI();
}

//*******************HORIZONTAL MAJOR TICK*******************
public void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;

if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=255,GREEN=0,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(255,0,0);

//Set color that will use to draw MAJOR TICK using created color
g.setColor(majorTickColor);

//Draw MAJOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height);
}
}
//*******************HORIZONTAL MAJOR TICK*******************

//*******************HORIZONTAL MINOR TICK*******************
public void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;

if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=12,GREEN=255,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(12,255,0);

//Set color that will use to draw MINOR TICK using created color
g.setColor(majorTickColor);

//Draw MINOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height/2);
}
}
//*******************HORIZONTAL MINOR TICK*******************
}


**********************************************************************
JUST COMPILE
**********************************************************************

**********************************************************************
COMPLETE SOURCE CODE FOR : SetJSliderTickColor.java
**********************************************************************

import javax.swing.*;

import java.awt.*;

public class SetJSliderTickColor
{
public SetJSliderTickColor()
{
//Create slider using JSlider
JSlider mySlider=new JSlider();

//Set major tick spacing to 10
mySlider.setMajorTickSpacing(10);

//Set minor tick spacing to 1
mySlider.setMinorTickSpacing(1);

//Make slider's numbers visible
mySlider.setPaintLabels(true);

//Make slider's ticks visible
mySlider.setPaintTicks(true);

//Create a window using JFrame with title ( Set JSlider tick color )
JFrame frame=new JFrame("Set JSlider tick color");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Add created JSlider into JFrame
frame.add(mySlider);

//Set JFrame's default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,300);

//Set JFrame locate at center of screen
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}

public static void main(String[]args)
{
/***********Set our own UI Delegate for Slider*************/
UIManager uim=new UIManager();

//MyNewMetalSliderUI contain settings that use to change tick color
uim.put("SliderUI","MyNewMetalSliderUI");
/***********Set our own UI Delegate for Slider*************/

SetJSliderTickColor sjstc=new SetJSliderTickColor();
}
}


**********************************************************************
JUST COMPILE AND EXECUTE
**********************************************************************

Set JOptionPane warning icon

Complete source code below will show you, how to set warning icon in JOptionPane.

**************************************************************************
COMPLETE SOURCE CODE FOR : SetJOptionPaneWarningIcon.java
**************************************************************************


import javax.swing.*;

public class SetJOptionPaneWarningIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Warning Icon-text in message box
//WARNING-JOptionPane title
//JOptionPane.WARNING_MESSAGE-Set warning icon that will be show in message box
JOptionPane.showMessageDialog(null,"Warning Icon","WARNING",JOptionPane.WARNING_MESSAGE);
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

RELAXING NATURE VIDEO