问题描述
我正在开发一个应用程序,该应用程序涉及用户需要将鼠标悬停在屏幕上的多个移动点上以启动特定的弹出窗口。目前,我正在监听 JPanel
上的 mouseMoved
事件,并在其上呈现点,然后启动只要光标位于点的特定距离内,就需要弹出窗口。
I am working on an application that involves the user requiring to hover over several moving dots on the screen in order to launch specific popups. At the moment, i am listening for mouseMoved
events on the JPanel
onto which the dots are rendered, and then launching the required pop ups whenever the cursor is within a specific distance of a dot.
当我有数百个点时 - 这可能变得非常昂贵。
When i have hundreds of dots - this probably becomes quite expensive.
理想的解决方案不是代表我的'点'作为小组件,并为每个点注册一个鼠标监听器?
Wouldnt the ideal solution be to represent my 'dots' as small components and register a mouse listener with each dot?
有没有人知道我如何用 JComponent
代表一个小椭圆?
Does anyone know how i might represent a small ellipse with a JComponent
?
非常感谢
推荐答案
这是一些旧代码,展示了如何创建圆形JButton。我会改为扩展JComponent,重写的重要方法是paintComponent()和contains():
Here is some old code which shows how to create a "round" JButton. I would extend JComponent instead and the important methods to override are paintComponent() and 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顺序。
This simplifies the hit detection easily since there is no custom code. Also it allows you to control overlapping of compnents easily since you can control the Z-Order of each component.
这篇关于Java - 自定义形状面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!