首页 > 解决方案 > 将Java中的放大工具限制在应用程序窗口中

问题描述

我们正在尝试修改现有的 Java 程序以适合视障人士,我们正在进行的更改之一是放大功能。

我们有用于放大鼠标下方图像的代码,但它会影响整个屏幕,我们希望将其限制为将运行它的 Java 程序。例如,用户单击辅助功能菜单,然后放大,鼠标下方但仅在应用程序窗口中的所有内容都被放大,而不是整个屏幕上的所有内容。

import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 
class magnify extends JFrame { 

// object 
static magnify m; 

// image 
Image i; 

// default constrcutor 
magnify() 
{ 
    // create a frame 
    super("magnify"); 

    // set size of frame 
    setSize(200, 220); 
    show(); 

    // function to magnify the image 
    work(); 
} 

// main function 
public static void main(String args[]) 
{ 

    // object of class 
    m = new magnify(); 
} 

public void work() 
{ 
    try { 
        // create a robot 
        Robot r = new Robot(); 

        // while the frame is visible 
        while (isVisible()) { 
            // get the position of mouse 
            Point p = MouseInfo.getPointerInfo().getLocation(); 

            // create a screen capture around the mouse pointer 
            i = r.createScreenCapture(new Rectangle((int)p.getX() - 30, 
                                                    (int)p.getY() - 30, 150, 150)); 

            // repaint the conatiner 
            repaint(); 
        } 
        // exit the program 
        System.exit(0); 
    } 
    catch (Exception e) { 
        System.err.println(e.getMessage()); 
    } 
} 

// paint function 
public void paint(Graphics g) 
{ 

    // draw the image 
    g.drawImage(i, 0, 0, 300, 300, this); 
} 
} 

这是我们目前在另一篇在线帖子中使用的代码。应用程序窗口可以调整大小以适应屏幕,就像原生应用程序一样。

谢谢你的帮助!

标签: javaeclipseswingoop

解决方案


推荐阅读