在桌面上画线,并单击周围的线 [英] Draw line on the desktop with clickable around line

查看:73
本文介绍了在桌面上画线,并单击周围的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个在桌面上使用此属性的程序,用户可以单击该行附近的桌面图标。
我创建示例。我创建透明框架并在其上绘制jWindow。在MouseReleased事件中处置主框架,然后保留所有创建的jwindows。我的代码创建了许多jwindow,这非常糟糕。对于30cm的绘制线程序,请创建超过400个jwindow,这将导致os非常沉重。
可以帮助我吗?
(对不起,我的英语不好)

I want create a program that draw line on desktop with this property that user can click on desktop icons near the line. I create sample. I create transparent frame and draw jWindow on this. in MouseReleased event dispose main frame then stay all jwindows that created. My code create many number of jwindow and this is very bad. For draw line 30cm program create over than 400 jwindow and this causes os be very heavy. Can help me anybody? (Excuse me for my ugly english)

    package PKHMain;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;

import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class FRMMain extends JFrame implements MouseListener, MouseMotionListener {

    public FRMMain() {
        this.setUndecorated(true);
        this.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLayout(null);
        this.setBackground(new Color(0, 0, 0, 0));
        this.setVisible(true);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public static void main(String[] args) {
        new FRMMain();
    }

    @Override
    public void mousePressed(MouseEvent event) {
    }

    @Override
    public void paint(Graphics g) {
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent event) {
    }

    @Override
    public void mouseEntered(MouseEvent event) {
    }

    @Override
    public void mouseExited(MouseEvent event) {
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        this.dispose();
    }

    @Override
    public void mouseDragged(MouseEvent event) {
        int x = event.getX();
        int y = event.getY();
        JWindow frame = new JWindow();
        frame.setBackground(new Color(0, 0, 0, 0));
        frame.setContentPane(new ShapedPane(x, y));
        frame.pack();
        frame.setLocation(x, y);
        frame.setAlwaysOnTop(true);
        frame.setVisible(true);
    }

    public void mouseMoved(MouseEvent event) {
    }

    public class ShapedPane extends JPanel {

        public int x1;
        public int y1;

        public ShapedPane(int x, int y) {
            setOpaque(false);
            setLayout(new GridBagLayout());
            x1 = x;
            y1 = y;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(5, 5);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHints(hints);
            g2.setColor(Color.red);
            g2.fill(new Ellipse2D.Double(0, 0, getWidth(), getHeight()));
            g2.dispose();
        }
    }
}


推荐答案

您正在使用程序触及系统特定的行为,例如在我的计算机上,该程序根本不会收到任何鼠标事件,因为背景色的alpha值为零。将其设置为至少一个使其接收点击和拖动。因此,这是控制所需点击行为的一种方法,但可能不适用于您。

You are touching system specific behavior with your program, e.g. on my machine the program will not receive any mouse event at all as the background color has an alpha value of zero. Setting it to at least one makes it receiving clicks and drags. So this is a way to control the desired click-through behavior but it might be the case that it doesn’t work for you.

以下是该程序的工作原理在我的机器上(Java 7和Windows 7):

Here is the program as it work on my machine (Java 7 and Windows 7):

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

public class FRMMain extends JFrame {
  private final List<Shape> list=new ArrayList<>();
  private boolean paintPhase=true;

  public FRMMain() {
      this.setUndecorated(true);
      final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      this.setSize(screenSize.width, screenSize.height);
      this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      this.setBackground(new Color(0, 0, 0, 1));
      this.setOpacity(1f);
      this.setAlwaysOnTop(true);
      this.setVisible(true);
      enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK);
  }
  @Override
  protected void processMouseEvent(MouseEvent e) {
    if(paintPhase && e.getID()==MouseEvent.MOUSE_RELEASED) {
      paintPhase = false;
      // on my machine the following line is enough to enable click-through
      setBackground(new Color(0, 0, 0, 0));
      // but if this doesn’t work, the following should do:
      Area area=new Area();
      BasicStroke b=new BasicStroke(2f);
      for(Shape s:list) area.add(new Area(b.createStrokedShape(s)));
      setShape(area);
    }
    super.processMouseEvent(e);
  }
  @Override
  protected void processMouseMotionEvent(MouseEvent event)
  {
    if(paintPhase && event.getID()==MouseEvent.MOUSE_DRAGGED) {
      int x = event.getX();
      int y = event.getY();
      list.add(new Ellipse2D.Float(x, y, 8, 8));
      repaint();
    }
    super.processMouseMotionEvent(event);
  }
  @Override
  public boolean contains(int x, int y) {
    return paintPhase;
  }

  public static void main(String[] args) {
      new FRMMain();
  }

  @Override
  public void paint(Graphics g) {
    Graphics2D gfx=(Graphics2D)g;
    gfx.setColor(Color.RED);
    for(Shape s:list) gfx.draw(s);
  }
}

这篇关于在桌面上画线,并单击周围的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆