2

私の目標は、paintComponent() メソッドで 2D グラフィックスを使用して複数の画像を描画することです。ただし、各画像が選択されているかどうかを知るために MouseListener を追加する方法がわかりません。

これまでの私の解決策は、マウスクリックの座標を記録し、それらが各画像の境界内に含まれているかどうかを確認するだけです。ただし、これはより複雑な境界を持つ画像では困難です。

別のオプションとして、単純な形状を作成して画像の上に配置することもできますが、より複雑な境界を持つ画像は難しいでしょう。ここにあるSO に関する別の議論で 、誰かが GeneralPath を使用してより複雑な形状を描画することに言及しました。で遊んだことはありませんが、これは励みになるようです。

これらの 2 つのオプションのうち、最良の解決策と思われるもの、または他の推奨事項はありますか

4

1 に答える 1

2

画像は重ねて描かれていますか、それとも別々に描かれていますか。

それらが別々に描画される場合は、JComponent でカスタム ペイントを行う必要があります。次に、GeneralPath を使用して描画を行うことができます。また、mouseclick が 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 );

    }
}
于 2010-10-31T04:08:23.480 に答える