Magnifier Tool

Started by
2 comments, last by PsychotikRabbit 11 years, 4 months ago
Hi, I did make some simple magnifier tool - a rectangle screen that zooms the content in it. But I got stuck at one place. I made it to capture the screen only once and added an option to refresh (recapture the screen). What I want to do is I want it to capture the screen constantly (say every 0.5 seconds), like the Windows Magnifier Tool. I guess what I have to do but I am not sure how to implement it.
That's my code so far:
http://pastebin.com/2qnBh23g
http://pastebin.com/qE5g7E24

I added a Timer to capture the screen every second:

[source lang="java"]time = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
magnifierPanel.getScreen();
}
});
time.start();[/source]
The problem here is that each time it captures the screen , it also captures the magnifier itself and it's kinda broken way to do it.

After that I tried:
[source lang="java"]time = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
magnifierPanel.getScreen();
setVisible(true);
}
});
time.start();[/source]
But in this case , the whole magnifier area (the rectangle screen in my case) is blinking, because it constantly swaps between setVisible - false and true.

I will be glad if you help me to find a solution ! Thanks smile.png .
Advertisement
From the looks of it, and from a Google search, it seems that createScreenCapture simply takes too long for what you are trying to do. I don't think Java has any built in methods that are fast enough for what you are trying to achieve. Maybe look for some sort of native library for the os you are running it on? Also I changed setVisible to setOpacity since it doesn't make the taskbar icon blink constantly.

--Vince
My OS is Windows 7 64 bit, setOpacity doesn't seem to work fine for me. I'll be glad if you have any other ideas and if someone know specific library for doing that job. I did find JxCaputre library but I don't know if it will be of any use.
Instead of creating a screenshot with a timer can't you just use the original image and scale it when its painted ?

I did not write this code, it seems to do what you need to do by scaling the image with AffineTransform on its paint. Of course you would adjust the scaling
depending on the zoom of your magnifier.


protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int w = getWidth();
int h = getHeight();
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
double x = (w - scale * imageWidth)/2;
double y = (h - scale * imageHeight)/2;
AffineTransform at = AffineTransform.getTranslateInstance(x,y);
at.scale(scale, scale);
g2.drawRenderedImage(image, at);
}


With this it won't flicker anymore because you won't need to use setVisible and to take a bunch of screenshots every second.

This topic is closed to new replies.

Advertisement