You are on page 1of 9

import java.awt.

*;

private int magnifierSize;

import java.awt.event.*;

private int locationX;

import javax.swing.*;

private int locationY;


private Area circle1;

public class Magnifier{


public static void main(String arg[]){
MagnifierWindow magnifierWindow =
new MagnifierWindow("Magnifier");
}

private Area circle2;


private Area circle3;
private Area circle4;
private Area circle5;
private Area rectangle1;

private Color brightGlassColor;

public class MagnifierIcon{


public static byte magnifierIcon[]={
71,73,70,56,57,97,16,0,16,0,111,0,0,0,0,0,-1,-1,-1,
-25,-25,-25,-1,-1,-1,33,7,4,1,0,0,3,0,44,0,0,0,0,16,
0,16,0,0,2,43,-100,45,-87,-57,-93,-65,24,100,-30,80,-118,82,-104,-95,

private Color darkGlassColor;


private Color lightInGlassColor;
private Robot robot;

public MagnifierPanel(int magnifierSize){


try{robot = new Robot();}
catch(AWTException e){}

7,71,121,-30,-58,-115,94,105,118,80,42,82,-84,-86,-68,30,40,-56,31,109,

getScreen();
setMagnifierSize(magnifierSize);

91,-11,-37,-20,105,115,48,53,10,0,59
};

}
import java.awt.*;

public void setMagnifierLocation(int


locationX,int locationY){

import java.awt.event.*;

this.locationX = locationX;

import javax.swing.*;

this.locationY = locationY;

import javax.swing.event.*;
import java.awt.geom.*;

public class MagnifierPanel extends JPanel{


private Image screenImage;

updateMagnifierPicture();
}

public void setImage(Image screenImage){

this.screenImage = screenImage;
updateMagnifierPicture();
}

circle3.intersect(circle4);
brightGlassColor = new
Color(140,140,140,50);
darkGlassColor = new
Color(100,100,100,50);

public void getScreen(){


screenImage =
robot.createScreenCapture(new
Rectangle(0,0,Toolkit.getDefaultToolkit().get
ScreenSize().width,Toolkit.getDefaultToolkit(
).getScreenSize().height));
}

lightInGlassColor = new
Color(255,255,255,100);
setPreferredSize(new
Dimension(magnifierSize,magnifierSize));
if (getParent() != null)
getParent().repaint();
updateMagnifierPicture();

public void setMagnifierSize(int


magnifierSize){
this.magnifierSize = magnifierSize;
circle1 = new Area(new
Ellipse2D.Double(0,0,magnifierSize,magnifi
erSize));
circle2 = new Area(new
Ellipse2D.Double((magnifierSize/5),
(magnifierSize/5),(magnifierSize/10*11),
(magnifierSize/10*11)));
circle3 = new Area(new
Ellipse2D.Double(0,0,magnifierSize,magnifi
erSize));
circle4 = new Area(new
Ellipse2D.Double((magnifierSize/5),
(magnifierSize/5),(magnifierSize/10*11),
(magnifierSize/10*11)));
circle5 = new Area(new
Ellipse2D.Double(0,0,magnifierSize,magnifi
erSize));
rectangle1 = new Area(new
Rectangle2D.Double(0,0,magnifierSize,mag
nifierSize));
rectangle1.subtract(circle5);
circle1.subtract(circle2);

public void updateMagnifierPicture(){


if (getParent() != null)
getParent().repaint();
else repaint();
}

public void paintComponent(Graphics g){


super.paintComponent((Graphics2D)g);
drawScreenRectangle((Graphics2D)g);
}
private void
drawScreenRectangle(Graphics2D g){
g.setClip(rectangle1);
g.drawImage(screenImage,
0,
0,
magnifierSize,
magnifierSize,

locationX,

import javax.swing.event.*;

locationY,

import java.io.*;

locationX+magnifierSize,
locationY+magnifierSize,
this);
g.setClip(circle5);
g.drawImage(screenImage,
0,
0,
magnifierSize,
magnifierSize,
locationX+(magnifierSize/4),
locationY+(magnifierSize/4),
locationX+(magnifierSize/4*3),
locationY+(magnifierSize/4*3),
this);
g.setColor(darkGlassColor);
g.fill(circle1);
g.setColor(brightGlassColor);
g.fill(circle3);
g.setColor(lightInGlassColor);
g.fillOval((magnifierSize/5),
(magnifierSize/5),(magnifierSize/10),
(magnifierSize/10));

public class MagnifierWindow extends


JFrame{
private Container container =
getContentPane();
private JPopupMenu popupMenu = new
JPopupMenu();
private JMenuItem menuRefresh = new
JMenuItem("Refresh");
private JMenuItem menuHide = new
JMenuItem("Hide");
private JMenu menuSize = new
JMenu("Size");
private JMenuItem menu100Pixels = new
JMenuItem("100 Pixels");
private JMenuItem menu200Pixels = new
JMenuItem("200 Pixels");
private JMenu menuHelp = new
JMenu("Help");
private JMenuItem menuHow = new
JMenuItem("How to use");
private JMenuItem menuSite = new
JMenuItem("Visit web site");
private JMenuItem menuAbout = new
JMenuItem("About");
private JMenuItem menuExit = new
JMenuItem("Exit");

}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

private TimeUpdate timeUpdate = new


TimeUpdate();
private int timeUpdateDelay = 10;
private int onScreenMoveDidectionUp = 1;

private int onScreenMoveDidectionDown =


-1;
private int onScreenMoveDidectionX =
onScreenMoveDidectionUp;
private int onScreenMoveDidectionY =
onScreenMoveDidectionUp;

private int setCoordinateX;

private int updateScreenDelay = 500;

public MagnifierWindow(String
windowTitle){
super(windowTitle);
magnifierIcon = new
ImageIcon(MagnifierIcon.magnifierIcon).getI
mage();

private int setCoordinateY;

setIconImage(magnifierIcon);

private int absoluteCoordinateX;

setUndecorated(true);

private int absoluteCoordinateY;


private int
relativeCoordinateXWhenMousePressed;
private int
relativeCoordinateYWhenMousePressed;
private boolean mousePressedNow;
private boolean magnifierStopped;

container.add(magnifierPanel);
addMouseListener(new
MouseFunctions());
addMouseMotionListener(new
MouseMotionFunctions());
timeUpdate.start();
menuRefresh.addActionListener(

private ConfigData configData = new


ConfigData();

new ActionListener(){
public void
actionPerformed(ActionEvent e){
updateScreen();

private int screenWidth =


Toolkit.getDefaultToolkit().getScreenSize().w
idth;
private int screenHeight =
Toolkit.getDefaultToolkit().getScreenSize().h
eight;
private int magnifierSize = 100;

}
}
);
menuHide.addActionListener(
new ActionListener(){

private MagnifierPanel magnifierPanel =


new MagnifierPanel(magnifierSize);

public void
actionPerformed(ActionEvent e){
setState(JFrame.ICONIFIED);

private Image magnifierIcon;


}
}

);
menu100Pixels.addActionListener(
new ActionListener(){
public void
actionPerformed(ActionEvent e){
magnifierSize = 100;
updateSize(magnifierSize);

"3.Single click on right


mouse button stops magnifier movement
and shows menu.\n" +
"4.Move magnifier by
click on left mouse button on it and drag to
desired place.\n" +
"5.Refresh option in the
menu is needed to update last screen
status.\n" +
"6.Hide option in the
menu is needed to minimize magnifier");

}
}

jOptionPane.showMessageDialog(null,

);

jTextArea,

menu200Pixels.addActionListener(
new ActionListener(){
public void
actionPerformed(ActionEvent e){
magnifierSize = 200;
updateSize(magnifierSize);
}
}
);
menuHow.addActionListener(
new ActionListener(){

"How to use
magnifier",
JOptionPane.INFORMATION_MESSAGE);
}
}
);
menuSite.addActionListener(
new ActionListener(){
public void
actionPerformed(ActionEvent e){

public void
actionPerformed(ActionEvent e){

JOptionPane jOptionPane = new


JOptionPane();

JOptionPane jOptionPane = new


JOptionPane();

JTextField jTextField = new


JTextField();

JTextArea jTextArea = new


JTextArea();
jTextArea.setText("1.Single click on
left mouse button stops magnifier
movement.\n" +
"2.Double click on left
mouse button starts magnifier movement.\n"
+

jTextField.setText("http://softcollection.sytes.
net/javaprog");
jOptionPane.showMessageDialog(null,
jTextField,

"Please visit web

popupMenu.add(menuHelp);

site!",

menuHelp.add(menuHow);
menuHelp.add(menuSite);

JOptionPane.INFORMATION_MESSAGE);

menuHelp.add(menuAbout);

popupMenu.add(menuExit);

}
);

updateSize(magnifierSize);

menuAbout.addActionListener(

openFile();

new ActionListener(){
public void
actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"Ma
gnifier
v1.14","About",JOptionPane.INFORMATIO
N_MESSAGE);

show();
}

public void updateSize(int magnifierSize){


magnifierPanel.setMagnifierSize(magnifierS
ize);

}
}

setSize(magnifierSize,magnifierSize);

);

validate();

menuExit.addActionListener(

new ActionListener(){
public void
actionPerformed(ActionEvent e){

public void updateScreen(){


hide();

System.exit(0);
}

try{Thread.sleep(updateScreenDelay);}
catch(InterruptedException e){}

magnifierPanel.getScreen();

);
popupMenu.add(menuRefresh);

show();
}

popupMenu.add(menuHide);
popupMenu.add(menuSize);
menuSize.add(menu100Pixels);
menuSize.add(menu200Pixels);

private class TimeUpdate extends Thread{


public void run(){
while(true){

if (magnifierStopped == false){
setCoordinateX +=
onScreenMoveDidectionX;
setCoordinateY +=
onScreenMoveDidectionY;
if (setCoordinateX < 0)
onScreenMoveDidectionX =
onScreenMoveDidectionUp;
if (setCoordinateY < 0)
onScreenMoveDidectionY =
onScreenMoveDidectionUp;
if (setCoordinateX > (screenWidthmagnifierSize)) onScreenMoveDidectionX =
onScreenMoveDidectionDown;

mousePressedNow = true;
relativeCoordinateXWhenMousePressed=e.
getX();
relativeCoordinateYWhenMousePressed=e.
getY();
magnifierStopped = true;
}
if
(e.getButton()==MouseEvent.BUTTON1&&
e.getClickCount()==2){
magnifierStopped = false;
}

if (setCoordinateY > (screenHeightmagnifierSize)) onScreenMoveDidectionY =


onScreenMoveDidectionDown;
magnifierPanel.setMagnifierLocation(setCo
ordinateX,setCoordinateY);

}
public void mouseReleased(MouseEvent
e){
mousePressedNow = false;
saveFile();

setLocation(setCoordinateX,setCoordinateY
);

if (e.isPopupTrigger()){

}
try{Thread.sleep(timeUpdateDelay);}
catch(InterruptedException e){}

popupMenu.show(e.getComponent(),e.getX
(),e.getY());
}

}
}

}
}

private class MouseMotionFunctions


extends MouseMotionAdapter{
private class MouseFunctions extends
MouseAdapter{
public void mousePressed(MouseEvent
e){
if (e.getClickCount()==1){

public void mouseDragged(MouseEvent


e){
if (mousePressedNow == true){

absoluteCoordinateX =
MagnifierWindow.this.getLocationOnScreen
().x + e.getX();

}
catch(IOException e){}

absoluteCoordinateY =
MagnifierWindow.this.getLocationOnScreen
().y + e.getY();

setCoordinateX =
absoluteCoordinateXrelativeCoordinateXWhenMousePressed;

public void openFile(){

setCoordinateY =
absoluteCoordinateYrelativeCoordinateYWhenMousePressed;
magnifierPanel.setMagnifierLocation(setCo
ordinateX,setCoordinateY);
setLocation(setCoordinateX,setCoordinateY
);
}
}

ObjectInputStream in;
try{
in = new ObjectInputStream(new
FileInputStream("Magnifier.cfg"));
try{
configData =
(ConfigData)in.readObject();
setCoordinateX =
configData.magnifierXCoordinate;
setCoordinateY =
configData.magnifierYCoordinate;
magnifierSize =
configData.magnifierSizeInPixels;

updateSize(magnifierSize);
public void saveFile(){

ObjectOutputStream out;

catch(ClassNotFoundException e){}

configData.magnifierXCoordinate =
getLocation().x;

catch(IOException e){}
in.close();

configData.magnifierYCoordinate =
getLocation().y;

}
catch(IOException e){}

configData.magnifierSizeInPixels =
magnifierSize;
try{

}
}

out = new ObjectOutputStream(new


FileOutputStream("Magnifier.cfg"));
out.writeObject(configData);

class ConfigData implements Serializable{

out.flush();

public int magnifierXCoordinate;

out.close();

public int magnifierYCoordinate;

public int magnifierSizeInPixels;

You might also like