lotus_waterlily 2014-06-30 07:22
浏览 971

关于java截图画线的问题 求大神帮忙

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
* 用Java模拟出QQ桌面截图功能
* @author 五斗米 <如转载请保留作者和出处>
* @blog http://blog.csdn.net/mq612
*/
public class Test extends JFrame {
private static final long serialVersionUID = -267804510087895906L;
private JButton button = null;
private JLabel imgLabel = null;
public Test() {
button = new JButton("模拟屏幕(点右键退出)");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
new ScreenWindow(imgLabel);
} catch (Exception e1) {
JOptionPane.showConfirmDialog(null, "出现意外错误!", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
}
});
JPanel pane = new JPanel();
pane.setBackground(Color.WHITE);
imgLabel = new JLabel();
pane.add(imgLabel);
JScrollPane spane = new JScrollPane(pane);
this.getContentPane().add(button, BorderLayout.NORTH);//把按钮设置在平面区域的上方(北方)
this.getContentPane().add(spane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
class ScreenWindow extends JFrame {
private static final long serialVersionUID = -3758062802950480258L;
private boolean isDrag = false;
private int x = 0;
private int y = 0;
private int xEnd = 0;
private int yEnd = 0;

public ScreenWindow(final JLabel imgLabel) throws AWTException, InterruptedException {
Dimension screenDims = Toolkit.getDefaultToolkit().getScreenSize();
JLabel label = new JLabel(new ImageIcon(ScreenImage.getScreenImage(0, 0, screenDims.width, screenDims.height)));
label.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
dispose();
}
}
public void mousePressed(MouseEvent e) {
//System.out.println("鼠标按下了开始");
x = e.getX();
y = e.getY();

        }
        public void mouseReleased(MouseEvent e) {
            //System.out.println(isDrag+"鼠标释放了开始执行");
            if (isDrag) {
                xEnd = e.getX();
                yEnd = e.getY();
                if(x > xEnd){
                    int temp = x;
                    x = xEnd;
                    xEnd = temp;
                }
                if(y > yEnd){
                    int temp = y;
                    y = yEnd;
                    yEnd = temp;
                }
                try {
                    Graphics g = getGraphics();
                    g.setColor(Color.BLUE);
                    g.drawRect(x, y, xEnd-x, yEnd-y);
                    g.drawLine(xEnd,y,xEnd,yEnd);
                    g.drawLine(x,yEnd,xEnd,yEnd);
                    g.drawLine(x,y,xEnd,y);
                    g.drawLine(x,y,x,yEnd);
                    imgLabel.setIcon(new ImageIcon(ScreenImage.getScreenImage(x, y, xEnd - x, yEnd - y)));
                } catch (Exception ex) {
                    JOptionPane.showConfirmDialog(null, "出现意外错误!", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
                }
                dispose();
            }
        }
    });

    label.addMouseMotionListener(new MouseMotionListener() {

    public void mouseDragged(MouseEvent e) {
        //System.out.println("鼠标开始拖动了:"+isDrag);
        if(!isDrag)
            isDrag = true;
        xEnd=e.getX();
        yEnd=e.getY();
        //画线之前清理原来的线条
        Graphics g = getGraphics();
        g.setColor(Color.red);  


        //repaint();    

        //画线
        g.drawLine(xEnd,y,xEnd,yEnd);
        g.drawLine(x,yEnd,xEnd,yEnd);
        g.drawLine(x,y,xEnd,y);
        g.drawLine(x,y,x,yEnd);                 }

            });

this.setUndecorated(true);
this.getContentPane().add(label);
this.setSize(screenDims.width, screenDims.height);
this.setVisible(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
class ScreenImage {
public static Image getScreenImage(int x, int y, int w, int h) throws AWTException, InterruptedException {
Robot robot = new Robot();
Image screen = robot.createScreenCapture(new Rectangle(x, y, w, h)).getScaledInstance(w, h, Image.SCALE_SMOOTH);
MediaTracker tracker = new MediaTracker(new Label());
tracker.addImage(screen, 1);
tracker.waitForID(0);
return screen;
}
}

本人是个刚入行的初级java程序员,自己在网上找了个模拟qq截图的练习,代码如上,可是当实现截图的轮廓线时,mouseDragged()方法中的画线多次执行,导致截图出现很多无用的线条,用了repaint()也不好使。各位大神,我刚入行,不要取笑我。谢谢各位了!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算