作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
是否有任何方法可以创建一项功能,让视障用户可以切换屏幕放大倍率。
我是在像 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);
}
}
关于java - 使用 Java 为视障人士放大屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35970371/
我是一名优秀的程序员,十分优秀!