簡體   English   中英

Java-自定義形狀面板?

[英]Java - Custom Shape Panels?

我正在開發一個涉及用戶要求將鼠標懸停在屏幕上幾個移動點上以啟動特定彈出窗口的應用程序。 此刻,我正在監聽JPanel上渲染點的mouseMoved事件,然后每當光標在點的特定距離內時,啟動所需的彈出窗口。

當我有數百個點時-這可能變得相當昂貴。

理想的解決方案是將我的“點”表示為小組件,並為每個點注冊一個鼠標偵聽器嗎?

有誰知道我怎么用JComponent表示一個小橢圓?

非常感謝

這是一些舊代碼,顯示了如何創建“圓形” JButton。 我將擴展JComponent,並且覆蓋的重要方法是paintComponent()和contains():

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class RoundButton extends JButton {
    public RoundButton(String label) {
        super(label);

        // These statements enlarge the button so that it
        // becomes a circle rather than an oval.
        Dimension size = getPreferredSize();
        size.width = size.height = Math.max(size.width, size.height);
        setPreferredSize(size);

        // This call causes the JButton not to paint the background.
        // This allows us to paint a round background.
        setContentAreaFilled(false);
    }

    // Paint the round background and label.
    protected void paintComponent(Graphics g) {
    if (getModel().isArmed()) {
            // You might want to make the highlight color
            // a property of the RoundButton class.
            g.setColor(Color.lightGray);
        } else {
            g.setColor(getBackground());
        }
    g.fillOval(0, 0, getSize().width-1, getSize().height-1);

        // This call will paint the label and the focus rectangle.
    super.paintComponent(g);
    }

    // Paint the border of the button using a simple stroke.
    protected void paintBorder(Graphics g) {
        g.setColor(getForeground());
        g.drawOval(0, 0, getSize().width-1, getSize().height-1);
    }

    // Hit detection.
    Shape shape;
    public boolean contains(int x, int y) {
        // If the button has changed size, make a new shape object.
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
        }
        return shape.contains(x, y);
    }

    // Test routine.
    public static void main(String[] args) {
        // Create a button with the label "Jackpot".
        JButton button = new RoundButton("Jackpot");
        button.setBackground(Color.green);
        button.setBounds(0, 0, 100, 100);

        JButton button2 = new RoundButton("Jackpot2");
        button2.setBackground(Color.red);
        button2.setBounds(50, 50, 100, 100);

        // Create a frame in which to show the button.
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(Color.yellow);
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(button);
        frame.getContentPane().add(button2);
//        frame.getContentPane().setLayout(new FlowLayout());
        frame.setSize(200, 200);
        frame.setVisible(true);

        MouseListener mouseListener = new MouseAdapter() {
            public void mouseEntered( MouseEvent e )
            {}

            public void mouseExited( MouseEvent e )
            {}

            public void mouseClicked( MouseEvent e )
            {
                System.out.println( "clicked " );
            }

            public void mousePressed( MouseEvent e )
            {
                System.out.println( "pressed " );
            }

            public void mouseReleased( MouseEvent e )
            {
                System.out.println( "released " );
            }
        };
        button.addMouseListener( mouseListener );

    }
}

由於沒有自定義代碼,因此可以輕松地簡化點擊檢測。 另外,由於可以控制每個組件的Z順序,因此它可以輕松控制組件的重疊。

您寫下“這可能會變得很昂貴”

無論您是自己編寫“ isMouseCloseToDot”方法的代碼,還是使用Swing內置的工具,在兩種情況下,仍需要計算機執行工作才能確定是否激活了一個點。

我建議您堅持使用當前的方法,除非您確定這種方法確實太昂貴了。 用幾百個點做一個小測試。 響應時間可以接受嗎?

不確定哪種方法更昂貴,但是基於組件的方法肯定更容易實現。

您要做的就是創建自己的組件,覆蓋paintcontains方法。 將其添加到容器中的特定位置(根據需要使用絕對布局或任何其他布局)。 您的偵聽器將成為提供組件行為的新組件的一部分。

從設計和程序組織的角度來看,這是一種更為優越的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM