我的任务是在 paintComponent() 方法中使用 2D 图形绘制多张图像。但是,我不确定如何添加 MouseListener 以便每张图像都知道它是否被选中。
到目前为止,我的解决方案过于简单地记录鼠标点击的坐标,然后查看它们是否包含在每个图像的边界内。然而,对于具有更复杂边界的图像,这将很困难。
另一种选择是创建简单的形状并将它们放置在图像上,但是具有更复杂边界的图像同样会很困难。在另一个关于 SO 的讨论中发现 here ,有人提到使用 GeneralPath 绘制更复杂的形状。我从未玩过 is,但这似乎令人鼓舞。
在这 2 个选项中,哪个似乎是最好的解决方案,或者是否有其他建议
最佳答案
你的图像是叠加绘制的还是分开绘制的。
如果它们是单独绘制的,那么您应该在 JComponent 上进行自定义绘制。然后您可以使用 GeneralPath 进行绘图。您还需要通过检查鼠标点击是否包含在 GeneralPath 中来实现 contains(...) 方法。如果正确实现了 contains() 方法,则 MouseListener 将正确响应。
这是一个更简单的例子:
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 );
}
}
关于java - Java swing 中的图像映射功能,Points 与 GeneralPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4061791/