创建鼠标拖动矩形,不画 [英] Create rectangle with mouse drag, not draw
问题描述
我想创建一个使用整个屏幕的矩形。通过使用整个屏幕,我的意思是这样的:
I want create a rectangle using the whole screen. By using the whole screen, I mean something like this:
要启动,是即使在Java中尽可能使用整个屏幕这样?第二,我怎么会去这样做呢?另一件事,我不想画一个矩形的实际,我想创建上,作为一个新的 java.awt.Rectangle中的
。
To start, is that even possible in Java, using the whole screen like that? Second, how would I go about doing it? Another thing, I do not want to draw an actual rectangle, I want to create on, as in a new java.awt.Rectangle
.
推荐答案
的 NB-首先要注意,这是用Java做7,创建Java 6中的透明窗口,不同的完成,是不可能低于更新10(我相信)的
基本上,这将创建一个透明窗口,大小和位置,以覆盖整个虚拟屏幕(也就是说,如果你有多个屏幕,这将覆盖所有的)。
Basically, this creates a transparent window, sized and positioned to cover the entire virtual screen (that is, if you have multiple screens, it will cover all of them).
然后我用的JPanel
作为主容器来捕捉鼠标事件并进行油漆效果。
I then use a JPanel
as the primary container to capture mouse events and perform paint effects.
的面板是由透明的。这让什么都低于面板(和框架),以保持可见。然后,我已经在此画用透明色(我这样做只是为了强调这一事实)。
The panel is made transparent. This allows what ever is below the panel (and the frame) to remain visible. I've then over painted this with a transparent color (I did this just to highlight the fact).
当您单击并拖动一个区域,它被暴露出来。
When you click and drag an area, it is exposed.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MySnipTool {
public static void main(String[] args) {
new MySnipTool();
}
public MySnipTool() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new CapturePane());
Rectangle bounds = getVirtualBounds();
frame.setLocation(bounds.getLocation());
frame.setSize(bounds.getSize());
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
});
}
public class CapturePane extends JPanel {
private Rectangle selectionBounds;
private Point clickPoint;
public CapturePane() {
setOpaque(false);
MouseAdapter mouseHandler = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
System.exit(0);
}
}
@Override
public void mousePressed(MouseEvent e) {
clickPoint = e.getPoint();
selectionBounds = null;
}
@Override
public void mouseReleased(MouseEvent e) {
clickPoint = null;
}
@Override
public void mouseDragged(MouseEvent e) {
Point dragPoint = e.getPoint();
int x = Math.min(clickPoint.x, dragPoint.x);
int y = Math.min(clickPoint.y, dragPoint.y);
int width = Math.max(clickPoint.x - dragPoint.x, dragPoint.x - clickPoint.x);
int height = Math.max(clickPoint.y - dragPoint.y, dragPoint.y - clickPoint.y);
selectionBounds = new Rectangle(x, y, width, height);
repaint();
}
};
addMouseListener(mouseHandler);
addMouseMotionListener(mouseHandler);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(255, 255, 255, 128));
Area fill = new Area(new Rectangle(new Point(0, 0), getSize()));
if (selectionBounds != null) {
fill.subtract(new Area(selectionBounds));
}
g2d.fill(fill);
if (selectionBounds != null) {
g2d.setColor(Color.BLACK);
g2d.draw(selectionBounds);
}
g2d.dispose();
}
}
public static Rectangle getVirtualBounds() {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
for (GraphicsDevice gd : lstGDs) {
bounds.add(gd.getDefaultConfiguration().getBounds());
}
return bounds;
}
}
同样,你可以只创建一个透明的框架,用户可以调整。您将负责执行所有的大小调整code自己,但解决方案仍是一个可行的。
Equally, you could just create a transparent frame that the user can resize. You will be responsible for implementing all the resize code yourself, but the solution would still be a viable one.
更新
您可能还需要检查,看看是否在OS /硬件可以支持transparancy ...
You may also need to check to see if the OS/hardware can support transparancy...
GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
if (!AWTUtilities.isTranslucencyCapable(config)) {
System.out.println("Transluceny is not supported");
}
if (!AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSPARENT)) {
System.out.println("PerPeixel Transparency is not supported");
}
更新了另一种方法
这是一种替代方法来解决问题。基本上,它需要在屏幕的快照,并将其呈现到窗口。这就使我们能够控制高亮/选择,因为我们需要的。
This is an alternative approach to the problem. Basically it takes a snap shot of the screen and renders it to the window. This then allows us to control the highlighting/selection as we need.
这样做的缺点是,它是一个静态的结果,你不会得到任何当前正在运行的动画效果。
The drawback to this is that it is a static result, you won't get any currently running animation effects.
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SnipWithScreenShoot {
public static void main(String[] args) {
new SnipWithScreenShoot();
}
public SnipWithScreenShoot() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
try {
JFrame frame = new JFrame("Test");
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (AWTException exp) {
exp.printStackTrace();
System.out.println("That sucks");
}
}
});
}
public class TestPane extends JPanel {
private BufferedImage image;
private Rectangle selection;
public TestPane() throws AWTException {
Robot bot = new Robot();
image = bot.createScreenCapture(getVirtualBounds());
MouseAdapter handler = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
selection = new Rectangle(e.getPoint());
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
int width = Math.max(selection.x - e.getX(), e.getX() - selection.x);
int height = Math.max(selection.y - e.getY(), e.getY() - selection.y);
selection.setSize(width, height);
repaint();
}
};
addMouseListener(handler);
addMouseMotionListener(handler);
}
@Override
public Dimension getPreferredSize() {
return image == null ? super.getPreferredSize() : new Dimension(image.getWidth(), image.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(image, WIDTH, 0, this);
if (selection != null) {
g2d.setColor(new Color(225, 225, 255, 128));
g2d.fill(selection);
g2d.setColor(Color.GRAY);
g2d.draw(selection);
}
g2d.dispose();
}
}
}
public static Rectangle getVirtualBounds() {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
for (GraphicsDevice gd : lstGDs) {
bounds.add(gd.getDefaultConfiguration().getBounds());
}
return bounds;
}
}
这篇关于创建鼠标拖动矩形,不画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!