写的是放大镜程序 问题是运行后出来放大镜后 只能放大桌面上的东西 ,但是在桌面上在打开一个网页或文件 在去用放大镜去放大发现 却是原来桌面的东西 求帮助!!! 贴代码!
package cc;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;import javax.swing.*;
public class Magnifier extends JFrame
{
/**
* 主容器
*/
private Container container = getContentPane();
/**
* 放大镜x坐标
* 计算方式:setCoordinateX = absoluteCoordinateX
* - relativeCoordinateXWhenMousePressed
*/
private int setCoordinateX; /**
* 放大镜y坐标
* 计算方式:setCoordinateY = absoluteCoordinateY
* - relativeCoordinateYWhenMousePressed
*/
private int setCoordinateY; /**
* 鼠标绝对x坐标
*/
private int absoluteCoordinateX; /**
* 鼠标绝对y坐标
*/
private int absoluteCoordinateY; /**
* 鼠标按下时的相对x坐标
*/
private int relativeCoordinateXWhenMousePressed; /**
* 鼠标按下时的相对y坐标
*/
private int relativeCoordinateYWhenMousePressed; /**
* 标记鼠标是否按下。如果按下则为true,否则为false
*/
private boolean mousePressedNow; /**
* 放大镜尺寸
*/
private int magnifierSize = 100; /**
* 放大镜内容面板
*/
private MagnifierPanel magnifierPanel = new MagnifierPanel(magnifierSize); /**
* 构造函数,创建一个放大镜窗体
*/
public Magnifier()
{
setUndecorated(true); // 窗体边缘
setResizable(false);
container.add(magnifierPanel);
addMouseListener(new MouseFunctions());
addMouseMotionListener(new MouseMotionFunctions());
updateSize(magnifierSize);
this.setVisible(true);
} /**
* 程序入口点
*
* @param arg
* 启动参数,这里为空
*/
public static void main(String arg[])
{
Magnifier magnifier = new Magnifier();
magnifier.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} /**
* 更新窗体
*
* @param magnifierSize
* 放大镜尺寸
*/
public void updateSize(int magnifierSize)
{
magnifierPanel.setMagnifierSize(magnifierSize + 100);
setSize(magnifierSize + 100, magnifierSize + 100);
validate(); // 更新所有子控件
} private class MouseFunctions extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if (e.getClickCount() == 1)
{// 如果鼠标左键点了一下,说明按住了窗体
mousePressedNow = true;
relativeCoordinateXWhenMousePressed = e.getX();
relativeCoordinateYWhenMousePressed = e.getY();
}
} public void mouseReleased(MouseEvent e)
{
mousePressedNow = false;
}
} private class MouseMotionFunctions extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
if (mousePressedNow == true)
{// 如果此时鼠标按下了,说明在拖拽窗体
absoluteCoordinateX = Magnifier.this
.getLocationOnScreen().x
+ e.getX();
absoluteCoordinateY = Magnifier.this
.getLocationOnScreen().y
+ e.getY();
setCoordinateX = absoluteCoordinateX
- relativeCoordinateXWhenMousePressed;
setCoordinateY = absoluteCoordinateY
- relativeCoordinateYWhenMousePressed;
magnifierPanel.setMagnifierLocation(setCoordinateX,
setCoordinateY);
setLocation(setCoordinateX, setCoordinateY);
}
}
}
}class MagnifierPanel extends JPanel
{
private Image screenImage; /**
* 放大镜的尺寸
*/
private int magnifierSize; private int locationX; private int locationY; private Robot robot; /**
* 带参数的构造函数
* @param magnifierSize
* 放大尺寸
*/
public MagnifierPanel(int magnifierSize)
{
try
{
robot = new Robot(); }
catch (AWTException e)
{
}
// 截屏幕
screenImage = robot.createScreenCapture(new Rectangle(0, 0, Toolkit
.getDefaultToolkit().getScreenSize().width, Toolkit
.getDefaultToolkit().getScreenSize().height));
this.magnifierSize = magnifierSize;
screenImage.flush();
} /**
* 设置放大镜的位置
* @param locationX
* x坐标
* @param locationY
* y坐标
*/
public void setMagnifierLocation(int locationX, int locationY)
{
this.locationX = locationX;
this.locationY = locationY;
repaint(); // 注意重画控件
} /**
* 设置放大镜的尺寸
* @param magnifierSize
* 放大镜尺寸
*/
public void setMagnifierSize(int magnifierSize)
{
this.magnifierSize = magnifierSize;
} public void paintComponent(Graphics g)
{
super.paintComponent((Graphics2D) g);
// 关键处理代码
g.drawImage(
screenImage, // 要画的图片
0, // 目标矩形的第一个角的x坐标
0, // 目标矩形的第一个角的y坐标
magnifierSize, // 目标矩形的第二个角的x坐标
magnifierSize, // 目标矩形的第二个角的y坐标
locationX + (magnifierSize / 4), // 源矩形的第一个角的x坐标
locationY + (magnifierSize / 4), // 源矩形的第一个角的y坐标
locationX + (magnifierSize / 4 * 3), // 源矩形的第二个角的x坐标
locationY + (magnifierSize / 4 * 3), // 源矩形的第二个角的y坐标
this
);
}
}
package cc;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;import javax.swing.*;
public class Magnifier extends JFrame
{
/**
* 主容器
*/
private Container container = getContentPane();
/**
* 放大镜x坐标
* 计算方式:setCoordinateX = absoluteCoordinateX
* - relativeCoordinateXWhenMousePressed
*/
private int setCoordinateX; /**
* 放大镜y坐标
* 计算方式:setCoordinateY = absoluteCoordinateY
* - relativeCoordinateYWhenMousePressed
*/
private int setCoordinateY; /**
* 鼠标绝对x坐标
*/
private int absoluteCoordinateX; /**
* 鼠标绝对y坐标
*/
private int absoluteCoordinateY; /**
* 鼠标按下时的相对x坐标
*/
private int relativeCoordinateXWhenMousePressed; /**
* 鼠标按下时的相对y坐标
*/
private int relativeCoordinateYWhenMousePressed; /**
* 标记鼠标是否按下。如果按下则为true,否则为false
*/
private boolean mousePressedNow; /**
* 放大镜尺寸
*/
private int magnifierSize = 100; /**
* 放大镜内容面板
*/
private MagnifierPanel magnifierPanel = new MagnifierPanel(magnifierSize); /**
* 构造函数,创建一个放大镜窗体
*/
public Magnifier()
{
setUndecorated(true); // 窗体边缘
setResizable(false);
container.add(magnifierPanel);
addMouseListener(new MouseFunctions());
addMouseMotionListener(new MouseMotionFunctions());
updateSize(magnifierSize);
this.setVisible(true);
} /**
* 程序入口点
*
* @param arg
* 启动参数,这里为空
*/
public static void main(String arg[])
{
Magnifier magnifier = new Magnifier();
magnifier.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} /**
* 更新窗体
*
* @param magnifierSize
* 放大镜尺寸
*/
public void updateSize(int magnifierSize)
{
magnifierPanel.setMagnifierSize(magnifierSize + 100);
setSize(magnifierSize + 100, magnifierSize + 100);
validate(); // 更新所有子控件
} private class MouseFunctions extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if (e.getClickCount() == 1)
{// 如果鼠标左键点了一下,说明按住了窗体
mousePressedNow = true;
relativeCoordinateXWhenMousePressed = e.getX();
relativeCoordinateYWhenMousePressed = e.getY();
}
} public void mouseReleased(MouseEvent e)
{
mousePressedNow = false;
}
} private class MouseMotionFunctions extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
if (mousePressedNow == true)
{// 如果此时鼠标按下了,说明在拖拽窗体
absoluteCoordinateX = Magnifier.this
.getLocationOnScreen().x
+ e.getX();
absoluteCoordinateY = Magnifier.this
.getLocationOnScreen().y
+ e.getY();
setCoordinateX = absoluteCoordinateX
- relativeCoordinateXWhenMousePressed;
setCoordinateY = absoluteCoordinateY
- relativeCoordinateYWhenMousePressed;
magnifierPanel.setMagnifierLocation(setCoordinateX,
setCoordinateY);
setLocation(setCoordinateX, setCoordinateY);
}
}
}
}class MagnifierPanel extends JPanel
{
private Image screenImage; /**
* 放大镜的尺寸
*/
private int magnifierSize; private int locationX; private int locationY; private Robot robot; /**
* 带参数的构造函数
* @param magnifierSize
* 放大尺寸
*/
public MagnifierPanel(int magnifierSize)
{
try
{
robot = new Robot(); }
catch (AWTException e)
{
}
// 截屏幕
screenImage = robot.createScreenCapture(new Rectangle(0, 0, Toolkit
.getDefaultToolkit().getScreenSize().width, Toolkit
.getDefaultToolkit().getScreenSize().height));
this.magnifierSize = magnifierSize;
screenImage.flush();
} /**
* 设置放大镜的位置
* @param locationX
* x坐标
* @param locationY
* y坐标
*/
public void setMagnifierLocation(int locationX, int locationY)
{
this.locationX = locationX;
this.locationY = locationY;
repaint(); // 注意重画控件
} /**
* 设置放大镜的尺寸
* @param magnifierSize
* 放大镜尺寸
*/
public void setMagnifierSize(int magnifierSize)
{
this.magnifierSize = magnifierSize;
} public void paintComponent(Graphics g)
{
super.paintComponent((Graphics2D) g);
// 关键处理代码
g.drawImage(
screenImage, // 要画的图片
0, // 目标矩形的第一个角的x坐标
0, // 目标矩形的第一个角的y坐标
magnifierSize, // 目标矩形的第二个角的x坐标
magnifierSize, // 目标矩形的第二个角的y坐标
locationX + (magnifierSize / 4), // 源矩形的第一个角的x坐标
locationY + (magnifierSize / 4), // 源矩形的第一个角的y坐标
locationX + (magnifierSize / 4 * 3), // 源矩形的第二个角的x坐标
locationY + (magnifierSize / 4 * 3), // 源矩形的第二个角的y坐标
this
);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货