日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 鼠标拖动矩形_java – 用鼠标拖动创建矩形,而不是绘制

發(fā)布時間:2025/3/19 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 鼠标拖动矩形_java – 用鼠标拖动创建矩形,而不是绘制 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

nb-首先要注意的是,這是使用Java 7完成的,在Java 6中創(chuàng)建透明窗口的方式不同,在更新10之下是不可能的(我相信)

基本上,這會創(chuàng)建一個透明窗口,其大小和位置可以覆蓋整個虛擬屏幕(也就是說,如果您有多個屏幕,它將覆蓋所有虛擬屏幕).

然后我使用JPanel作為主要容器來捕獲鼠標事件并執(zhí)行繪制效果.

面板是透明的.這允許面板(和框架)下方的任何東西保持可見.然后我用透明的顏色畫了這個(我這樣做只是為了強調(diào)事實).

單擊并拖動某個區(qū)域時,它將被暴露.

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;

}

}

同樣,您可以創(chuàng)建一個用戶可以調(diào)整大小的透明框架.您將負責(zé)自己實施所有調(diào)整大小的代碼,但解決方案仍然是可行的.

更新

您可能還需要檢查操作系統(tǒng)/硬件是否可以支持透明度…

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");

}

更新了替代方法

這是解決問題的另一種方法.基本上它需要快速拍攝屏幕并將其渲染到窗口.這樣我們就可以根據(jù)需要控制突出顯示/選擇.

這樣做的缺點是它是一個靜態(tài)結(jié)果,你不會得到任何當前正在運行的動畫效果.

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;

}

}

總結(jié)

以上是生活随笔為你收集整理的java 鼠标拖动矩形_java – 用鼠标拖动创建矩形,而不是绘制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。