Showing posts with label tooltiptext. Show all posts
Showing posts with label tooltiptext. Show all posts

Java simple image magnifier tooltip


Sometime when we play with images...i mean here a lot of images, like we let user choose an image from 500 images in a JFrame and all images show at the same time without any scrollbar, it can give user a little problem if the image too small. So, how can user make a good choice. This give me some idea to built my own image tooltip. This is a simple tooltip. It will enlarge image when someone hover mouse cursor on it.Image below is a result from program below.


Before you compile and execute it, you should download all images below. After that place them at same location with this java source file.

Love_Music_by_jovincent.jpg

Leafs_1_by_NerghaL.jpg

Hancock-1611.jpg

matrix.jpg

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


import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

import java.awt.GridLayout;
import java.awt.Graphics;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class ImageToolTip
{
public static void main(String[]args)
{
PanelImage imageA=new PanelImage("matrix.jpg");

PanelImage imageB=new PanelImage("Hancock-1611.jpg");

PanelImage imageC=new PanelImage("Leafs_1_by_NerghaL.jpg");

PanelImage imageD=new PanelImage("Love_Music_by_jovincent.jpg");

JFrame myMainWindow=new JFrame("Image Tool Tip");
myMainWindow.setResizable(false);
myMainWindow.getContentPane().setLayout(new GridLayout(2,2));
myMainWindow.getContentPane().add(imageA);
myMainWindow.getContentPane().add(imageB);
myMainWindow.getContentPane().add(imageC);
myMainWindow.getContentPane().add(imageD);

myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setSize(100,100);
myMainWindow.setLocationRelativeTo(null);
myMainWindow.setVisible(true);
}
}

class PanelImage extends JPanel implements MouseListener
{
ImageIcon temp;
ImageMagnifier im;

public PanelImage(String a)
{
addMouseListener(this);
temp=new ImageIcon(a);
}

public void paint(Graphics g)
{
super.paint(g);
g.drawImage(temp.getImage(),0,0,getSize().width,getSize().height,this);
}

public void mouseClicked(MouseEvent event)
{
}

public void mouseEntered(MouseEvent event)
{
im=new ImageMagnifier(temp,getSize().width,getSize().height,event.getXOnScreen(),event.getYOnScreen());
}

public void mouseExited(MouseEvent event)
{
im.dispose();
}

public void mousePressed(MouseEvent event)
{
}

public void mouseReleased(MouseEvent event)
{
}
}

class ImageMagnifier extends JFrame
{
ImageIcon temp;

public ImageMagnifier(ImageIcon imageFile,int width,int height,int x,int y)
{
setUndecorated(true);
temp=imageFile;
setLocation(x,y);
setSize(width*9,height*9);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.drawImage(temp.getImage(),0,0,getSize().width,getSize().height,this);
}
}


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

Set ToolTipText background color

Complete source code below, will show you how to set ToolTipText background color.

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


import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Color;

public class SetToolTipTextBackgroundColor
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

//Create a window using JFrame with text ( Set ToolTipText background color )
JFrame frame=new JFrame("Set ToolTipText background color");

public SetToolTipTextBackgroundColor()
{
//Create a color base on RGB value
//You can get your color value base on RGB using Color picker at above
//R=204 G=0 B=153
Color backgroundColor=new Color(204,0,153);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext background color using created Color
uim.put("ToolTip.background",backgroundColor);

//Set tooltiptext for created button
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextBackgroundColor stttbc=new SetToolTipTextBackgroundColor();
}
}


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

Set ToolTipText text font

Complete source code below, will show you how to set ToolTipText text font.

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


import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Font;

public class SetToolTipTextFont
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

//Create a window using JFrame with text ( Set ToolTipText text font )
JFrame frame=new JFrame("Set ToolTipText text font");

public SetToolTipTextFont()
{
//Create a font :
//Name : Verdana
//Style : Bold
//Size : 20
Font textFont=new Font("Verdana",Font.BOLD,20);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext text font using created Font
uim.put("ToolTip.font",textFont);

//Set tooltiptext for created button
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextFont stttf=new SetToolTipTextFont();
}
}


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

Set ToolTipText text color

Complete source code below, will show you how to set ToolTipText text color.

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


import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Color;

public class SetToolTipTextColor
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

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

public SetToolTipTextColor()
{
//Create a color base on RGB value
//You can get your color value base on RGB using Color picker at above
//R=255 G=255 B=255 is RGB value for white
Color textColor=new Color(255,255,255);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext text color using created Color
uim.put("ToolTip.foreground",textColor);

//Set tooltiptext for created button
//So, it's tooltiptext text color will be WHITE
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextColor stttc=new SetToolTipTextColor();
}
}


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

Tool tip text

Tool tip text is a description about a component. For example, about a button in a graphical user interface. A tool tip text will appear when you hover and stop for certain time on the button.

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


import javax.swing.JFrame;
import javax.swing.JButton;

public class ToolTipText
{
public static void main(String[]args)
{
//Create a button with text ( Hover on me and don't move )
JButton button=new JButton("Hover on me and don't move");

//SET TOOL TIP TEXT TO THE BUTTON
//Text : I am tool tip text
button.setToolTipText("I am tool tip text");

//Create a JFrame with title ( Tool tip text )
JFrame frame=new JFrame("Tool tip text");

//Add created button into JFrame
frame.add(button);

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

//Set JFrame size
frame.setSize(250,65);

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


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

RELAXING NATURE VIDEO