使用Java为视障人士提供屏幕放大功能

使用Java为视障人士提供屏幕放大功能,java,swing,jpanel,awt,zooming,Java,Swing,Jpanel,Awt,Zooming,有没有办法创建一个功能,允许用户切换屏幕放大率,如果他们是视力受损 我是在一个像eclipse这样的程序中问这个问题的,在这个程序中,视力受损的用户可以打开和关闭一个放大文本、图标和导航栏的功能 在裁剪图像时,我将此简短代码用作通用放大镜 import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; class ZoomOnMouse { R

有没有办法创建一个功能,允许用户切换屏幕放大率,如果他们是视力受损


我是在一个像eclipse这样的程序中问这个问题的,在这个程序中,视力受损的用户可以打开和关闭一个放大文本、图标和导航栏的功能

在裁剪图像时,我将此简短代码用作通用放大镜

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

class ZoomOnMouse {

    Robot robot;
    int zoomFactor = 2;
    PointerInfo pi;
    JPanel gui;
    JLabel output;
    Timer t;

    public ZoomOnMouse() throws AWTException {
        robot = new Robot();
        gui = new JPanel(new BorderLayout(2, 2));
        output = new JLabel("Point at something to see it zoomed!");
        gui.add(output, BorderLayout.PAGE_END);
        final int size = 256;
        final BufferedImage bi = new BufferedImage(
                size, size, BufferedImage.TYPE_INT_RGB);
        final JLabel zoomLabel = new JLabel(new ImageIcon(bi));
        gui.add(zoomLabel, BorderLayout.CENTER);

        MouseListener factorListener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (zoomFactor == 2) {
                    zoomFactor = 4;
                } else if (zoomFactor == 4) {
                    zoomFactor = 8;
                } else if (zoomFactor == 8) {
                    zoomFactor = 2;
                }
                showInfo();
            }
        };
        zoomLabel.addMouseListener(factorListener);

        ActionListener zoomListener = (ActionEvent e) -> {
            pi = MouseInfo.getPointerInfo();
            Point p = pi.getLocation();
            Rectangle r = new Rectangle(
                    p.x - (size / (2 * zoomFactor)),
                    p.y - (size / (2 * zoomFactor)),
                    (size / zoomFactor),
                    (size / zoomFactor));
            BufferedImage temp = robot.createScreenCapture(r);
            Graphics g = bi.getGraphics();
            g.drawImage(temp, 0, 0, size, size, null);
            g.setColor(new Color(255,0,0,128));
            int x = (size/2)-1;
            int y = (size/2)-1;
            g.drawLine(0,y,size,y);
            g.drawLine(x,0,x,size);
            g.dispose();
            zoomLabel.repaint();
            showInfo();
        };
        t = new Timer(40, zoomListener);
        t.start();
    }

    public void stop() {
        t.stop();
    }

    public Component getGui() {
        return gui;
    }

    public void showInfo() {
        pi = MouseInfo.getPointerInfo();
        output.setText("Zoom: " + zoomFactor + "  Point: " + pi.getLocation());
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                final ZoomOnMouse zm = new ZoomOnMouse();
                final JFrame f = new JFrame("Mouse Zoom");
                f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                f.add(zm.getGui());
                f.setResizable(false);
                f.pack();
                f.setLocationByPlatform(true);
                f.setAlwaysOnTop(true);
                f.setVisible(true);

                WindowListener closeListener = new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent e) {
                        zm.stop();
                        f.dispose();
                    }
                };
                f.addWindowListener(closeListener);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

阅读此代码时,我是否认为这会放大鼠标单击的特定区域,有点像缩放框?如何将其约束为仅应用程序而不是整个屏幕?@user6020197您知道如何获取顶级容器的边界吗?你知道如何使用条件语句吗?我不知道如何获得顶级容器的边界,但我对条件语句非常满意。第四点:当有人提到Java的一部分时,搜索文档!例如,
getBound
在任何扩展组件的类中都会找到它(而且它的速度比等待我从我离开计算机时正在做的任何事情中返回要快得多)。