日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java – 在當(dāng)前鼠標(biāo)位置檢測(cè)圖像僅適用于圖像的一部分

 印度阿三17 2019-08-30

我有一個(gè)JTextPane,我添加了文本,而一些文本有一個(gè)通過(guò)StyleConstants.setIcon()設(shè)置的圖像.我還向JTextPane添加了一個(gè)鼠標(biāo)監(jiān)聽(tīng)器,以檢測(cè)鼠標(biāo)在圖像上單擊/懸停的時(shí)間,但它只在圖像的左側(cè)部分檢測(cè)到它.難道我做錯(cuò)了什么?

SSCCE(將鼠標(biāo)懸停在圖像上會(huì)更改鼠標(biāo)光標(biāo)以指示何時(shí)檢測(cè)到圖像):

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * SSCCE to show how detecting an image under the current mouse position only
 * works on part of the image. It adds a simple image to the document of the
 * JTextPane and changes the mouse cursor when it detects the mouse hovering
 * over the image.
 */
public class JTextPaneImage {

    private static void createWindow() {

        // Create window
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create JTextPane and add to window
        final JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseMoved(MouseEvent e) {
                AttributeSet style = getAttributes(e);
                if (style != null && StyleConstants.getIcon(style) != null) {
                    textPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                } else {
                    textPane.setCursor(Cursor.getDefaultCursor());
                }
            }
        });
        frame.add(new JScrollPane(textPane));

        try {
            StyledDocument doc = (StyledDocument)textPane.getDocument();

            // Add some text
            doc.insertString(0, "Some text ", null);

            // Add the image
            SimpleAttributeSet style = new SimpleAttributeSet();
            StyleConstants.setIcon(style, createImage());
            doc.insertString(doc.getLength(), "test", style);
        } catch (BadLocationException ex) {
            Logger.getLogger(JTextPaneImage.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Display everything
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    /**
     * Retrieves the style of where the mouse is positioned (assuming this is
     * a JTextPane).
     * 
     * @param e The mouse event containing the mouse position
     * @return The AttributeSet or null if none could be found
     */
    private static AttributeSet getAttributes(MouseEvent e) {
        JTextPane text = (JTextPane)e.getSource();
        Point mouseLocation = new Point(e.getX(), e.getY());
        int pos = text.viewToModel(mouseLocation);

        if (pos >= 0) {
            StyledDocument doc = text.getStyledDocument();
            Element element = doc.getCharacterElement(pos);
            return element.getAttributes();
        }
        return null;
    }

    /**
     * Creates a single 28x28 image filled with a single color.
     * 
     * @return The created ImageIcon
     */
    public static ImageIcon createImage() {
        BufferedImage image = new BufferedImage(28,28, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 28, 28);
        g.dispose();
        return new ImageIcon(image);
    }

    public static final void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createWindow();
            }
        });
    }
}

解決方法:

您使用強(qiáng)text.viewToModel(mouseLocation)來(lái)檢測(cè)偏移量,然后從獲得的偏移量中檢索樣式.

邏輯是返回更接近鼠標(biāo)位置的偏移量.因此,當(dāng)您單擊視圖的右半部分時(shí),將返回下一個(gè)偏移(視圖后的偏移).您可以嘗試相同的設(shè)置一個(gè)大字母(例如m大字體).當(dāng)你clikc接近字母結(jié)束時(shí),插入符號(hào)設(shè)置在字母后面.這里的邏輯是一樣的.

所以你得到了圖像之后的位置并從位置獲取樣式但是在圖像視圖文本元素之后沒(méi)有屬性中的圖標(biāo)而你沒(méi)有圖像.

更新:

為了提供正確的行為,我建議使用modelToView()并傳遞獲得的偏移量.從矩形中你可以弄清楚你的X位置是否已經(jīng)過(guò)時(shí)了.矩形的X.如果插入矩形的x大于鼠標(biāo)X,則可以嘗試前一個(gè)偏移.

UPDATE2:您可以覆蓋IconView并使用paint()方法存儲(chǔ)圖像視圖的最后一個(gè)繪制矩形.存儲(chǔ)在最后繪制的地圖中.在鼠標(biāo)移動(dòng)/單擊時(shí),檢查地圖以查找其中一個(gè)矩形是否包含該點(diǎn).

要么

你可以使用View的方法getChildAllocation()類(lèi)似的東西被描述為here來(lái)計(jì)算圖像邊界.從根視圖開(kāi)始,然后向下直到葉子計(jì)算X,Y的正確視圖.如果葉子視圖是IconView,則你的圖像已經(jīng)過(guò)了.

來(lái)源:https://www./content-1-426601.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多